为什么在使用 Tablayout 和 viewpager 时选项卡在片段中不可见?

问题描述 投票:0回答:1

所以,我正在开发我的 android 项目,并且正在为我的社区页面(这是一个片段)开发 UI。我正在使用 Tablayout 和 viewpager。我已经完成了所有操作并设置了适配器,但是当我运行应用程序时,选项卡不可见。

fragment_bite_back_community.xml:

<RelativeLayout 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=".fragment.BiteBackCommunityFragment">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/communityTabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:tabGravity="fill"
        app:tabMode="fixed">

        <!-- Add TabItems for each tab -->
        <com.google.android.material.tabs.TabItem
            android:id="@+id/tabFeed"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/feed" />

        <com.google.android.material.tabs.TabItem
            android:id="@+id/tabExplore"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/explore" />

        <com.google.android.material.tabs.TabItem
            android:id="@+id/tabCreatePost"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/create_post" />
    </com.google.android.material.tabs.TabLayout>

    <!-- ViewPager for managing fragments in tabs -->
    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/communityTabLayout" />

</RelativeLayout>

我的 CommunityPagerAdapter:

// CommunityPagerAdapter

package com.saurtech.biteback.adapter

import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.viewpager.widget.PagerAdapter
import com.saurtech.biteback.fragment.CommunityCreatePostFragment
import com.saurtech.biteback.fragment.CommunityExploreFragment
import com.saurtech.biteback.fragment.CommunityFeedFragment

class CommunityPagerAdapter(private val fm: FragmentManager) : PagerAdapter() {

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        // Return the fragment based on the position
        val fragment: Fragment = when (position) {
            0 -> CommunityFeedFragment()
            1 -> CommunityExploreFragment()
            2 -> CommunityCreatePostFragment()
            else -> throw IllegalArgumentException("Invalid tab position")
        }

        val transaction = fm.beginTransaction()
        transaction.add(container.id, fragment)
        transaction.commitAllowingStateLoss()

        return fragment
    }

    override fun getCount(): Int {
        // Return the total number of tabs
        return 3
    }

    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view == `object`
    }

    override fun getPageTitle(position: Int): CharSequence? {
        // Return tab title based on position
        return when (position) {
            0 -> "Community Feed"
            1 -> "Explore"
            2 -> "Create Post"
            else -> null
        }
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        val transaction = fm.beginTransaction()
        transaction.remove(`object` as Fragment)
        transaction.commitAllowingStateLoss()
    }
}

我的 BiteBackCommunityFragment.kt:

package com.saurtech.biteback.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import com.google.android.material.tabs.TabLayout
import com.saurtech.biteback.R
import com.saurtech.biteback.adapter.CommunityPagerAdapter

class BiteBackCommunityFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_bite_back_community, container, false)

        // Setup ViewPager
        val viewPager: ViewPager = view.findViewById(R.id.viewPager)

        val pagerAdapter: PagerAdapter = CommunityPagerAdapter(childFragmentManager)
        viewPager.adapter = pagerAdapter

        // Setup TabLayout
        val tabLayout: TabLayout = view.findViewById(R.id.communityTabLayout)
        tabLayout.setupWithViewPager(viewPager)

        return view
    }
}

没有错误,应用程序运行正常,但看不到选项卡。

这是我得到的屏幕。

这是所需的输出。

user-interface android-viewpager android-tablayout
1个回答
0
投票

所以,我尝试了一切可能的方法来完成这项工作,最终得到了正确的观点。

我只是做了一些调整,例如将 TabLayout 放置在顶部,并在 TabLayout 下方使用了 ViewPager2。

fragment_bite_back_community.xml

<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=".fragment.BiteBackCommunityFragment">
    
    <com.google.android.material.tabs.TabLayout
        android:id="@+id/communityTabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:tabGravity="fill"
        app:tabMode="fixed">
        
        <com.google.android.material.tabs.TabItem
            android:id="@+id/tabFeed"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/feed" />

        <com.google.android.material.tabs.TabItem
            android:id="@+id/tabExplore"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/explore" />

        <com.google.android.material.tabs.TabItem
            android:id="@+id/tabCreatePost"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/create_post" />
    </com.google.android.material.tabs.TabLayout>
    
    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/communityTabLayout" />

</androidx.constraintlayout.widget.ConstraintLayout>

更改了 CommunityPagerAdapter.kt 中的一些内容,现在看起来像这样:

// CommunityPagerAdapter
package com.saurtech.biteback.adapter

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.saurtech.biteback.fragment.CommunityCreatePostFragment
import com.saurtech.biteback.fragment.CommunityExploreFragment
import com.saurtech.biteback.fragment.CommunityFeedFragment

class CommunityPagerAdapter(fragmentManager: FragmentManager, lifecycle: Lifecycle) :
    FragmentStateAdapter(fragmentManager, lifecycle) {

    override fun getItemCount(): Int {
        return 3 // Return the total number of tabs
    }

    override fun createFragment(position: Int): Fragment {
        // Return the fragment based on the position
        return when (position) {
            0 -> CommunityFeedFragment()
            1 -> CommunityExploreFragment()
            2 -> CommunityCreatePostFragment()
            else -> throw IllegalArgumentException("Invalid tab position")
        }
    }
}

最后一个BiteBackCommunityFragment.kt文件:

package com.saurtech.biteback.fragment

// BiteBackCommunityFragment.kt
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.saurtech.biteback.R
import com.saurtech.biteback.adapter.CommunityPagerAdapter

class BiteBackCommunityFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_bite_back_community, container, false)

        // Setup ViewPager
        val viewPager: ViewPager2 = view.findViewById(R.id.viewPager)
        val pagerAdapter = CommunityPagerAdapter(childFragmentManager, lifecycle)
        viewPager.adapter = pagerAdapter

        // Setup TabLayout using TabLayoutMediator
        val tabLayout: TabLayout = view.findViewById(R.id.communityTabLayout)
        TabLayoutMediator(tabLayout, viewPager) { tab, position ->
            when (position) {
                0 -> tab.text = "Feed"
                1 -> tab.text = "Explore"
                2 -> tab.text = "Create Post"
            }
        }.attach()

        return view
    }
}

之前我试图将 TabLayout 放在底部,但它不起作用,所以这次将它放在顶部,现在它工作得很好。

© www.soinside.com 2019 - 2024. All rights reserved.