I use accompanist-pager wrote a TabLayout compose.
implementation("com.google.accompanist:accompanist-pager:0.28.0")
I'm curious, does using TabLayout create performance issues?
Assuming tablayout has 10 tabs, when one of the pages is displayed, should the other 9 pages be cached?
If I'm not using tablayout, only one compose is shown at a time, by navigating to another compose. Is there a performance advantage to doing this compared to using tablayout?
If compose has a ViewModel associated with it, will it affect performance more?
Related
I am stuck with a problem where I have designed a tab layout that I have used as a library in the current project which has a Recycler View. I want to show the tab layout on top and the recycler view under the tab layout.
For reference in Whatsapp, there is tab layout under which there is a recycler view that shows all the messages.
Problem is that I am confused about how to combine both activities together so the user can use the tab layout.
If anyone knows any solution please help!!
Use single-activity pattern. This pattern means that you need to use Fragments in a single activity (MainActivity, etc). Additional tip, you can use ViewPager2 with TabLayout for your Fragments.
Activities can't be composed into one, cause it designed so.
I have a standard implementation of PagerTitleStrip in ViewPager but it cannot work in ViewPager2. If I want to migrate to ViewPager2, how should I implement PagerTitleStrip within ViewPager2? I found solution and many detailed info regarding how to implement PagerTabStrip behaviour using TabLayout but it is not applicable to PagerTitleStrip.
ViewPager2 based on RecyclerView and implement only a limited subset of old ViewPager features.
Currently you cannot properly use neither PagerTitleStrip nor PagerTabStrip with ViewPager2.
Frankly I don't even know usecase where TabLayout with custom themed tabs and indicator and, maybe, some little extra code won't suffice.
Thus the only solution for ViewPager2 as for now is to use TabLayout.
Maybe if you will ask a question in context of your usecase or needed design - there will be a more detailed answer.
Hope it helps.
Currently ViewPager2 has many bugs, that should be fix may be in future.
while i was working on same i also found a bug and because of that i am unable to use ViewPager2 properly.
For now it should be good if you use ViewPager only and avoid to use ViewPager2 for some time.
Or the suggestion of Paul Ost is also good and acceptable.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I decided to give a try on the new and now available stable release of ViewPager2.
My ViewPager2 have a lot of pages, and I am using TabLayout to give each page (fragment) scrollable tab titles, but I am facing a delay when loading the ViewPager2 for the first time in my view.
Is this the normal behavior?
I figured it out what was causing the ViewPager2 initial delay to load the pages.
It happens I was using TabLayout from com.google.android.material.tabs.TabLayout along with the ViewPager2 to give scrollable tab titles to each page. But if you have a lot of pages which is my case, the UI takes time to setup each tab and place in the UI.
The solution is to remove the TabLayout from ViewPager2, and if you need to display a title, use the ToolBar as title page indicator for each page by changing and setting ToolBar text according to your needs.
Looking the bright side you end up with more clean UI, by having more space in screen for your ViewPager2 to display its content to the user.
Another thing I noticed is if you want to take full advantage of ViewPager2 performance benefits, then use the default viewpager's offscreenPageLimit, as stated bellow:
public void setOffscreenPageLimit (int limit)
Set the number of pages that should be retained to either side of the currently visible page(s). Pages beyond this limit will be recreated from the adapter when needed. Set this to OFFSCREEN_PAGE_LIMIT_DEFAULT to use RecyclerView's caching strategy. The given value must either be larger than 0, or #OFFSCREEN_PAGE_LIMIT_DEFAULT.
Pages within limit pages away from the current page are created and added to the view hierarchy, even though they are not visible on the screen. Pages outside this limit will be removed from the view hierarchy, but the ViewHolders will be recycled as usual by RecyclerView.
This is offered as an optimization. If you know in advance the number of pages you will need to support or have lazy-loading mechanisms in place on your pages, tweaking this setting can have benefits in perceived smoothness of paging animations and interaction. If you have a small number of pages (3-4) that you can keep active all at once, less time will be spent in layout for newly created view subtrees as the user pages back and forth.
You should keep this limit low, especially if your pages have complex layouts. By default it is set to OFFSCREEN_PAGE_LIMIT_DEFAULT.
https://developer.android.com/reference/androidx/viewpager2/widget/ViewPager2.html#setOffscreenPageLimit(int)
That being said I strongly recommend the replacement of your ViewPager to ViewPager2 for all cases, because ViewPager is no longer receiving Google support, and ViewPager2 has a lot to offer other than what I spoke here:
RTL (right-to-left) layout support
Vertical orientation support
Reliable Fragment support (including handling changes to the underlying Fragment collection)
Dataset change animations (including DiffUtil support)
https://developer.android.com/jetpack/androidx/releases/viewpager2
There's a whole set of improvements over ViewPager:
RTL (right-to-left) layout support
Vertical orientation support
Reliable Fragment support (including handling changes to the underlying Fragment collection)
Dataset change animations (including DiffUtil support)
These features will never be fixed in ViewPager, so migrating to ViewPager2 would be the only way to get these features.
Here is a sample from duolingo app:
I want to implement a similar feature. I can use ViewPager to make swiping tabs. What I can't seem to figure out is how to display the adjacent tabs (part of it) alongside the current tab, which is in the center.
I initially thought of implementing this using horizontal LinearLayout, but I don't know how to make the snapping effect. And it also seemed more reasonable to use something which already provides you with swiping and snapping than try to implement your own.
Using ViewPager also let's me use fragments and memory management is also effective for cases of more than a few tabs.
Any help will be appreciated. Thank You in advance.
I would like to achieve a navigation in my app like Pinterest or Trello, that is, kind of three tabs to navigation + horizontal scrolling.
I have made a custom tabbar for this (since I couldn't guess how to change tabs width in my actionbar with navigation TAB mode). So I have three buttons to navigate from one fragment to another. Now I would like to implement the horizontal scrolling like these two examples, to also navigate among my fragments.
I have read about View Pager but I don't know if it fits to my case, since I don't have only views but fragments. Does anybody have an example or an idea of how to do it? Could I apply it among different activities?
Thanks
Please take a look at the duplicate question that I just answered:
How to implement a swipe-gesture between Fragments?
I suppose the Android ViewPager is what you are looking for:
http://developer.android.com/reference/android/support/v4/view/ViewPager.html
Here is a nice tutorial on how to implement it:
http://developer.android.com/training/animation/screen-slide.html
The basic idea is that you have multiple Fragments, each representing
a different Screen. The ViewPager enables the user to swipe between
the different Fragments and display different content.
You can use ViewPager. More information about viewpager this.