TabLayout+ViewPager简易实现

//在gradle中配置依赖compile 'com.android.support:design:25.3.1'

//布局文件

//activity_main布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <View
        android:layout_width="match_parent"
        android:layout_height="40dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">

        <android.support.design.widget.TabLayout
            android:id="@+id/tablayout"
            app:tabMode="scrollable"
            app:tabTextColor="#9900ff"
            app:tabSelectedTextColor="#ff9999"
            app:tabIndicatorColor="#ffff00"
            app:tabIndicatorHeight="1dp"
            android:layout_width="match_parent"
            android:layout_height="40dp">
        </android.support.design.widget.TabLayout>

        <ImageView
            android:layout_width="40dp"
            android:layout_height="40dp" />
    </LinearLayout>

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

//Fragment布局

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

    <TextView
        android:textColor="#ffff0000"
        android:id="@+id/tv_fragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />
</FrameLayout>

//MyFragment中

public class MyFragment extends Fragment {

    private TextView textView;
    private String text;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        text = arguments.getString("text");
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.myfragment, container, false);
        textView = (TextView) inflate.findViewById(R.id.tv_fragment);
        textView.setText(text);
        return inflate;
    }
}

//适配器

public class MyPagerAdapter extends FragmentPagerAdapter {

    private String[] titles = {"推荐", "热点", "北京", "视频", "军事娱乐", "热点", "北京", "视频", "军事娱乐"};

    private List<Fragment> mFragments = new ArrayList<>();

    //这个为开启事务
    private FragmentManager mFragmentManager;

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
        mFragmentManager = fm;
    }

    public MyPagerAdapter(FragmentManager fm, List<Fragment> tempFragment) {
        super(fm);
        mFragmentManager = fm;
        mFragments = tempFragment;
    }

@Override
    public Fragment getItem(int position) {
        //new 我对应得fragment
        MyFragment myFragment = new MyFragment();
        Bundle bundle = new Bundle();
        bundle.putString("text", titles[position]);
        myFragment.setArguments(bundle);
        return myFragment;
    }

    //记得盘空
    @Override
    public int getCount() {
        return titles == null ? 0 : titles.length;
    }


    //设置tablayout的每个tab的标题
    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }
}

//MainActivity中

public class MainActivity extends FragmentActivity {

    private TabLayout mTablayout;
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTablayout = (TabLayout) findViewById(R.id.tablayout);

        TabLayout.Tab tab = mTablayout.newTab();
        TabLayout.Tab tab1 = mTablayout.newTab();
        TabLayout.Tab tab2 = mTablayout.newTab();
        TabLayout.Tab tab3 = mTablayout.newTab();
        

        mViewPager = (ViewPager) findViewById(R.id.viewpager);

        //传入我们自定义的pagerAdapter
        mViewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));

        //将viewpager和tablayout关联
        mTablayout.setupWithViewPager(mViewPager);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值