Android框架之路——Tablayout+ViewPager+Fragment的使用

i、参考博客:

  • TabLayout、ViewPager和Fragment的多页面滑动
  • 首页-底部&顶部Tab导航(菜单栏)的实现:TabLayout+ViewPager+Fragment

ii、实现效果:

   Android框架之路——Tablayout+ViewPager+Fragment的使用_第1张图片

iii、使用教程:

1. 导包,添加依赖

compile 'com.android.support:appcompat-v7:22.2.0'  
compile 'com.android.support:design:22.2.0' 

2. 创建Fragment和布局文件

创建fragment1,fragment2,fragment3和fragment4及其布局文件。

// fragment1.java
public class Fragment1 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.layout_fragment1, container, false);
    }
}

layout_fragment1.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="fragment1"/>

LinearLayout>

3. 创建FragmentPagerAdapter子类管理Fragment

这里对其还是进行了优化,fragment只实例化一次。

public class PageAdapter extends FragmentPagerAdapter {

    private int num;
    private HashMap<Integer, Fragment> mFragmentHashMap = new HashMap<>();

    public PageAdapter(FragmentManager fm, int num) {
        super(fm);
        this.num = num;
    }

    @Override
    public Fragment getItem(int position) {

        return createFragment(position);
    }

    @Override
    public int getCount() {
        return num;
    }

    private Fragment createFragment(int pos) {
        Fragment fragment = mFragmentHashMap.get(pos);

        if (fragment == null) {
            switch (pos) {
                case 0:
                    fragment = new Fragment1();
                    Log.i("fragment", "fragment1");
                    break;
                case 1:
                    fragment = new Fragment2();
                    Log.i("fragment", "fragment2");
                    break;
                case 2:
                    fragment = new Fragment3();
                    Log.i("fragment", "fragment3");
                    break;
                case 3:
                    fragment = new Fragment4();
                    Log.i("fragment", "fragment4");
                    break;
            }
            mFragmentHashMap.put(pos, fragment);
        }
        return fragment;
    }
}

4. activity_main中添加

<android.support.design.widget.TabLayout
    android:id="@+id/tabLayout"
    android:layout_width="match_parent"
    android:layout_height="50dp"/>

<android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

5. MainActivity中实现

TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
//添加标签
tabLayout.addTab(tabLayout.newTab().setText("tab1"));
tabLayout.addTab(tabLayout.newTab().setText("tab2"));
tabLayout.addTab(tabLayout.newTab().setText("tab3"));
tabLayout.addTab(tabLayout.newTab().setText("tab4"));

//设置adapter,滑动时间
final ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setAdapter(new PageAdapter(getSupportFragmentManager(), tabLayout.getTabCount()));
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

//绑定tab点击事件
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
});

6. Tab想要设置icon的话,可以继续用setIcon方法,如下:

tabLayout.addTab(tabLayout.newTab().setText("tab1").setIcon(R.drawable.ic_menu_camera));



个人公众号:每日推荐一篇技术博客,坚持每日进步一丢丢…欢迎关注,想建个微信群,主要讨论安卓和Java语言,一起打基础、用框架、学设计模式,菜鸡变菜鸟,菜鸟再起飞,愿意一起努力的话可以公众号留言,谢谢…

你可能感兴趣的:(Android,Android框架之路)