简体   繁体   中英

Tabbed Activity fragment to Activity

I am Yusuf, my English is bad so I write this article with translate. My question is: Can I switch to another fragment or activity with Tabbed activity in Android Studio?

You can use TabLayout with ViewPager2 setup and FragmentPagerAdapter to bind fragment as child. I can help you further if required.

Layout

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:fitsSystemWindows="true"
        android:theme="@style/AppBarOverlay">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="256dp"
            android:fitsSystemWindows="true"
            auto:contentScrim="?attr/colorPrimary"
            auto:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
            auto:titleEnabled="false"
            tools:ignore="NewApi">

            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/backdrop"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                auto:layout_collapseMode="parallax" />

            <View
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:alpha="0.3"
                android:background="@android:color/black"
                android:fitsSystemWindows="true"
                android:visibility="gone" />

            <com.google.android.material.appbar.MaterialToolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:layout_gravity="top"
                android:layout_marginBottom="48dp"
                auto:layout_collapseMode="pin"
                auto:popupTheme="@style/PopupOverlay" />

            <com.google.android.material.tabs.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:background="?attr/colorPrimary"
                auto:tabGravity="fill"
                auto:tabIndicatorColor="@android:color/white"
                auto:tabMode="scrollable"
                auto:tabSelectedTextColor="@android:color/white"
                auto:tabTextColor="@color/material_white" />

        </com.google.android.material.appbar.CollapsingToolbarLayout>

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        auto:layout_behavior="@string/appbar_scrolling_view_behavior" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Adapter abstract class BasePagerAdapter(activity: AppCompatActivity): FragmentStateAdapter(activity) {

protected val items: ArrayList<T>

init {
    items = ArrayList()
}

override fun getItemCount(): Int {
    return items.size
}

fun addItem(item: T) {
    items.add(item)
}

fun getItem(position: Int) : T? = items.get(position)

}

class StationPagerAdapter(activity: AppCompatActivity): BasePagerAdapter(activity) {

init {
    addItems()
}

override fun createFragment(position: Int): Fragment {
    return items.get(position)
}

}

Setup in Activity

adapter = StationPagerAdapter(this)
        bind.pager.adapter = adapter
        TabLayoutMediator(
            bind.tabs,
            bind.pager,
            TabLayoutMediator.TabConfigurationStrategy { tab, position ->
                tab.text = adapter.getTitle(position)
            }).attach()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM