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
}
}
老一本 发布了75 篇原创文章 · 获赞 9 · 访问量 1万+ 私信 关注