TabLayout的学习以及BUG,Viewpager的指示器

来源:互联网 发布:老九门哪个软件 编辑:程序博客网 时间:2024/05/23 13:38

依赖:

compile 'com.android.support:design:25.3.1'

初始化tabLayout:

 mTabLayout = (TabLayout) findViewById(R.id.tabLayout);

Xmal 中的设置:

    <android.support.design.widget.TabLayout        android:id="@+id/tabLayout"        app:tabTextColor="#55000000"        app:tabSelectedTextColor="#ff0000"        android:layout_width="match_parent"        android:layout_height="wrap_content">    </android.support.design.widget.TabLayout>    <TextView        android:text="共有37件商品"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:textColor="@color/half_black"        android:background="#CD8500"        android:padding="10dp"        />    <android.support.v4.view.ViewPager        android:id="@+id/vp_product_list"        android:layout_width="match_parent"        android:layout_height="match_parent">    </android.support.v4.view.ViewPager>


给TabLayout 设置标题。并且绑定ViewPager,这里,一定要将标题用List<String>另外在存一下,不然会造成标题不发显示。然后在Adapter中在   getPageTitle()
//  将tab和viewpager绑定        mTabLayout.addTab(mTabLayout.newTab().setText("默认"), true);        mTabLayout.addTab(mTabLayout.newTab().setText("价格"), false);        mTabLayout.addTab(mTabLayout.newTab().setText("销量"), false);        mTitleList.add("默认");        mTitleList.add("价格");        mTitleList.add("销量");        mFragmentList.add( new ProductDefaultFragment());        mFragmentList.add(new ProductPriceFragment());        mFragmentList.add(new ProductSalesFragment());        mProductListAdapter = new ProductListAdapter(getSupportFragmentManager(), mFragmentList,mTitleList);        vp_product_list.setAdapter(mProductListAdapter);        mTabLayout.setupWithViewPager(vp_product_list);

在adapter中设置

public class ProductListAdapter extends FragmentPagerAdapter {    private List<BaseFragment> mFragmentList;    private  List<String> mTitleList;    public ProductListAdapter(FragmentManager fm, List<BaseFragment> fragmentList, List<String> titleList) {        super(fm);        this.mFragmentList=fragmentList;        this.mTitleList=titleList;    }    @Override    public CharSequence getPageTitle(int position) {        return mTitleList.get(position);    }    @Override    public BaseFragment getItem(int position) {        return mFragmentList.get(position);    }    @Override    public int getCount() {        return mFragmentList.size();    }}


原创粉丝点击