AndroidX底部导航栏

社会主义新天地 提交于 2020-01-12 11:53:16
    implementation 'me.majiajie:pager-bottom-tab-strip:2.3.0X'

 

布局文件

<?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:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"></androidx.viewpager.widget.ViewPager>

    <me.majiajie.pagerbottomtabstrip.PageNavigationView
        android:id="@+id/tab"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_alignParentBottom="true"
        android:background="#FFF"
        android:elevation="8dp" />

</LinearLayout>

viewpager的adapter

package com.iallchain.xumu.Adapter;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.iallchain.xumu.Fragment.XmHomeFragment;
import com.iallchain.xumu.Fragment.XmMyFragment;

public class MyViewPagerAdapter extends FragmentPagerAdapter {

    private int size;

    public MyViewPagerAdapter(FragmentManager fm, int size) {
        super(fm);
        this.size = size;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new XmHomeFragment();
            case 1:
                return new XmMyFragment();
            default:
                return new XmHomeFragment();
        }
    }

    @Override
    public int getCount() {
        return size;
    }
}

主页面代码

package com.iallchain.xumu

import android.graphics.Color
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.iallchain.xumu.Adapter.MyViewPagerAdapter
import kotlinx.android.synthetic.main.activity_main.*
import me.majiajie.pagerbottomtabstrip.item.BaseTabItem
import me.majiajie.pagerbottomtabstrip.item.NormalItemView


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initTabUI()
    }


    /**
     * 初始化页面布局
     */
    private fun initTabUI() {
        val navigationController = tab.custom()
            .addItem(newItem(R.drawable.icon_home_default, R.drawable.icon_home, "首页"))
            .addItem(newItem(R.drawable.icon_my_default, R.drawable.icon_my, "我的"))
            .build()
        viewPager.adapter = MyViewPagerAdapter(
            supportFragmentManager,
            navigationController.itemCount
        )
        //自动适配ViewPager页面切换
        navigationController.setupWithViewPager(viewPager);

        //设置消息数
//        navigationController.setMessageNumber(1, 8);

        //设置显示小圆点
//        navigationController.setHasMessage(0, true);
    }

    //创建一个Item
    private fun newItem(
        drawable: Int,
        checkedDrawable: Int,
        text: String
    ): BaseTabItem? {
        val normalItemView = NormalItemView(this)
        normalItemView.initialize(drawable, checkedDrawable, text)
        normalItemView.setTextDefaultColor(Color.GRAY)
        normalItemView.setTextCheckedColor(-0xff6978)
        return normalItemView
    }
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!