【问题标题】:Tabbed Activity fragment to Activity选项卡式活动片段到活动
【发布时间】:2020-04-22 17:46:42
【问题描述】:

我是 Yusuf,我的英语不好,所以我用 translate 来写这篇文章。我的问题是:我可以在 Android Studio 中使用 Tabbed Activity 切换到另一个片段或 Activity 吗?

【问题讨论】:

    标签: android android-studio android-layout android-fragments


    【解决方案1】:

    您可以使用 TabLayout 与 ViewPager2 设置和 FragmentPagerAdapter 将片段绑定为子级。如果需要,我可以进一步帮助您。

    布局

    <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>
    

    适配器 抽象类 BasePagerAdapter(活动:AppCompatActivity): 片段状态适配器(活动){

    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)
    

    }

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

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

    }

    活动中的设置

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

    【讨论】:

    • 首先谢谢你。你能帮我多一点吗
    • 当然。我正在用代码库更新上面的答案。
    • @HasanyusufBesli 请看一下上面的答案。请随时进一步询问。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多