viewpager2 出来有一段时间了,不过好像还没有正式版。决定尝试一下。

使用要先导入依赖:

implementation 'androidx.viewpager2:viewpager2:1.0.0-rc01'implementation 'com.google.android.material:material:1.2.0-alpha01' // 这个不是必要,但是要结合 tablayout 的使用需要

布局很简洁:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <com.google.android.material.tabs.TabLayout        android:id="@+id/register_tabs"        android:layout_width="match_parent"        android:layout_height="46dp"        app:layout_constraintTop_toTopOf="parent"        app:tabIndicatorColor="#FFD900"        app:tabIndicatorFullWidth="false"        app:tabIndicatorHeight="@dimen/view_dp_2"        app:tabMode="fixed"        app:tabSelectedTextColor="#0C1F34"        app:tabTextColor="#999999">    </com.google.android.material.tabs.TabLayout>    <androidx.viewpager2.widget.ViewPager2        android:id="@+id/register_viewpager"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="horizontal"        app:layout_constraintTop_toBottomOf="@id/register_tabs" /></androidx.constraintlayout.widget.ConstraintLayout>

看起来完全没毛病,运行之后,界面也没有错乱。
不过tablayout 的 item 不能点击,只能使用viewpager 滑动去带动 item 切换。

这个问题搞了几个小时,找不到原因。然后看到官方示例代码不是使用 ConstraintLayout, 而是使用的LinearLayout。就投机替换试试看,结果就正常了,可以点击 item 切换了。

这个坑先放这里,记录一下,不过真正原因还没有找到,只是找到了解决方法。

找到解决方法之后的代码:

  1. 布局文件
<?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"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <com.google.android.material.tabs.TabLayout        android:id="@+id/register_tabs"        android:layout_width="match_parent"        android:layout_height="46dp"        app:layout_constraintTop_toTopOf="parent"        app:tabIndicatorColor="#FFD900"        app:tabIndicatorFullWidth="false"        app:tabIndicatorHeight="@dimen/view_dp_2"        app:tabMode="fixed"        app:tabSelectedTextColor="#0C1F34"        app:tabTextColor="#999999">    </com.google.android.material.tabs.TabLayout>    <androidx.viewpager2.widget.ViewPager2        android:id="@+id/register_viewpager"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"        android:orientation="horizontal"        app:layout_constraintTop_toBottomOf="@id/register_tabs" /></LinearLayout>
  1. java代码
mViewPager2 = findViewById(R.id.register_viewpager);    mTabLayout = findViewById(R.id.register_tabs);mViewPager2.setAdapter(new FragmentStateAdapter(this) {            private int count = 3;            @NonNull            @Override            public Fragment createFragment(int position) {                Fragment fragment;                switch (position) {                    default:                    case 0:                        fragment = new VerifyFragment();                        break;                    case 1:                        fragment = new InfoPerfectionFragment();                        break;                    case 2:                        fragment = new SubmitFragment();                        break;                }                return fragment;            }            @Override            public int getItemCount() {                return count;            }        });        final String[] tabTexts = {"你好", "我是", "静静"};        TabLayoutMediator mediator = new TabLayoutMediator(mTabLayout, mViewPager2,                (tab, position) -> {                    Log.e("tab", "--attach , pos=" + position + " , " + mViewPager2.getCurrentItem());                    tab.setText(tabTexts[position]);                });        mediator.attach();  // 这个 mediator 就是 依赖库 material:material:1.2.0-alpha01 里面的  // 然后这个 attach() 就将 viewpager 和 tablaayout 关联起来了。

关于 tablayout 属性:https://www.jianshu.com/p/88679fed9ecb

补充一个 ,默认 tab 的下划线就是 tab 宽度,加上 app:tabIndicatorFullWidth="false"就能让 下划线只是 tab 内容的宽度。

整体效果如下图:

更多相关文章

  1. 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
  2. 小猪Android越来越方式 Day 5 - part 2
  3. Android仿美团分类下拉菜单实例代码
  4. RadioGroup组与onCheckedChanged事件
  5. Android(安卓)Inflate 的使用
  6. listview中加入listbutton
  7. Android判断网络状态的代码
  8. Android(安卓)keep screen on 方法
  9. 安卓学习笔记之自定义ContentProvider

随机推荐

  1. Android常用Manager
  2. Error:Module production: java.lang.NoC
  3. android 常见错误:fabric connection time
  4. Android 利用Android studio打包jar包
  5. android Activity生命周期详解(图文)
  6. Android之Adapter系列之SimpleAdapter类
  7. Android中WebView控件的使用
  8. android监控来电显示
  9. android 超简单的拖动按钮 悬浮按钮 吸附
  10. 解决 pinyin4j 在Android(安卓)某些编译