TabLayout+ViewPaper+Fragment底部导航

在这里插入图片描述
先把依赖导入 implementation 'com.google.android.material:material:1.1.0'
布局先上activity_main

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

   <androidx.viewpager.widget.ViewPager
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_weight="1"
       android:id="@+id/viewpaper"/>
    <com.google.android.material.tabs.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tablayout"
        app:tabSelectedTextColor="@color/blue"
        app:tabTextColor="#1C1C1C"
        app:tabMode="fixed"
        app:tabGravity="fill"
        app:tabIndicatorHeight="0dp"
        app:tabRippleColor="@android:color/transparent" />
        <!--tabRippleColor去除点击时的阴影效果-->

</LinearLayout>

导航的子布局main_tab_item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingTop="10dp"
    android:gravity="center_horizontal">
    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:id="@+id/img"
        android:src="@mipmap/ic_launcher"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="首页"
        android:paddingTop="5dp"
        android:gravity="center"
        android:id="@+id/tv"/>


</LinearLayout>

准备5个fragment,这里只写一个,其余的换类名及生成布局就可以了

public class HomeFragment extends BaseFragment{
    @Override
    protected void initData() {

    }

    @Override
    protected View initView() {
        View view = View.inflate(getActivity(), R.layout.fragment_home, null);

        return view;
    }
}

fragment_home

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="首页"
        android:textColor="@color/blue"
        android:textSize="60sp"
        android:textStyle="bold"
        android:gravity="center"
        android:id="@+id/tv"/>

</LinearLayout>

MainActivity

public class MainActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private TabLayout tablayout;
    private List<Fragment> fragmentList;
    private String[] titles = {"首页", "服务", "发布", "新闻", "中心"};
    private int[] unSele = {R.mipmap.main_home, R.mipmap.main_type, R.mipmap.main_cart, R.mipmap.main_community, R.mipmap.main_user};
    private int[] onSele = {R.mipmap.main_home_press, R.mipmap.main_type_press, R.mipmap.main_cart_press, R.mipmap.main_community_press, R.mipmap.main_user_press};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = findViewById(R.id.viewpaper);
        tablayout = findViewById(R.id.tablayout);
        initData();
    }

    private void initData() {
        fragmentList = new ArrayList<>();
        fragmentList.add(new HomeFragment());
        fragmentList.add(new ServiceFragment());
        fragmentList.add(new PostFragment());
        fragmentList.add(new NewsFragment());
        fragmentList.add(new UserFragment());

        MainTabAdapter mainTabAdapter = new MainTabAdapter(getSupportFragmentManager());
        viewPager.setAdapter(mainTabAdapter);
        tablayout.setupWithViewPager(viewPager);

        for (int i = 0; i < tablayout.getTabCount(); i++) {
            TabLayout.Tab tabAt = tablayout.getTabAt(i);
            tabAt.setCustomView(mainTabAdapter.getView(i));
        }

        tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                ImageView img = view.findViewById(R.id.img);
                TextView tv = view.findViewById(R.id.tv);
                String title = tv.getText().toString();
                if (title == "首页") {
                    img.setImageResource(onSele[0]);
                } else if (title == "服务") {
                    img.setImageResource(onSele[1]);
                }else if (title == "发布") {
                    img.setImageResource(onSele[2]);
                }else if (title == "新闻") {
                    img.setImageResource(onSele[3]);
                }else if (title == "中心") {
                    img.setImageResource(onSele[4]);
                }


            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                ImageView img = view.findViewById(R.id.img);
                TextView tv = view.findViewById(R.id.tv);
                String title = tv.getText().toString();
                if (title=="首页"){
                    img.setImageResource(unSele[0]);
                } else if (title=="服务") {
                    img.setImageResource(unSele[1]);
                } else if (title=="发布") {
                    img.setImageResource(unSele[2]);
                } else if (title=="新闻") {
                    img.setImageResource(unSele[3]);
                } else if (title=="中心") {
                    img.setImageResource(unSele[4]);
                }

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


    }

    public class MainTabAdapter extends FragmentPagerAdapter {

        public MainTabAdapter(@NonNull FragmentManager fm) {
            super(fm);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            if (position == 0) {
                return fragmentList.get(0);
            } else if (position == 1) {
                return fragmentList.get(1);
            } else if (position == 2) {
                return fragmentList.get(2);
            } else if (position == 3) {
                return fragmentList.get(3);
            } else if (position == 4) {
                return fragmentList.get(4);
            }
            return fragmentList.get(0);
        }

        @Override
        public int getCount() {
            return titles.length;
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }

        public View getView(int position) {
            View inflate = View.inflate(MainActivity.this, R.layout.main_tab_item, null);
            ImageView img = inflate.findViewById(R.id.img);
            TextView tv = inflate.findViewById(R.id.tv);
            if (tablayout.getTabAt(position).isSelected()) {
                img.setImageResource(onSele[position]);
            } else {
                img.setImageResource(unSele[position]);
            }
            tv.setText(titles[position]);
            tv.setTextColor(tablayout.getTabTextColors());
            return inflate;
        }
    }
}

请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值