1.XML布局中
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent">
<com.cobe.app.widget.BanViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/tabLayout" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="@color/white"
app:tabBackground="@color/transparent"
app:tabGravity="fill"
app:tabIndicatorHeight="0dp"
app:tabMode="fixed"
app:tabRippleColor="@null" />
</RelativeLayout>
2.MainTab
public class MainTab {
private Fragment fragment;
private String name;
private int res;
public Fragment getFragment() {
return fragment;
}
public void setFragment(Fragment fragment) {
this.fragment = fragment;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRes() {
return res;
}
public void setRes(int res) {
this.res = res;
}
public MainTab(Fragment fragment, String name, int res) {
this.fragment = fragment;
this.name = name;
this.res = res;
}
}
3.子布局:list_item_nav.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img"
android:layout_centerHorizontal="true"
android:layout_width="20dp"
android:layout_height="20dp"
android:scaleType="centerInside"
android:src="@mipmap/icon_camera" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_below="@+id/img"
android:layout_centerHorizontal="true"
android:text="首页"
android:textColor="@drawable/tab_text_color"
android:textSize="12dp" />
</RelativeLayout>
4.选中与未选中时文字的颜色设置:drawable/tab_text_color
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/blue" android:state_selected="true"/>
<item android:color="@color/color_333" />
</selector>
5.图标选中与未选中时的设置(有几个tab就新建几个):drawable/main_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/icon_tab1" android:state_selected="true"/>
<item android:drawable="@mipmap/icon_tab1_un"/>
</selector>
6.fragment的适配器:MainFragmentAdapter
public class MainFragmentAdapter extends FragmentPagerAdapter {
private Context mContext;
private List<MainTab> mainTabs;
public MainFragmentAdapter(FragmentManager fm, Context context, List<MainTab> mainTabs) {
super(fm);
this.mContext = context;
this.mainTabs=mainTabs;
}
public View getView(int pos){
View view = LayoutInflater.from(mContext).inflate(R.layout.list_item_nav,null);
TextView name = view.findViewById(R.id.name);
ImageView img = view.findViewById(R.id.img);
name.setText(mainTabs.get(pos).getName());
img.setImageResource(mainTabs.get(pos).getRes());
return view;
}
@Override
public Fragment getItem(int position) {
return mainTabs.get(position).getFragment();
}
@Override
public int getCount() {
return mainTabs.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mainTabs.get(position).getName();
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
7.不可滑动的viewpager:BanViewPager
public class BanViewPager extends ViewPager {
private boolean isCanScroll = true;
public BanViewPager(Context context) {
super(context);
}
public BanViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setNoScroll(boolean noScroll) {
this.isCanScroll = noScroll;
}
@Override
public void scrollTo(int x, int y) {
super.scrollTo(x, y);
}
@Override
public boolean onTouchEvent(MotionEvent arg0) {
if (isCanScroll) {
return false;
} else {
return super.onTouchEvent(arg0);
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent arg0) {
if (isCanScroll) {
return false;
} else {
return super.onInterceptTouchEvent(arg0);
}
}
@Override
public void setCurrentItem(int item) {
super.setCurrentItem(item, false);
}
}
8.tab的名字:arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="main_tab">
<item>消息</item>
<item>动态</item>
<item>我的</item>
</string-array>
</resources>
9.最后在MainActivity的使用:
private MainFragmentAdapter mAdapter;
private BanViewPager mVp;
private TabLayout mTabLayout;
private void initTabLayout() {
String[] mName = getResources().getStringArray(R.array.main_tab);
int[] mRes =new int[] {R.drawable.main_tab1,R.drawable.main_tab2, R.drawable.main_tab3};
List<MainTab> mainTabs=new ArrayList<>();
MainTab mainTab1=new MainTab(fragment1,mName[0],mRes[0]);
MainTab mainTab2=new MainTab(fragment1,mName[1],mRes[1]);
MainTab mainTab3=new MainTab(fragment1,mName[2],mRes[2]);
mainTabs.add(mainTab1);
mainTabs.add(mainTab2);
mainTabs.add(mainTab3);
mAdapter=new MainFragmentAdapter(getSupportFragmentManager(),mContext,mainTabs);
mVp.setAdapter(mAdapter);
mTabLayout.setupWithViewPager(mVp);
updateTabLayoutView();
mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
//mVp.setCurrentItem(tab.getPosition());
//或者
updateTabLayoutView();
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
mVp.setCurrentItem(lastIndex);
}
private void updateTabLayoutView(){
for (int i = 0; i < mTabLayout.getTabCount(); i++) {
TabLayout.Tab tab = mTabLayout.getTabAt(i);
if (tab != null) {
View v;
if(tab.getCustomView()==null){
v= mAdapter.getView(i);
}else{
v =tab.getCustomView();
}
if (i == lastIndex) {//默认第i个选中
v.setSelected(true);
}else{
v.setSelected(false);
}
tab.setCustomView(v);
}
}
}
完结撒花!!!!!
网友评论