一、Toolbar + TabLayout + Viewpager2
效果图:
xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<androidx.appcompat.widget.Toolbar
android:id="@+id/tb_tabvp_tb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways|snap"
app:title="测试tablayout相关"
app:titleTextColor="@color/white"
app:navigationIcon="@mipmap/ic_navigation_back_white">
</androidx.appcompat.widget.Toolbar>
<com.google.android.material.tabs.TabLayout
android:id="@+id/tl_tabvp_tab"
android:layout_width="match_parent"
android:layout_height="45dp"
app:tabGravity="fill"
android:background="@color/white"/>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/vp_tabvp_vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
activity:
class ToolbarTabVpActivity: AppCompatActivity() {
val tabs = arrayOf("关注", "推荐", "最新")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_tabvp)
tb_tabvp_tb.setNavigationOnClickListener {
finish()
}
val fragments = arrayListOf<Fragment>()
for ( i in 0..2) {
val instance = TabFragment.instance(i)
fragments.add(instance)
}
//fragments.add(TabFragment.instance(0))
val adapter = TabAdapter(this,fragments)
vp_tabvp_vp.adapter = adapter
//将TabLayout和ViewPager2关联起来
val metiaor: TabLayoutMediator = TabLayoutMediator(tl_tabvp_tab,vp_tabvp_vp,
TabLayoutMediator.TabConfigurationStrategy { tab, position ->
tab.text = tabs[position]
})
metiaor.attach()
//tablayout change
tl_tabvp_tab.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener{
override fun onTabReselected(tab: TabLayout.Tab?) {
}
override fun onTabUnselected(tab: TabLayout.Tab?) {
}
override fun onTabSelected(tab: TabLayout.Tab?) {
}
})
}
}
fragment:
class TabFragment : Fragment() {
companion object{
fun instance(type: Int): TabFragment {
val bundle = Bundle()
bundle.putInt("type",type)
val viewPagerFragment = TabFragment()
viewPagerFragment.arguments = bundle
return viewPagerFragment
}
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fg_tabvp,container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val type = arguments?.getInt("type")
LogUtils.e("--------$type")
}
}
fragment 简单布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/large_text"
android:layout_margin="@dimen/m10"/>
</androidx.core.widget.NestedScrollView>
adapter:
class TabAdapter(fragmentActivity: FragmentActivity,
private val fragments: List<Fragment>): FragmentStateAdapter(fragmentActivity) {
override fun getItemCount(): Int = fragments.size
override fun createFragment(position: Int): Fragment = fragments[position]
}
二、CollaspingToolbarLayout + TabLayout + Viewpager2
效果图:
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/ctl_collasping_tab_collapsing"
android:layout_width="match_parent"
android:layout_height="220dp"
app:contentScrim="@color/blue_74D3FF"
app:title="测试标题"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
android:src="@mipmap/wuhuang"
app:layout_collapseMode="parallax" />
<androidx.appcompat.widget.Toolbar
android:id="@+id/tb_collasping_tab_tb"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:navigationIcon="@mipmap/ic_navigation_back_white"
app:titleTextColor="@color/white"
/>
</com.google.android.material.appbar.CollapsingToolbarLayout>
<com.google.android.material.tabs.TabLayout
android:id="@+id/tl_collasping_tab_tab"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="@color/white"/>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/vp_collasping_tab_vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
activity:
class CollaspingTabActivity: AppCompatActivity() {
val tabs = arrayOf("关注", "推荐", "最新")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_collasping_tab)
tb_collasping_tab_tb.setNavigationOnClickListener { finish() }
val fragments = arrayListOf<Fragment>()
for ( i in 0..2) {
val instance = TabFragment.instance(i)
fragments.add(instance)
}
//fragments.add(TabFragment.instance(0))
val adapter = TabAdapter(this,fragments)
vp_collasping_tab_vp.adapter = adapter
//将TabLayout和ViewPager2关联起来
val metiaor: TabLayoutMediator = TabLayoutMediator(tl_collasping_tab_tab,vp_collasping_tab_vp,
TabLayoutMediator.TabConfigurationStrategy { tab, position ->
tab.text = tabs[position]
})
metiaor.attach()
//设置标题相关
ctl_collasping_tab_collapsing.run {
setExpandedTitleColor(ContextCompat.getColor(this@CollaspingTabActivity,R.color.transparent))
setCollapsedTitleTextColor(ContextCompat.getColor(this@CollaspingTabActivity,R.color.white))
}
}
}