So, I am working on my android project and I am developing UI for my community page (which is a fragment). I am using Tablayout and viewpager. I have done everything and set up the adapter as well but when I run the app the tabs are not visible.
碎片_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>
我的社区PagerAdapter:
// 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()
}
}
My 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
}
}
没有任何错误,而且该笔钱正在罚款,但可以看上去。