博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CommonTabLayout+ViewPager快速完成APP首页搭建
阅读量:5878 次
发布时间:2019-06-19

本文共 7200 字,大约阅读时间需要 24 分钟。

 款APP开始的时候往往少不了多页面的切换,这就涉及到viewpager的使用,以前往往用Google自带的效果去实现,比较麻烦不说,后面做出来的效果还不如人意。

下面就利用CommonTabLayout+ViewPager来实现类似各电商APP首页的效果;

搭建很简单,第一步,新建一个工程。在build.gradle里面加入下面的引用:

compile 'com.flyco.roundview:FlycoRoundView_Lib:1.1.2@aar'compile 'com.flyco.tablayout:FlycoTabLayout_Lib:1.5.0@aar'compile 'com.android.support:design:24.2.1'compile 'com.nineoldandroids:library:2.4.0'

这样就能使用CommTabLayout插件了;

下面是activity_main.xml文件,我在里面加入了一个FloatingActionButton。

先创建几个fagment用来做viewPager的元素,我在viewPager里面加了三个fragment,都是非常简单的布局;这些fragment继承了我自己创建的一个BaseFragment;

BaseFragment

package com.learn.bob.testfragmentadapter;import android.content.Context;import android.os.Bundle;import android.support.v4.app.Fragment;public class BaseFragment extends Fragment {    public int dialogTheme;    public Context mContext;        @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);    }    @Override    public void onPause() {        super.onPause();    }    @Override    public void onResume() {        super.onResume();    }}

 

package com.learn.bob.testfragmentadapter; import android.content.Context; import android.os.Bundle; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /**  * Created by Administrator on 2018/1/7.  */ public class ThirdFragment extends BaseFragment {
private Context mContext; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mContext = getActivity(); return inflater.inflate(R.layout.fragment_thirdd, container, false); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); initView(); } private void initView(){
} }

 

下面是MainActivity,

package com.learn.bob.testfragmentadapter;import android.support.design.widget.FloatingActionButton;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentPagerAdapter;import android.support.v4.view.ViewPager;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Toast;import com.flyco.tablayout.CommonTabLayout;import com.flyco.tablayout.listener.CustomTabEntity;import com.flyco.tablayout.listener.OnTabSelectListener;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity{    private ViewPager mViewPager;    private CommonTabLayout mTab;    private FirstFragment firstFragment;    private FirstReplaceFragment firstReplaceFragment;    private SecondFragment secondFragment;    private ThirdFragment thirdFragment;    private ViewPagerAadpter viewPagerAadpter;    private List
fragmentList; private ArrayList
mTabEntities = new ArrayList<>(); private FloatingActionButton mBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView(){ mViewPager = (ViewPager)findViewById(R.id.view_main); mTab = (CommonTabLayout) findViewById(R.id.tab_main); mBtn = (FloatingActionButton)findViewById(R.id.float_button); mBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(null != firstFragment){
// Toast.makeText(MainActivity.this,"点击了",Toast.LENGTH_LONG).show(); } } }); if(null == fragmentList){ fragmentList = new ArrayList
(); } if(null == firstFragment){ firstFragment = new FirstFragment(); fragmentList.add(firstFragment); mTabEntities.add(new TabEntity("首页",R.mipmap.ic_launcher,R.mipmap.ic_launcher)); } if(null == secondFragment){ secondFragment = new SecondFragment(); fragmentList.add(secondFragment); mTabEntities.add(new TabEntity("扉页",R.mipmap.ic_launcher,R.mipmap.ic_launcher)); } if(null == thirdFragment){ thirdFragment = new ThirdFragment(); fragmentList.add(thirdFragment); mTabEntities.add(new TabEntity("尾页",R.mipmap.ic_launcher,R.mipmap.ic_launcher)); } viewPagerAadpter = new ViewPagerAadpter(getSupportFragmentManager(),fragmentList); mViewPager.setAdapter(viewPagerAadpter); mTab.setTabData(mTabEntities); mViewPager.setOffscreenPageLimit(3); //为tab页的点击添加监听事件 mTab.setOnTabSelectListener(new OnTabSelectListener() { @Override public void onTabSelect(int position) { mViewPager.setCurrentItem(position); } @Override public void onTabReselect(int position) { } }); //为viewPager的滑动添加监听事件 mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { mTab.setCurrentTab(position); } @Override public void onPageScrollStateChanged(int state) { } }); } //viewPager的适配器 private class ViewPagerAadpter extends FragmentPagerAdapter{ private List
fragments; private FragmentManager fragmentManager; public ViewPagerAadpter(FragmentManager fm, List
fragmentList) { super(fm); this.fragments = fragmentList; this.fragmentManager = fm; } @Override public Fragment getItem(int position) { return fragments.get(position); } @Override public int getCount() { return fragments.size(); } }}

在CusstommonTabEntity的基础上我添加了一个TabEntity的实体类来定义我的tab页;

package com.learn.bob.testfragmentadapter;import com.flyco.tablayout.listener.CustomTabEntity;/** * Created by bob on 2017-4-12 14:37 */public class TabEntity implements CustomTabEntity {    public String title;    public int selectedIcon;    public int unSelectedIcon;    public TabEntity(String title, int selectedIcon, int unSelectedIcon) {        this.title = title;        this.selectedIcon = selectedIcon;        this.unSelectedIcon = unSelectedIcon;    }    public TabEntity(String title) {        this.title = title;    }    @Override    public String getTabTitle() {        return title;    }    @Override    public int getTabSelectedIcon() {        return selectedIcon;    }    @Override    public int getTabUnselectedIcon() {        return unSelectedIcon;    }}

这是最终的效果,在xml里面能够设置下面tab页滑动时线条文字显示的颜色,也可以设置成无线条

 

项目GitHub地址:

转载于:https://www.cnblogs.com/BobAdmin/p/8267203.html

你可能感兴趣的文章
8个很实用的在线工具来提高你的Web设计和开发能力
查看>>
P1026 统计单词个数
查看>>
AndroidStudio EventBus报错解决方法its super classes have no public methods with the @Subscribe...
查看>>
Python RGB 和HSV颜色相互转换
查看>>
mybatis分页练手
查看>>
.net数据库连接字符串加密
查看>>
[js高手之路] html5 canvas系列教程 - 状态详解(save与restore)
查看>>
文件监控
查看>>
poi excel 常用api
查看>>
AD提高动态的方法(附SNR计算)
查看>>
[转]轻松实现可伸缩性,容错性,和负载平衡的大规模多人在线系统
查看>>
五 数组
查看>>
也谈跨域数据交互解决方案
查看>>
EntityFramework中使用Include可能带来的问题
查看>>
activity 用 service 更新界面
查看>>
我的时间管理——充分利用WindowsPhone、Android等设备,实现真正的无压工作!
查看>>
面试题28:字符串的排列
查看>>
GetParent( ) 和AfxGetMainWnd( )
查看>>
css important
查看>>
MySQL学习笔记20:数据备份与还原
查看>>