在我的MainActivity中,我有一个带有5个标签的Tablayout的viewpager2。那里的滑动工作正常。但是在5个选项卡之一中,我又有了一个viewpager2,它的布局由3个选项卡组成。在内部viewpager2(位于片段内)中,滑动根本不起作用,我不明白为什么。
外部viewpager2在活动中,而内部viewpager2在片段中
片段内的Viewpager2,无法滑动:
class FreelancerMyProjectsFragment : Fragment() {
lateinit var binding : FragmentFreelancerMyProjectsBinding
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
binding = DataBindingUtil.inflate(inflater,R.layout.fragment_freelancer_my_projects, container, false)
setUpViewPager()
addTabs()
return binding.root
}
private fun addTabs() {
TabLayoutMediator(binding.projectsTabLayout, binding.projectsViewPager) { tab, position ->
}.attach()
binding.projectsTabLayout.getTabAt(0)?.text =resources.getText(R.string.active)
binding.projectsTabLayout.getTabAt(1)?.text =resources.getText(R.string.bids)
binding.projectsTabLayout.getTabAt(2)?.text =resources.getText(R.string.past)
}
private fun setUpViewPager() {
val adapter = ScreenSlidePagerAdapterNewForFragment(this)
adapter.addFragment(ActiveProjectsFragment())
adapter.addFragment(BidsFragment())
adapter.addFragment(PastProjectFragment())
binding.projectsViewPager.offscreenPageLimit = 3
binding.projectsViewPager.adapter = adapter
}
}
class ScreenSlidePagerAdapterNewForFragment(fragment: Fragment) : FragmentStateAdapter(fragment) {
var fragmentList: ArrayList<Fragment> = ArrayList()
public fun addFragment(fragment: Fragment) {
fragmentList.add(fragment)
}
override fun getItemCount(): Int{ return fragmentList.size}
override fun createFragment(position: Int): Fragment {return fragmentList[position]}
}
这是布局:
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/projectsViewPager"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@+id/projectsTabLayout"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
</androidx.viewpager2.widget.ViewPager2>
<com.google.android.material.tabs.TabLayout
android:id="@+id/projectsTabLayout"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginHorizontal="40dp"
app:tabTextAppearance="@style/ProjectTabLayoutFreelancer"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
</com.google.android.material.tabs.TabLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
请有人能告诉我我在做什么错吗?