12

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?

Gene Z. Ragan
  • 2,643
  • 2
  • 31
  • 41
Ahmed Elshaer
  • 364
  • 7
  • 21
  • 1
    It's alpha atm, so you can still file an issue [in Google's tracker for VP2](https://issuetracker.google.com/issues/new?component=561920). However, [someone already beat you to it](https://issuetracker.google.com/issues/140751461) – Martin Marconcini Sep 26 '19 at 20:38
  • great that someone already filed it, but I don't think the made it for nothing so I asked to know the reason – Ahmed Elshaer Sep 26 '19 at 20:43

3 Answers3

3

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

FrankKrumnow
  • 501
  • 5
  • 13
  • I do not like ViewPager2. Can't even control the speed when sliding animations. It looks so lame. My ActionBar menu items take time to show up when it switches to another fragment because of the slow animation – chitgoks Oct 23 '21 at 03:41
1

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

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

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • 4
    what if I wanted to make custom behaviours? and the previous viewpager wasn't final – Ahmed Elshaer Sep 26 '19 at 20:35
  • 1
    just use `ViewPager` or build it from source, then you can remove the `final` keyword... but likely `ViewPager2` was set `final` for some reason. https://issuetracker.google.com/issues/140751461 – Martin Zeitler Sep 26 '19 at 20:46
  • sure they didn't just make it for no reason and i hope someone tells us the reason – Ahmed Elshaer Sep 26 '19 at 20:50
  • @AhmedElshaer just follow above bug on the issue tracker... but it's already two weeks without a reaction, which might lead to a "working as intended, won't fix". https://issuetracker.google.com/issues/135299048 discloses, that they won't add features until `1.0.0` stable had been released. – Martin Zeitler Sep 26 '19 at 21:00
  • @MartinZeitler if you just know how much sub classes need to customize to make this work. ..probably 6.. – Guy Jan 17 '20 at 23:25
  • @Guy I haven't tried it, but nevertheless it is possible. For some reason they've set it `final`... the common `ViewPager` is not much different, so this might be the one workaround with the least effort involved. – Martin Zeitler Jan 17 '20 at 23:36
1

You can use onTouch of recyclerView which inside of viewPage2.

fun ViewPager2.getRecyclerView(): RecyclerView {
    val recyclerViewField = ViewPager2::class.java.getDeclaredField("mRecyclerView")
    recyclerViewField.isAccessible = true
    return recyclerViewField.get(this) as RecyclerView
}

Usage

 pager2.getRecyclerView().setOnTouchListener { view, motionEvent ->
        false
    }
Jayesh
  • 101
  • 6