添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

网上搜了很多,都没有找到有关Java的完整解决方案。到处问也没有结果。自己瞎折腾,居然出结果啦!——不是科班出生小白太难了!

首先说,我要想的效果:底部一个导航栏,点击导航栏,可以实现翻页效果。也可以通过左滑、右滑实现翻页效果。

第一步:在gradle里添加支持:

implementation 'androidx.viewpager2:viewpager2:1.0.0'

第二步:MainActivity的XML里

<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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/ViewPager2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        app:itemIconTint="@color/bg_tab"
        app:itemTextColor="@color/bg_tab"
        app:menu="@menu/navigation" />
</LinearLayout>

第三步,color写法,在res里新建一个文件夹,取名color,下面再建一个资源文件bg_tab.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/red" android:state_checked="true" />
    <item android:color="@color/black" android:state_checked="false" />
</selector>

第四步:menu写法,新建下menu文件夹,取名navigation.xml。——icon就随便来了。

<menu xmlns:android="http://schemas.android.com/apk/res/android">
        android:id="@+id/home_navigation"
        android:icon="@drawable/airplane"
        android:orderInCategory="0"
        android:title="@string/sy" />
        android:id="@+id/se"
        android:icon="@drawable/bluetooth"
        android:orderInCategory="1"
        android:title="@string/se" />
</menu>

第五步,新建两个碎片,blankFragment,blankFragment2。

第六步,新建一个Java,取名ViewPager2Adapter。

public class ViewPager2Adapter extends FragmentStateAdapter {
    BlankFragment blankFragment = new BlankFragment();
    BlankFragment2 blankFragment2 = new BlankFragment2();
    public ViewPager2Adapter(@NonNull FragmentActivity fragmentActivity) {
        super(fragmentActivity);
    public int getItemCount() {
        return 2;
    @NonNull
    public Fragment createFragment(int position) {
        switch (position) {
            case 0:
                return blankFragment;
            case 1:
                return blankFragment2;
        return null;

第七步,MainActivity写法:

public class MainActivity extends AppCompatActivity {
    ViewPager2 viewPager2;
    NavigationBarView mNavigationView;
    MenuItem menuItem;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    private void init() {
        ViewPager2Adapter adapter = new ViewPager2Adapter(this);
        viewPager2 = findViewById(R.id.ViewPager2);
        mNavigationView = findViewById(R.id.navigation);
        mNavigationView.setOnItemSelectedListener(mOnItemSelectedListener);
        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int p) {
                menuItem = mNavigationView.getMenu().getItem(p);
                menuItem.setChecked(true);
        viewPager2.setAdapter(adapter);
    private final NavigationBarView.OnItemSelectedListener mOnItemSelectedListener
            = new NavigationBarView.OnItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
             menuItem = item;
             if (item.getItemId() == R.id.home) {
                viewPager2.setCurrentItem(0);
                return true;
            } else if (item.getItemId() == R.id.se) {
                viewPager2.setCurrentItem(1);
                return true;
            return false;

写在最后:

实现多个Fragment滑动翻页,或者通过底部导航栏翻页。并实现Fragment与Fragment之前传值,和Fragment与宿主Activity之间传值。并且解决了viewPager2内存泄漏的问题;提供了FrameLayout布局中控件居中的方案。请移步下载:

https://download.csdn.net/download/u011501017/87693049

FragmentStateManager An Android library that holds fragment states for BottomNavigationView. Saves fragment back stack even after activity rotation. Implementation derived from FragmentStatePagerAdapter. Installation Add Jitpack to project level gradle file allprojects { repositories { maven { url "https://jitpack.io" } Then add library to module level gradle file dependencies { compile 'com.github.okaybroda:FragmentStateManager:1. ' Usage Create a FragmentStateManager in
用到ViewPager和TabLayout时,发现FragmentPagerAdapter()方法过时 class MyAdapter(fm: FragmentManager?, fragmentList: List<Fragment>): FragmentPagerAdapter(fm!!){ ....... 解决办法: class MyAdapter(fm: FragmentManager?, fragmentList: List<Fragment>): 客户端socket>连接服务器socket: java.io.IOException: read failed, socket might closed or timeout, read ret:-1 androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.LinearLayout 客户端socket>连接服务器socket: java.io.IOException: read failed, socket might closed or timeout, read ret:-1 白云LDC: [code=java] private synchronized void connectionFailed() { Message message = handler.obtainMessage(Constant.MESSAGE_TOAST); Bundle bundle = new Bundle(); bundle.putString(Constant.TOAST, "不能连接到此蓝牙设备"); message.setData(bundle); handler.sendMessage(message); BlueUtil.this.start(); [/code]