dimanche 3 mai 2015

Edit framework class implementation Android for backward compatibility

I am trying to use ViewPager.PageTransformer interface in old Android 2.2 application. My target API is 8 Level. But there is known check in ViewPager compat library class

if (Build.VERSION.SDK_INT >= 11) {
        final boolean hasTransformer = transformer != null;
        final boolean needsPopulate = hasTransformer != (mPageTransformer ....
    }

So because of this check PageTransformer doesn't work in old Androids even with great NineOldAndroids library.
I know there are some solutions available, one of them is to change default implementation of ViewPager class. Futhermore, fork of dafault compat library without this check is available on github.
Of course I can use this library or just change it by myself and use my custom ViewPager class.
But in this case a lot of problems appear.
Most important that you cannot use this custom class with other libraries, for instance ViewPageIndicator, because they have been created for default ViewPager class.
Firstly,what I want to implement ?

My idea is to create some library and custom view for my purposes, this custom view will include page indicator, view pager and some other views.

So user of the library don't have to work with ViewPager directly it will use only custom view and some other classes from the library.

I have different ideas how can I solve this problem.

  1. Just create custom ViewPager , edit classes from other libraries dependent on ViewPager with my custom implementation, just to change type of class member. So just hide all this dirty stuff from user and allow him to use methods for customs view.

  2. Use reflection. At first, I thought that it is bad idea, because it is quite expensive , I thought so, but than I found the article on official Android Development blog Android Backward Compatibility . Here is example how you can use reflection in order backward compat. If to use reflection in this case, I can simply override method and than without calling super, change the part, but it will look really heavy and ugly. But we will have one great advantage, we can use this custom class with everywhere where is default ViewPager is required.

Maybe there is another good way to deal with this problem.

But I am really concerned about performance as far as this code should run on old devices, it should be as lightweight as possible.

Please suggest the best way to follow in this case, just implement library doing whatever I want with default implementation and keep it hidden from users or use reflection and save compatibility with other classes.

I will be grateful for any help and advice. Thx.





Aucun commentaire:

Enregistrer un commentaire