Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the Android ViewPager2 class final?

While using ViewPager2 in my current project, I wanted to use setOnTouchListener, but I was unable to get any events.

I decided to extend the ViewPager2 class to override the touch event methods, but was unable to as the class definition is final.

Why is this class final and is there any way to extend it so that I can override the touch events?

like image 214
Ahmed Elshaer Avatar asked Sep 26 '19 20:09

Ahmed Elshaer


People also ask

What is ViewPager2 in android?

ViewPager2 uses FragmentStateAdapter objects as a supply for new pages to display, so the FragmentStateAdapter will use the fragment class that you created earlier. Create an activity that does the following things: Sets the content view to be the layout with the ViewPager2 .

What is the difference between ViewPager and ViewPager2?

ViewPager2 is an improved version of the ViewPager library that offers enhanced functionality and addresses common difficulties with using ViewPager . If your app already uses ViewPager , read this page to learn more about migrating to ViewPager2 .

How do I turn off ViewPager2 swipe?

You can easily do that by creating a custom class inherits from viewPager and override two methods: “onTouchEvent()” and “onInterceptTouchEvent()” and return true or false to disable and enable the swiping on certain touch events i.e say swiping.


2 Answers

Well I think they f'd up the whole viewpager thing from the getgo. Most liky because all the fragment in fragment code was also messed up back than.

They even ask us to not use the normal viewpager from brand new androidx because it has errors. I think some errors that are reported are now older code trying to fix issues from old viewpager implementations. I think they now got the code stable enough to be useable and don't want us to implement all the now-unnessessary and maybe counter-productive fixes when transitioning to viewpager2.

I am quite mad at how lazy google handled the whole fragments and viewpager stuff in the past. It speaks volumes they never got it working in the main codebase and always suggested using the compat packages.

So in short. Now after years they got it working and they want you to drop all your fixes that are now not needed anymore..

like image 197
FrankKrumnow Avatar answered Nov 15 '22 19:11

FrankKrumnow


Likely it is final, because it is not the idea to extend on it

... and that keyword is extremely effective in preventing that.

like image 38
Martin Zeitler Avatar answered Nov 15 '22 19:11

Martin Zeitler