2

I couldn't find any guidance on that yet.

I want to make a small change on an Android API class (ViewPager) and import it to my project. I want to customize a parameter of a method that is not public.

I tried to extend this class and override the method, but since the it is protected I cannot access super.method() from my package, so I don't know how to proceed.

I decompiled it in Android Studio, tried to copy and paste the class, but it wouldn't work. I'm clueless right now.

How can I achieve it?

This is what I tried: enter image description here

Teo Inke
  • 5,928
  • 4
  • 38
  • 37
  • What method are you having trouble with? You should be able to override protected methods... – Buddy May 13 '15 at 17:41
  • See the screen shot I added – Teo Inke May 13 '15 at 17:50
  • 1
    "since the it is protected" -- `setCurrentItemInternal()` is not `protected`. It is "package-private", meaning only code in the `android.support.v4.view` package can access it. – CommonsWare May 13 '15 at 18:34
  • Wow, that's new stuff for me! Until today I thought the default for non-private/non-public was protected, end of conversation. Thanks for the info! – Teo Inke May 13 '15 at 18:43

2 Answers2

2

You can find the source of ViewPager on the AOSP project: https://android.googlesource.com/platform/frameworks/support/+/android-5.1.1_r2/v4/java/android/support/v4/view/ViewPager.java and copy it into your project. You can then modify it and extend it as you need.

However, you will not get the benefit of updates that come from new AOSP versions of ViewPager if you follow this course. I highly recommend finding another way to accomplish your goal without duplicating ViewPager like this.

Jschools
  • 2,698
  • 1
  • 17
  • 18
1

The possibilities you have:

  • Extend the class and override a public or protected method.
  • Copy and paste the whole class from the Android source code, make the needed changes.
  • Change the behavior using reflection.

Regarding the velocity of the ViewPager: check the following SO questions:

Slowing speed of Viewpager controller in android

Android: velocity-based ViewPager scrolling

Community
  • 1
  • 1
Daniel Zolnai
  • 16,487
  • 7
  • 59
  • 71
  • See my screen shot. Why can't I override that method? – Teo Inke May 13 '15 at 18:09
  • 1
    Package methods are only accessible from classes located in the same package. So if android.view.ViewPager is the class you want to access, you can call the method from android.view.View, but not from android.os.Handler, because the package is not the same anymore. – Daniel Zolnai May 13 '15 at 18:37
  • 1
    I've tried reflections but from my tests it doesn't ignore the access modifier of the method (which makes sense). I'll stick to the SO answers you referred. Thanks – Teo Inke May 13 '15 at 19:08