关于TabLayout+ViewPager+Fragment 简单明了的用法

先上图

这里写图片描述

创建好布局文件和你需要的fragment数量

"><LinearLayout 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:id="@+id/activity_history"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/page_bg"
    android:orientation="vertical"
    tools:context="au.com.hbuy.aotong.activity.personactivity.usercenter.HistoryActivity">

    <android.support.design.widget.TabLayout
        android:id="@+id/history_user_record_tab"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:tabTextColor="@color/xiankaung"
        app:tabSelectedTextColor="@color/button_color"
        app:tabIndicatorColor="@color/button_color">
    </android.support.design.widget.TabLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/history_user_record_vp"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    </android.support.v4.view.ViewPager>

</LinearLayout>


//  app 为自定义命名空间 可以定制tablayout的文字下划线之类的属性

所需要的fragment根据自己的需要去创建 我这里一共需要三个

package au.com.hbuy.aotong.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

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

import au.com.hbuy.aotong.R;
import au.com.hbuy.aotong.adapter.HistoryOrderAdapter;
import au.com.hbuy.aotong.contronller.common.util.ToastUtil;
import au.com.hbuy.aotong.nethttp.ConfigConstants;
import au.com.hbuy.aotong.nethttp.NetUtils;
import au.com.hbuy.aotong.nethttp.okhttp.ApiClient;
import au.com.hbuy.aotong.nethttp.okhttp.RespPkgListCallback;
import au.com.hbuy.aotong.nethttp.responsebody.Order;

/**
 * Created by wangjungang on 2017/6/13--18:02.
 * <p>
 * E-Mail:mrjungang@outlook.com
 */

public class HistoryOrder extends Fragment {

    private View view;
    private XRecyclerView history_order;
    private ArrayList<Order> data = new ArrayList<>();
    private HistoryOrderAdapter historyOrderAdapter;
    private String Allpager = "";
    private int pager = 1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.historyorder_layout, null);
        initData();
        initView();
        return view;
    }

    private void initData() {
        if (NetUtils.hasAvailableNet(getActivity())) {

            ApiClient.getInstance(getActivity().getApplicationContext()).postForm(ConfigConstants.waitingPay
                    + "/" + mStyle + "?p=1", new RespPkgListCallback<List<Order>>(getActivity(), "") {
                @Override
                public void onFail(List<Order> workOrderBeans) {
                }
                @Override
                public void onSuccess(List<Order> lists, String str, String s, String num) {
                    Allpager = str;
                    data.clear();
                    data.addAll(lists);
                    historyOrderAdapter.notifyDataSetChanged();
                }
            });
        } else {
            ToastUtil.show(getActivity(), "请检查网络");
        }
    }

    private void initDataMore() {
        if (NetUtils.hasAvailableNet(getActivity())) {
            if (Integer.parseInt(Allpager) >= pager){

                ApiClient.getInstance(getActivity()).postForm(ConfigConstants.waitingPay
                        + "/" + mStyle + "?=p" + pager, new RespPkgListCallback<List<Order>>(getActivity(), "") {
                    @Override
                    public void onFail(List<Order> orders) {

                    }

                    @Override
                    public void onSuccess(List<Order> lists, String str, String number, String num) {
                        if (null != lists) {
                            data.addAll(lists);
                            pager++;
                            Allpager = str;
                            historyOrderAdapter.notifyDataSetChanged();
                        }
                    }
                });
            }else {
                ToastUtil.show(getActivity(),"数据全部加载完毕");
            }
        } else {
            ToastUtil.show(getActivity(), "请检查网络");
        }
    }


    private void initView() {
        history_order = (XRecyclerView) view.findViewById(R.id.history_order_recyc);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        history_order.setLayoutManager(layoutManager);
        history_order.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        history_order.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        history_order.setArrowImageView(R.drawable.iconfont_downgrey);
        //设置空试图
        ImageView blance_empty = (ImageView) view.findViewById(R.id.user_orser_empty);
        history_order.setEmptyView(blance_empty);
        //设置适配器
        historyOrderAdapter = new HistoryOrderAdapter(data, getActivity());
        history_order.setAdapter(historyOrderAdapter);

        //上拉刷新 下拉加载
        history_order.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initData();
                history_order.refreshComplete();
            }

            @Override
            public void onLoadMore() {
                initDataMore();
                history_order.loadMoreComplete();
            }
        });
    }
}

这是其中一个   中间代码可以忽略

Activity中设置这几个控件之间的关联

package au.com.hbuy.aotong.activity.personactivity.usercenter;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;

import au.com.hbuy.aotong.R;
import au.com.hbuy.aotong.adapter.UserHistoryVPadapter;
import au.com.hbuy.aotong.base.BaseActivity;

public class HistoryActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("历史记录");
        initView();
    }
    private void initView() {
        //初始化VIewPager
        ViewPager  vp = (ViewPager) findViewById(R.id.history_user_record_vp);
        //s适配器准备
        UserHistoryVPadapter adapter = new UserHistoryVPadapter(getSupportFragmentManager());
        vp.setAdapter(adapter);
        //初始化tabLayout
        TabLayout tab = (TabLayout) findViewById(R.id.history_user_record_tab);
        tab.setTabMode(TabLayout.MODE_FIXED);
        //ViewPager关联tabLayout
        tab.setupWithViewPager(vp);
    }
    @Override
    protected int getContentView() {
        return R.layout.activity_history;
    }
}
这里要明白项目继承的Activity是那个包里边的

adapter的创建 将我们的fragment关联

package au.com.hbuy.aotong.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import au.com.hbuy.aotong.fragment.HistoryOrder;
import au.com.hbuy.aotong.fragment.HistoryPack;
import au.com.hbuy.aotong.fragment.HistoryPackRecord;

/**
 * Created by wangjungang on 2017/6/13--17:55.
 * <p>
 * E-Mail:mrjungang@outlook.com
 */

public class UserHistoryVPadapter extends FragmentPagerAdapter {

    private String mTitle [] = new String[]{"打包记录","包裹","订单"};
    public UserHistoryVPadapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        if (1 == position){
            return new HistoryPack();
        }
        if (2 == position){
            return new HistoryOrder();
        }
        return new HistoryPackRecord();
    }
    @Override
    public int getCount() {
        return mTitle.length;
    }
    @Override
    public CharSequence getPageTitle(int position) {
        return mTitle[position];
    }
}

代码就这么多 简单明了将这几个控件结合起来

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值