TabLayout配合Viewpager+Fragment结合使用

//------------------------------------布局文件----------------------------------------------
<?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">

    <android.support.design.widget.TabLayout
        android:id="@+id/table"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </android.support.design.widget.TabLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </android.support.v4.view.ViewPager>
</LinearLayout>


//-----------------------------------------大类标题---------------------------------------------

public class Frag_02 extends Fragment {


    private FragmentTransaction transaction;
    private ArrayList<String> title;
    private ViewPager pager;
    private TabLayout table;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag_02,container,false);

        //控件

        table = view.findViewById(R.id.table);
        pager = view.findViewById(R.id.pager);

        //页面的集合
        final ArrayList<Fragment> fragments = new ArrayList<>();

        //标题的集合
        title = new ArrayList<>();

        final Tab_frag_01 tab_frag_01 = new Tab_frag_01();
        final Tab_frag_02 tab_frag_02 = new Tab_frag_02();
        final Tab_frag_03 tab_frag_03 = new Tab_frag_03();

        fragments.add(tab_frag_01);
        fragments.add(tab_frag_02);
        fragments.add(tab_frag_03);

        title.add("山东");
        title.add("小韩系");
        title.add("河南");

        pager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                if(i==0){
                    return tab_frag_01;
                }else if(i==1){
                    return tab_frag_02;
                }else{
                    return tab_frag_03;
                }
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return title.get(position);
            }
        });

        pager.setOffscreenPageLimit(fragments.size());

        table.setupWithViewPager(pager);
        return view;
    }
}

//-------------------------------------小类内容--------------------------------------------------


public class Tab_frag_01 extends Fragment {
    private String path = "http://api.expoon.com/AppNews/getNewsList/type/1/p/";
    private List<Beans.DataBean> list = new ArrayList<>();
    private PullToRefreshListView plistview;
    private int num = 1;
    private Handler handler = new Handler();
    private MyAdapter myAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tab_frag_01,container,false);

        //控件
        plistview = view.findViewById(R.id.plistview1);

        //支持上拉加载   下拉刷新
        plistview.setMode(PullToRefreshListView.Mode.BOTH);

        //上拉加载  下拉刷新的方法
        initjias();
        //解析的方法
        RequstData(1);

        return view;

    }

    private void initjias() {
        plistview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        num = 1;
                        list.clear();
                        RequstData(1);
                    }
                },3000);
                myAdapter.notifyDataSetChanged();
                plistview.onRefreshComplete();  //停止刷新
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        num++;
                        RequstData(num);
                    }
                },3000);
                myAdapter.notifyDataSetChanged();
                plistview.onRefreshComplete();  //停止刷新
            }
        });
    }

//调用
    private void RequstData(int num) {

        NetUtils.getInstsance().RequestData(path+num, Beans.class, new NetUtils.CallBacks<Beans>() {
            @Override
            public void onSuccess(Beans beans) {
                list.addAll(beans.getData());
                myAdapter = new MyAdapter(getActivity(), list);
                plistview.setAdapter(myAdapter);
            }
        });
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值