TabLayout滑动效果显示

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <android.support.design.widget.TabLayout
        android:layout_below="@+id/lll"
        android:id="@+id/tab"
        app:tabGravity="center"
        app:tabIndicatorColor="@color/colorAccent"
        app:tabMode="scrollable"
        app:tabSelectedTextColor="@color/colorPrimaryDark"
        app:tabTextColor="@color/colorPrimary"
        android:layout_width="match_parent"
        android:layout_height="40dp"></android.support.design.widget.TabLayout>

    <android.support.v4.view.ViewPager

        android:id="@+id/vp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v4.view.ViewPager>
</LinearLayout>

activity

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

import chenshasha.bwie.com.day20.R;

/**
 * Created by A on 2017/10/24.
 */

public class Shouye extends Fragment {
    private TabLayout tab;
    private ViewPager vp;
    private List<String> list;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.shouye, container, false);
        tab = (TabLayout) view.findViewById(R.id.tab);
        vp = (ViewPager)view.findViewById(R.id.vp);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        list = new ArrayList<>();
        list.add("推荐");
        list.add("十九大");
        list.add("热点");
        list.add("科技");
        vp.setOffscreenPageLimit(list.size());
        //进行一个获取数据的方法
        vp.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public CharSequence getPageTitle(int position) {
                return list.get(position);
            }

            @Override
            public Fragment getItem(int position) {
                Fragment fragment = null;


                switch (position) {
                    case 0:
                        fragment = new Tuijian();
                        break;
                    case 1:
                        fragment = new Shijiuda();
                        break;
                    case 2:
                        fragment = new Redian();
                        break;
                    case 3:
                        fragment = new Keji();
                        break;

                    default:
                        break;
                }
                return fragment;
            }
            @Override
            public int getCount() {
                return list.size();
            }
        });
        //关联
        tab.setupWithViewPager(vp);

    }
}


发布了8 篇原创文章 · 获赞 0 · 访问量 2343
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览