经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
AndroidX下滑动ViewPager时TabLayout滑动间隔不对
来源:cnblogs  作者:xing_star  时间:2020/11/9 15:14:34  对本文有异议

  最近在写新App,那么新App中使用的就是AndroidX那一套玩意了,然后尝试了个首页功能,Androidx viewPager + tabLayout。 结果就这点小功能就折腾了几个小时。写的过程中,中间也是踩了不少的坑,下面听我详细描述吧。

  找了几篇博客,看了下AndroidX下的代码写法,抄到了xml布局文件中, 布局如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical">
  7.  
  8. <com.google.android.material.tabs.TabLayout
  9. android:id="@+id/tab_layout"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. app:tabBackground="@android:color/transparent"
  13. app:tabGravity="fill"
  14. app:tabIndicatorColor="@color/colorAccent"
  15. app:tabIndicatorFullWidth="true"
  16. app:tabMaxWidth="0dp"
  17. app:tabMode="fixed"
  18. app:tabRippleColor="@android:color/transparent"
  19. app:tabSelectedTextColor="@color/colorPrimary"
  20. app:tabTextColor="@color/colorPrimary" />
  21.  
  22. <View
  23. android:layout_width="match_parent"
  24. android:layout_height="0.5dp"
  25. android:background="#E4E4E4" />
  26.  
  27. <androidx.viewpager.widget.ViewPager
  28. android:id="@+id/viewPager"
  29. android:layout_width="match_parent"
  30. android:layout_height="match_parent">
  31.  
  32. </androidx.viewpager.widget.ViewPager>
  33.  
  34. </LinearLayout>

就是这样很常规的布局,然后写好了viewpager的adapter,以及代码调用等

  1. imageFragment = ImageFragment.newInstance();
  2. fragmentList.add(imageFragment);
  3. videoFragment = VideoFragment.newInstance();
  4. fragmentList.add(videoFragment);
  5. mainAdapter = new MainAdapter(getSupportFragmentManager());
  6. viewPager.setAdapter(mainAdapter);
  7. tabLayout.setupWithViewPager(viewPager);

运行的结果,点击tabLayout,切换效果一切ok,但是呢,滑动ViewPager切换,就出现了问题,tabLayout滑动的效果不对,我只有两个title,期望的是滑动ViewPager,对应的tabItem切到另外一个,而不是在中间阶段停住,网上搜索了好久,也没找到答案。运行了几个开源项目写的sample,发现也如此,所以懵逼了。最后只好祭出了大招,看源码。发现ViewPager的onPageScrolled的方法里面,有个变量很关键,mDecorChildCount,用来控制viewPager子View的滑动,瞬间就明白了,找到了问题所在,改了布局,运行下,效果完美。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical">
  7.  
  8. <androidx.viewpager.widget.ViewPager
  9. android:id="@+id/viewPager"
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent">
  12.  
  13. <com.google.android.material.tabs.TabLayout
  14. android:id="@+id/tab_layout"
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content"
  17. app:tabBackground="@android:color/transparent"
  18. app:tabGravity="fill"
  19. app:tabIndicatorColor="@color/colorAccent"
  20. app:tabIndicatorFullWidth="true"
  21. app:tabMaxWidth="0dp"
  22. app:tabMode="fixed"
  23. app:tabRippleColor="@android:color/transparent"
  24. app:tabSelectedTextColor="@color/colorPrimary"
  25. app:tabTextColor="@color/colorPrimary" />
  26.  
  27. <View
  28. android:layout_width="match_parent"
  29. android:layout_height="0.5dp"
  30. android:background="#E4E4E4" />
  31.  
  32.  
  33. </androidx.viewpager.widget.ViewPager>
  34.  
  35. </LinearLayout>

将tabLayout放到ViewPager里面即可。

 

原文链接:http://www.cnblogs.com/xing-star/p/13873595.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号