Android TabLayout 结合ViewPager实现标签页切换

第一步导入tablayout的依赖

在module里的build.gradle

compile 'com.android.support:design:25.3.1'
如果和本项目的V7包冲突,只需要把版本号改成一致就可以了


写布局,注意Tablayout是support.design.widget下的

<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">
    <android.support.design.widget.TabLayout
        android:id="@+id/tablayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:tabMode="scrollable"
        app:tabSelectedTextColor="#FC4E4E"
        ></android.support.design.widget.TabLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"></android.support.v4.view.ViewPager>

</LinearLayout>

创建fragment布局

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

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="fragment"
        android:textSize="40sp" />
</LinearLayout>

创建fragment

public class MyFragment extends Fragment {
    public static final String ARG_PAGE = "ARG_PAGE";
    private int mPage;

    public static MyFragment newInstance(int Page) {
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, Page);
        MyFragment myFragment = new MyFragment();
        myFragment.setArguments(args);
        return myFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPage = getArguments().getInt(ARG_PAGE);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag_layout,container,false);
        TextView tv = (TextView) view.findViewById(R.id.tv);
        tv.setText("FragMent #" + mPage);
        return view;
    }
}

我们使用的是ViewPager结合Fragment,所以要配置适配器FragMentPagerAdapter

public class MyFragmentAdapter extends FragmentPagerAdapter {
    final int PAGE_COUNT = 9;
    private String tabTitles[] = new String[]{"tab1", "tab2", "tab3", "tab4", "tab5", "tab6", "tab7", "tab8", "tab9"};
    private Context context;

    public MyFragmentAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.context = context;
    }

    @Override
    public Fragment getItem(int position) {
        return MyFragment.newInstance(position + 1);
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        return tabTitles[position];
    }
}

适配器与fragmen都写完了,接下来在Activity里面

public class TabActivity extends FragmentActivity {
    private ViewPager vp;
    private MyFragmentAdapter adapter;
    private TabLayout tabLayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_layout);
        vp = (ViewPager) findViewById(R.id.viewpager);
        tabLayout = (TabLayout) findViewById(R.id.tablayout);
        //配置适配器
        adapter = new MyFragmentAdapter(getSupportFragmentManager(), TabActivity.this);
        vp.setAdapter(adapter);
        tabLayout.setupWithViewPager(vp);
//        设置此模式,在title条目数比较多的时候会拥挤在一起,所以如果条目很多一个屏幕容不下的时候就不要设置次方法
//        tabLayout.setTabMode(TabLayout.MODE_FIXED);
    }
}
在初始化Tablayout的时候,setupWithViewPager这个方法出不来,仔细找原来是导包导错了,正确的包是
import android.support.design.widget.TabLayout;
千万不要搞错了



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值