Android TabLayout布局记载
.xml主页面布局
<android.support.design.widget.TabLayout android:id="@+id/business_head" android:layout_width="match_parent" android:layout_height="50dp" app:tabMode="fixed"/> <com.always.library.View.LrRecycleview.recyclerview.LRecyclerView //自定义的RecyclerView android:id="@+id/listview" android:layout_below="@+id/business_head" android:layout_width="match_parent" android:layout_height="match_parent"/>
页面
主MainActivity
private static final String[] titleList = {"逾期订单,"已完成订单};
@Override protected void setData() { initList(); business(); } private void business() { final LinearLayoutManager manager = new LinearLayoutManager(mContext); listview.setLayoutManager(manager); adapter = new RCommonAdapter<BussessResBean.DataBean>(mContext, R.layout.item_activity_overdue_list) { @Override protected void convert(ViewHolder holder, final BussessResBean.DataBean bean, int position) { } }; adapter.setOnItemClickListener(new RMultiItemTypeAdapter.OnItemClickListener<BussessResBean.DataBean>() { @Override public void onItemClick(RecyclerView.ViewHolder holder, BussessResBean.DataBean data, int position) { //点击具体订单完成的事件 } }); LRecyclerViewAdapter mLRecyclerViewAdapter = new LRecyclerViewAdapter(adapter); listview.setEmptyView(emptyView); listview.setAdapter(mLRecyclerViewAdapter); listview.setOnRefreshListener(new OnRefreshListener() { @Override public void onRefresh() { getData(); } }); listview.setRefreshing(true); } private void initList() { for (int i = 0; i < titleList.length; i++) { //遍历数组 View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_tab, null); //单个模块 ((TextView) inflate.findViewById(R.id.tv_title)).setText(titleList[i]); //填入模块名称
bushead.addTab(bushead.newTab().setCustomView(inflate)); //把有名称的模块放入TabLayout中
bushead.addOnTabSelectedListener(new TabLayout.BaseOnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
int position = tab.getPosition();
switch (position) {
case 0:
status = "0";
break;
case 1:
status = "1";
break;
}
listview.setRefreshing(true);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
} }
其中getdata()为获取RecyclerView数据
TabLayout子布局item_tab如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="wrap_content" android:gravity="center" android:padding="8dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:id="@+id/tv_title" android:layout_gravity="center" android:textColor="@color/defcolor0" android:textSize="@dimen/textsize_15" /> </LinearLayout>
代码这里只贴出重要部分,
逻辑是先声明布局,然后遍历TabLayout的数组,
当点击第一个item的时候,状态为0,通过getData();方法根据状态通过接口获取到,订单数据,声明一个adapter
用户展示订单详情,这样就把Tablayout和下方的RecyclerView关联起来了,实现点击一个TabLayout中的item
展示出来的订单数和信息均不相同的功能