TabLayout+viewPager

实现过程

1.在build.gradle中添加依赖,例如:
compile 'com.android.support:support-v4:23.4.0'
compile 'com.android.support:design:23.4.0'

2.然后在主布局文件activity_main.xml中添加布局控件:

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


    <LinearLayout
        android:id="@+id/home_tab_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/main_color"
        android:gravity="center_vertical">


        <ImageView
            android:id="@+id/home_tab_search_img"
            android:layout_width="40dp"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:padding="4dp"
            android:src="@mipmap/search"/>


        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            app:tabMode="scrollable"
            app:tabTextColor="@color/white_color"
            app:tabSelectedTextColor="#E6E6FA"
            app:tabIndicatorColor="@color/white_color"/>


        <ImageView
            android:id="@+id/home_tab_rank_img"
            android:layout_width="40dp"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:padding="4dp"
            android:src="@mipmap/icon_toplist"/>


    </LinearLayout>


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


</LinearLayout>

3.接下来就我们集添加适配器

public class MyPageAdapter extends FragmentStatePagerAdapter {
    private List<Fragment> fragments;
    private List<String> titles;


    public MyPageAdapter(FragmentManager fm, List<Fragment> fragments, List<String> titles) {
        super(fm);
        this.fragments = fragments;
        this.titles = titles;
    }


    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }


    @Override
    public int getCount() {
        return fragments.size() != 0 ? fragments.size() : 0;
    }


    @Override
    public CharSequence getPageTitle(int position) {
        if (titles != null)
            return titles.get(position);
        else
            return null;
    }
}

4.在HomeFragment中使用

 @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.vp_live_score)
    ViewPager live_view;
    private List<String> mTitles;
    private List<Fragment> fragments;
    private MyPageAdapter pagerAdapter;


    @Override
    protected int getLayout() {
        return R.layout.fragment_home;
    }


    @Override
    protected void initView() {


    }


    @Override
    protected void initData() {
        fragments = new ArrayList<>();
        mTitles=new ArrayList<>();
        mTitles.add("关注");
        mTitles.add("热门");
        mTitles.add("最新");
        mTitles.add("附近");
        fragments.add(new AttentionFragment());
        fragments.add(new HotFragment());
        fragments.add(new NewestFragment());
        fragments.add(new NearbyFragment());
        pagerAdapter = new MyPageAdapter(getChildFragmentManager(), fragments,mTitles);
        live_view.setAdapter(pagerAdapter);
        tabLayout.setupWithViewPager(live_view);
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden){


        }
 }

5.设置当前位置

//设置默认选择tab

tabLayout.setCurrentTab(1);

live_view.setCurrentItem(1);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值