Tablayout 横向滚动条的简单实现

该博客介绍了如何在Android中使用TabLayout和ViewPager实现横向滚动的选项卡。首先,添加必要的依赖库,然后创建一个包含TabLayout和ViewPager的布局。在MainActivity中设置数据源并关联TabLayout与ViewPager。每个选项卡对应一个Fragment,通过FragmentPagerAdapter管理。最后,自定义Adapter以提供选项卡标题和对应的Fragment实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


//导入依赖库

package com.example.tablayout;

import android.support.design.widget.TabLayout;
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.util.Log;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private List<String> datas=new ArrayList<String>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TabLayout tabLayout= (TabLayout) findViewById(R.id.tas);
        ViewPager viewPager= (ViewPager) findViewById(R.id.vp);

        datas.add("推荐");
        datas.add("要闻");
        datas.add("娱乐");
        datas.add("科技");
        datas.add("汽车");
        datas.add("体育");
        datas.add("图片");
        datas.add("动漫");
        datas.add("社会");
        datas.add("游戏");

        viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));

        //进行关联
        tabLayout.setupWithViewPager(viewPager);
    }

    class MyAdapter extends FragmentPagerAdapter {

        //带参的构造方法
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        //返回选项卡的文本 ,,,添加选项卡
        @Override
        public CharSequence getPageTitle(int position) {
            return datas.get(position);
        }

       
@Override
public Fragment getItem(int position) {
    Fragment f = null;
    switch (position){
        case 0:f = new TjFragment();//动态
            break;
        case 1:f = new KcFragment();//热门
            break;
        case 2:f = new SzFragment();//发现
            break;
        case 3:f = new ZyFragment();//
            break;
    }

    return f;
}
@Override public int getCount() { return datas.size();//返回选项卡的数量 } }}

//布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context="com.example.tablayout.MainActivity">


    <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/tas"
        app:tabGravity="center"
        app:tabIndicatorColor="@color/colorAccent"
        app:tabMode="scrollable"
        app:tabSelectedTextColor="@color/colorPrimaryDark"
        app:tabTextColor="@color/colorPrimary"
        ></android.support.design.widget.TabLayout>

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

</android.support.constraint.ConstraintLayout>

//fragment的传值
public class ContentFragment extends Fragment{

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=View.inflate(getActivity(),R.layout.content,null);
        TextView textView= (TextView) view.findViewById(R.id.tv);

        Bundle bundle=getArguments();
        textView.setText(bundle.getString("key"));


        return view;
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值