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

查看:133
本文介绍了片段内具有Tablayout的ViewPager2在kotlin中没有滑动效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的MainActivity中,我有一个带有5个标签的Tablayout的viewpager2.那里的滑动工作正常.但是在5个选项卡之一中,我又有了一个viewpager2,它的布局由3个选项卡组成.在内部viewpager2(位于片段内)中,滑动根本不起作用,我不明白为什么.

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.

外部viewpager2在活动中,而内部viewpager2在片段中

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

片段内的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>

请有人能告诉我我在做什么错吗?

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

推荐答案

外部Viewpager阻止内部Viewpager接收触摸事件.您需要拦截该事件并阻止外部viewpager滑动.请参阅链接以获取已实现的解决方案代码

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

这篇关于片段内具有Tablayout的ViewPager2在kotlin中没有滑动效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆