一、Toolbar + TabLayout + Viewpager2

效果图:
iShot2020-08-1923.23.21.gif
xml 文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. xmlns:app="http://schemas.android.com/apk/res-auto">
  6. <com.google.android.material.appbar.AppBarLayout
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. >
  10. <androidx.appcompat.widget.Toolbar
  11. android:id="@+id/tb_tabvp_tb"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. app:layout_scrollFlags="scroll|enterAlways|snap"
  15. app:title="测试tablayout相关"
  16. app:titleTextColor="@color/white"
  17. app:navigationIcon="@mipmap/ic_navigation_back_white">
  18. </androidx.appcompat.widget.Toolbar>
  19. <com.google.android.material.tabs.TabLayout
  20. android:id="@+id/tl_tabvp_tab"
  21. android:layout_width="match_parent"
  22. android:layout_height="45dp"
  23. app:tabGravity="fill"
  24. android:background="@color/white"/>
  25. </com.google.android.material.appbar.AppBarLayout>
  26. <androidx.viewpager2.widget.ViewPager2
  27. android:id="@+id/vp_tabvp_vp"
  28. android:layout_width="match_parent"
  29. android:layout_height="match_parent"
  30. app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"/>
  31. </androidx.coordinatorlayout.widget.CoordinatorLayout>

activity:

  1. class ToolbarTabVpActivity: AppCompatActivity() {
  2. val tabs = arrayOf("关注", "推荐", "最新")
  3. override fun onCreate(savedInstanceState: Bundle?) {
  4. super.onCreate(savedInstanceState)
  5. setContentView(R.layout.activity_tabvp)
  6. tb_tabvp_tb.setNavigationOnClickListener {
  7. finish()
  8. }
  9. val fragments = arrayListOf<Fragment>()
  10. for ( i in 0..2) {
  11. val instance = TabFragment.instance(i)
  12. fragments.add(instance)
  13. }
  14. //fragments.add(TabFragment.instance(0))
  15. val adapter = TabAdapter(this,fragments)
  16. vp_tabvp_vp.adapter = adapter
  17. //将TabLayout和ViewPager2关联起来
  18. val metiaor: TabLayoutMediator = TabLayoutMediator(tl_tabvp_tab,vp_tabvp_vp,
  19. TabLayoutMediator.TabConfigurationStrategy { tab, position ->
  20. tab.text = tabs[position]
  21. })
  22. metiaor.attach()
  23. //tablayout change
  24. tl_tabvp_tab.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener{
  25. override fun onTabReselected(tab: TabLayout.Tab?) {
  26. }
  27. override fun onTabUnselected(tab: TabLayout.Tab?) {
  28. }
  29. override fun onTabSelected(tab: TabLayout.Tab?) {
  30. }
  31. })
  32. }
  33. }

fragment:

  1. class TabFragment : Fragment() {
  2. companion object{
  3. fun instance(type: Int): TabFragment {
  4. val bundle = Bundle()
  5. bundle.putInt("type",type)
  6. val viewPagerFragment = TabFragment()
  7. viewPagerFragment.arguments = bundle
  8. return viewPagerFragment
  9. }
  10. }
  11. override fun onCreateView(
  12. inflater: LayoutInflater,
  13. container: ViewGroup?,
  14. savedInstanceState: Bundle?
  15. ): View? {
  16. return inflater.inflate(R.layout.fg_tabvp,container, false)
  17. }
  18. override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  19. super.onViewCreated(view, savedInstanceState)
  20. val type = arguments?.getInt("type")
  21. LogUtils.e("--------$type")
  22. }
  23. }

fragment 简单布局:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.core.widget.NestedScrollView
  3. xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <TextView
  6. android:layout_width="match_parent"
  7. android:layout_height="wrap_content"
  8. android:text="@string/large_text"
  9. android:layout_margin="@dimen/m10"/>
  10. </androidx.core.widget.NestedScrollView>

adapter:

  1. class TabAdapter(fragmentActivity: FragmentActivity,
  2. private val fragments: List<Fragment>): FragmentStateAdapter(fragmentActivity) {
  3. override fun getItemCount(): Int = fragments.size
  4. override fun createFragment(position: Int): Fragment = fragments[position]
  5. }

二、CollaspingToolbarLayout + TabLayout + Viewpager2

效果图:
iShot2020-08-1923.50.18.gif
布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. xmlns:app="http://schemas.android.com/apk/res-auto">
  6. <com.google.android.material.appbar.AppBarLayout
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content">
  9. <com.google.android.material.appbar.CollapsingToolbarLayout
  10. android:id="@+id/ctl_collasping_tab_collapsing"
  11. android:layout_width="match_parent"
  12. android:layout_height="220dp"
  13. app:contentScrim="@color/blue_74D3FF"
  14. app:title="测试标题"
  15. app:layout_scrollFlags="scroll|exitUntilCollapsed">
  16. <ImageView
  17. android:layout_width="match_parent"
  18. android:layout_height="match_parent"
  19. android:fitsSystemWindows="true"
  20. android:scaleType="centerCrop"
  21. android:src="@mipmap/wuhuang"
  22. app:layout_collapseMode="parallax" />
  23. <androidx.appcompat.widget.Toolbar
  24. android:id="@+id/tb_collasping_tab_tb"
  25. android:layout_width="match_parent"
  26. android:layout_height="?attr/actionBarSize"
  27. app:layout_collapseMode="pin"
  28. app:navigationIcon="@mipmap/ic_navigation_back_white"
  29. app:titleTextColor="@color/white"
  30. />
  31. </com.google.android.material.appbar.CollapsingToolbarLayout>
  32. <com.google.android.material.tabs.TabLayout
  33. android:id="@+id/tl_collasping_tab_tab"
  34. android:layout_width="match_parent"
  35. android:layout_height="45dp"
  36. android:background="@color/white"/>
  37. </com.google.android.material.appbar.AppBarLayout>
  38. <androidx.viewpager2.widget.ViewPager2
  39. android:id="@+id/vp_collasping_tab_vp"
  40. android:layout_width="match_parent"
  41. android:layout_height="match_parent"
  42. app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"/>
  43. </androidx.coordinatorlayout.widget.CoordinatorLayout>

activity:

  1. class CollaspingTabActivity: AppCompatActivity() {
  2. val tabs = arrayOf("关注", "推荐", "最新")
  3. override fun onCreate(savedInstanceState: Bundle?) {
  4. super.onCreate(savedInstanceState)
  5. setContentView(R.layout.activity_collasping_tab)
  6. tb_collasping_tab_tb.setNavigationOnClickListener { finish() }
  7. val fragments = arrayListOf<Fragment>()
  8. for ( i in 0..2) {
  9. val instance = TabFragment.instance(i)
  10. fragments.add(instance)
  11. }
  12. //fragments.add(TabFragment.instance(0))
  13. val adapter = TabAdapter(this,fragments)
  14. vp_collasping_tab_vp.adapter = adapter
  15. //将TabLayout和ViewPager2关联起来
  16. val metiaor: TabLayoutMediator = TabLayoutMediator(tl_collasping_tab_tab,vp_collasping_tab_vp,
  17. TabLayoutMediator.TabConfigurationStrategy { tab, position ->
  18. tab.text = tabs[position]
  19. })
  20. metiaor.attach()
  21. //设置标题相关
  22. ctl_collasping_tab_collapsing.run {
  23. setExpandedTitleColor(ContextCompat.getColor(this@CollaspingTabActivity,R.color.transparent))
  24. setCollapsedTitleTextColor(ContextCompat.getColor(this@CollaspingTabActivity,R.color.white))
  25. }
  26. }
  27. }

fragment, adapter 和上面都一样

三、地址和参考

demo