Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

android - How to prevent viewpager2 from turning page when a tab is selected

I have a viewpager2 tied to a tablayout with many different fragments holding textviews, i don't want the user to select another tab if he hasn't completed some of the textviews on each fragment. So when a user select another tab i would show a message asking to complete the current fragment data. I tried to select the old tab when a user select another tab in the "onTabUnselected" method of the TabSelectedListener but without success. Here is the code, please look at the TODO comment where i would like to prevent fragment changing.

viewPager = view.findViewById(R.id.mypager)

TabLayoutMediator(tabLayout, viewPager, true, false,
    TabLayoutMediator.TabConfigurationStrategy { tab: TabLayout.Tab, position: Int ->
        tab.text = titles[position]
    }).attach()

tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
    override fun onTabSelected(tab: TabLayout.Tab?) {

    }

    override fun onTabUnselected(tab: TabLayout.Tab?) {
        val currFragment= fragmentSet[tab!!.position]
        if (!currFragment.validateFields()) {
            //TODO: don't change tab, remain in this tab something like tab.aborselectedtTransition
        }
    }

    override fun onTabReselected(tab: TabLayout.Tab?) {
    }

})

Can anybody help me please?

question from:https://stackoverflow.com/questions/65925334/how-to-prevent-viewpager2-from-turning-page-when-a-tab-is-selected

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I solved on my own, instead of using onTabUnselected it's better to use a listener on onTouch event, this way you can prevent viewpager to change page just returning true from onTouchListener. Please vote me if you like it.

viewPager = view.findViewById(R.id.mypager)

TabLayoutMediator(tabLayout, viewPager, true, false,
    TabLayoutMediator.TabConfigurationStrategy { tab: TabLayout.Tab, position: Int ->
                tab.text = titles[position]
                tab.view.setOnTouchListener { v, event -> onTabLayoutTouch(v,event) }
 }).attach()

private fun onTabLayoutTouch(view:View, event:MotionEvent):Boolean {
    val viewTab = view as TabLayout.TabView
    val tabIndex = viewTab.tab?.position?:-1

    var validationOk = true

    if (event.action == MotionEvent.ACTION_DOWN ) {

        val currFragment = fragmentSet[viewPager.currentItem]

        if (viewPager.currentItem!=tabIndex) {
            val validation = currFragment.validateFields()
            if (!validation.validationResult && event.action==MotionEvent.ACTION_DOWN) {
                showMessage(context, "Compila i dati obbligatori.
 ${validation.message}")
                validationOk = false
            }
        }
    }
    return !validationOk
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...