Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewPager preview layout in Android Studio

Is it possible to show a preview layout for ViewPager in AndroidStudio like in ListView/RecyclerView/etc.?

like image 205
Daniel H. Avatar asked Oct 27 '15 14:10

Daniel H.


People also ask

How do I use ViewPager on Android?

Android ViewPager widget is found in the support library and it allows the user to swipe left or right to see an entirely new screen. Today we're implementing a ViewPager by using Views and PagerAdapter. Though we can implement the same using Fragments too, but we'll discuss that in a later tutorial.

How do I get ViewPager current position?

getChildAt(1); will return the current page. But, if you then change back to page 2 (from page 3) your list of children will be in this order page 2, page 3, page 1 which means that ViewPager. getChildAt(1); does not return the current page.

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 .


1 Answers

Short answer: No.

I think you're talking about ViewPager from ViewPager 2. My answer will be assuming ViewPager 2. Pls update the question to clarify that.

- Workaround: a hidden include

<include              android:visibility="gone"             tools:visibility="visible"             layout="@layout/item_that_shows_inside_viewholder"/> 

And I agree that's a very poor way to do it. If in constraint layout you can loosely match the same constraints to make this show on top. And yes, you will inflate this in production code so you may incurr in performance slow down depending on what you have there.

It's very unfortunate that tools:layout doesn't exist or work properly (yes, I got inconsistent results while trying it).

- 2nd less worst way is using isInEditMode on code on the parent class

something in the fashion of

class CustomView .... init {     // inflate layout that contains ViewPager     if ( isInEditMode() ) {       //do something that replaces ViewPager with its inflated view holder     }  } 

which has served me well when I can use that. However that's unsuitable for most places.

- If at least ViewPager were not final we could extend and do some tricks there using isInEditMode like above.

- You can make a custom class wrapper with internal field ViewPager

One way that does certainly work is making your custom class extend FrameLayout and having an internal view that is your actual ViewPager. But then you have to re-implement and delegate all its methods, which is a big pain. Maybe kotlin has a way to do that, but so far I don't know how. Or maybe using reflection or kotlin poet could be a way to do that. It's too risky for my taste.

- If we could make a databinding adapter to do the isInEditMode like above it would work. But databinding adapters don't run in preview.

- I tend to think these tools attributes get processed by Android Studio, so it would take probably an android studio plugin to work around it.

This is the current full list of tools attributes: https://developer.android.com/studio/write/tool-attributes.html#toolslistitem_toolslistheader_toolslistfooter

It may be useful reading this Android XML Is there a way to use the tool namespace with custom attributes?

where a library to read custom attributes with tools gets mentioned. There may be an alternative with this library, but I have never used it and not sure how it works: https://github.com/giljulio/sneakpeek

I'd love to be proved wrong, but in my opinion all of the options are dead ends or too much effort.

like image 99
Fabio Avatar answered Oct 03 '22 07:10

Fabio