Can you please tell me how to implement this behavior? Under AppBar has a horizontal menu that allows you to go to the desired category, list location, and it is also automatically selected when scrolling the screenCan you please tell me how to implement this behavior? Under AppBar has a horizontal menu that allows you to go to the desired category, list location, and it is also automatically selected when scrolling the screen.
I looked at the material elements and did not find the necessary data. It’s also unclear how this navigation works in general.
I believe you can accomplish this by utilizing TabLayout and ViewPager/ViewPager2; please consult the official documentation for full guidance.
dependencies {
implementation "androidx.viewpager2:viewpager2:1.0.0"
implementation "com.google.android.material:material:1.5.0-alpha01"
implementation "androidx.fragment:fragment-ktx:1.5.0"
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:tabMode="fixed"
app:tabGravity="fill"/>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@+id/tabLayout"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
class PagerAdapter(activity: FragmentActivity) : FragmentStateAdapter(activity) {
override fun getItemCount(): Int = 3
override fun createFragment(position: Int): Fragment {
return when (position) {
0 -> Fragment1()
1 -> Fragment2()
2 -> Fragment3()
else -> throw IllegalArgumentException("Invalid position")
}
}
}
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import androidx.viewpager2.widget.ViewPager2
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val viewPager: ViewPager2 = findViewById(R.id.viewPager)
val tabLayout: TabLayout = findViewById(R.id.tabLayout)
val adapter = PagerAdapter(this)
viewPager.adapter = adapter
// Connect TabLayout and ViewPager2
TabLayoutMediator(tabLayout, viewPager) { tab, position ->
when (position) {
0 -> tab.text = "Tab 1"
1 -> tab.text = "Tab 2"
2 -> tab.text = "Tab 3"
}
}.attach()
}
}