在kotlin中,带有Tablayout的ViewPager2在片段中没有显示滑动效果[英] ViewPager2 with Tablayout inside a fragment shows no swipe effect in kotlin

本文是小编为大家收集整理的关于在kotlin中,带有Tablayout的ViewPager2在片段中没有显示滑动效果的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

在我的主activity中,我有一个带有5个标签的TableAut的ViewPager2.那里的滑动正常工作.但是在5个选项卡之一中,我再次具有一个带有3个选项卡的ViewPager2.在内部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>

请有人告诉我我做错了什么?

推荐答案

外视图器正在阻止内视图器接收触摸事件.您需要拦截事件并阻止外视点滑动.请参阅链接用于实施的解决方案代码

本文地址:https://www.itbaoku.cn/post/2091067.html

问题描述

In my MainActivity I have a viewpager2 with Tablayout with 5 tabs. The swipe there is working fine. But in one of the 5 tabs I again have a viewpager2 with a tablayout of 3 tabs. In the inner viewpager2 (which is inside a fragment) the swipe isnt working at all and I don't understand why.

The outer viewpager2 is inside a activity while the inner viewpager2 is in the fragment

Viewpager2 inside fragment , swipe not working :

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

And here is the layout:

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

Please can someone tell me what I'm doing wrong?

推荐答案

The outer Viewpager is preventing the inner viewpager from receiving touch events. You need to intercept the event and stop the outer viewpager from swiping. See the link for implemented solution code