tablayout 在布局文件中展示
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_matter_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ll_detail"
android:layout_marginTop="@dimen/margin_space_10"
app:tabBackground="@color/white"
app:tabIndicatorColor="@color/appThemeColor"
app:tabIndicatorHeight="2dp"
app:tabSelectedTextColor="@color/appThemeColor"
app:tabTextColor="#66000000" />
tablayout 初始化及切换
/**
* 切换卡 tablayout
*/
String[] tabArray = {"工作沟通", "反馈情况"};
private void initTab() {
for (int i = 0; i < 2; i++) {
TabLayout.Tab tab = tabMatterDetail.newTab();
//注意!!!这里就是添加我们自定义的布局
tab.setCustomView(getCustomView(i));
//这里是初始化时,默认item0被选中,setSelected(true)是为了给图片和文字设置选中效果,代码在文章最后贴出
if (i == 0) {
(tab.getCustomView().findViewById(R.id.tab_iv)).setSelected(true);
( tab.getCustomView().findViewById(R.id.tv_title)).setSelected(true);
}
tabMatterDetail.addTab(tab);
}
if("1".equals(type)){
initSelect(0);
}else{
initSelect(1);
}
tabMatterDetail.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
if (tab.getPosition() == 0) {
//切换逻辑
type="1";
} else {
//切换逻辑
type="2";
}
(tab.getCustomView().findViewById(R.id.tab_iv)).setSelected(true);
(tab.getCustomView().findViewById(R.id.tv_title)).setSelected(true);
//全部逻辑
、、、
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
(tab.getCustomView().findViewById(R.id.tab_iv)).setSelected(false);
(tab.getCustomView().findViewById(R.id.tv_title)).setSelected(false);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
注意!!!这里就是我们自定义的布局tab_item
public View getCustomView(int position){
View view= LayoutInflater.from(this).inflate(R.layout.layout_common_tablayout_style1,null);
ImageView iv= view.findViewById(R.id.tab_iv);
TextView tv=view.findViewById(R.id.tv_title);
switch (position){
case 0:
iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_matter_detail_gzgt));
tv.setText(tabArray[0]);
tvTitleArray[0]=tv;
imageViewArray[0]=iv;
break;
case 1:
iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_matter_detail_fkqk));
tv.setText(tabArray[1]);
tvTitleArray[1]=tv;
imageViewArray[1]=iv;
break;
}
return view;
}
TextView tvTitleArray[]=new TextView[2];
ImageView imageViewArray[]=new ImageView[2];
private void initSelect(int i){
tvTitleArray[i].setSelected(true);
imageViewArray[i].setSelected(true);
}
layout_common_tablayout_style1布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
>
<ImageView
android:id="@+id/tab_iv"
android:layout_width="20dip"
android:layout_height="20dip"
android:src="@drawable/tab_group_sel"
>
</ImageView>
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="人员"
android:textSize="@dimen/textsize_16"
android:textColor="@color/tablayout_common_tv_bg"
android:layout_marginLeft="10dip"
>
</TextView>
</LinearLayout>