I'm writing an app with multiple tabs. Every tab is a fragment and every tab uses the same layout file. You can change to the next tab by swiping (handled by a SwipeAdapter). Inflating the same layout for every tab again is quite inefficient and makes the app lag. Is there a way to inflate the layout once in the beginning and to reuse the inflated layout for all tabs?
Or alternatively, is there a way to do so with e.g. three inflated layouts? (-> one for each the currently displayed tab and both neighbours, so when you swipe from tab 4 to 5, the inflated layout of tab 3 - which is not needed anymore - is used for tab 6)
I know you can reuse inflated views with a listview, but it wouldn't work here because I want the user to swipe instead of continuous scrolling.
Welcome to StackOverflow. Use RecyclerView with a SnapHelper (works both horizontally and vertically). If you Google it you will find many tutorials.
Alternatively you can use ViewPager2, which is in alpha state (if you can wait for it to be stable). It is based in RecyclerView, and is an update to the old ViewPager to bring it to 2019. See this tutorial for example Hands on With ViewPager2. See also ViewPager2 releases page at Google to keep track of development progress.
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'm trying to add a slidingUpPanel(Sliding up panel) across all activities. This panel consists of a viewpager which in turn consists of two fragments. One of these fragments has a recyclerView (more than 1000 items) and the other fragment has some data that changes dynamically depending on the users' choice. This panel is very similar to that of google play music and soundcloud. Now to display this panel I tested two approaches:
1) I created a base activity and added the sliding up panel to it and then extended rest of the activities to base activity. So this way I have to create only one panel and viewpager.
2) I included the sliding up panel in all activities. But this approach is quite unmanageable as I have create viewpagers for all activities and if data changes in one activity I have to write the code to reflect that change across all activities.
Now the problem with both the methods is that each time I open a new activity, a new instance of the viewpager and fragments is created. So, suppose if I have 1000+ items in the recyclerview fragment, switching activities takes more than 2-3 seconds, because each time new instance of fragment is created and the data is loaded all again. This will definitely result in bad user experience.
Is there any way by which the viewpager and the fragments are created only once(when the app starts) and are destroyed only when user closes the app? And data should not load each time user switches activities. I just want to reduce the activity switching time. Any ideas?
Thank you.
Well, for such ui elements as your sliding panel, which stays the same for many items it is preferable to have a single activity.
So if you have 1 activity, you can have a viewpager inside sliding panel, and that part stays untouched. Next, inside your activity you can have FrameLayout wich can be used to host fragments. Doing this you can achieve single instance of sliding panel and navigation between content with fragments.
Having some heavy data collections makes you wishing minimum recreation of that items.
Two approaches for this are (assuming you're using SlidingUpPanelLayout by sothree)
With bottom navigation view
Easy way to do it is creating a bottom navigation view and keeping it in MainActivity and attaching the sliding up panel layout to the bottom navigation view ( layout_above = bottomnavbar_id ) since bottom navigation stays throughout the app so sliding up panel will also have to stay with it
Without bottom navigation view
Create a frame layout inside MainActivity give attributes width and height as match_parent and create slidinguppanelayout and give attributes gravity="bottom"
make the frame layout stay above that slidinguppanellayout , use that frame_layout to show content your want to show from fragments
that's all
What I would like to achieve: Have a ViewPager with TabLayout where each Tab contains a new Fragment that has a RecyclerView layout out horizontally and managed by a FragmentPagerAdapter.
What I am struggling with: The default behaviour is that when you get end of the list on one of the tabs, it switches Tabs and snaps (snapping means if you pull over 50% of the viewport, it jumps to either direction). I want it to free-flow and not to snap. Sort of like how it happens in iOS by default.
I got inspired for this setup by the following two CodePath guide:
https://guides.codepath.com/android/google-play-style-tabs-using-tablayout
https://guides.codepath.com/android/ViewPager-with-FragmentPagerAdapter
How should I approach this problem (preferably without any other libraries)?
What I would do is use HorizontalScrollView instead of a ViewPager. The only caveat is that the view pager sends the lifecycle events to fragments as you scroll between them, whereas for HorizontalScrollView you will have to add them initially, and then they will be continuously active unless you manually change that. Also you may have an option to not use the fragments and use simple views depending on what you use the fragments for.
I would definitely not override the touch events for the ViewPager because it does some cleanup when it detects the UP motion, so just swallowing it is not a correct thing to do.
I've just started converting my apps to use fragments. I have a ListView activity and a "Details" activity, which the user goes to after selecting an item in the Listview. I've converted them to fragments successfully, however, I have a header in the layout of each activity. The ListView has a "reload" icon in the header, but the header in the details does not.
Everything works fine when I'm viewing the app in portrait, but I have a layout in the "layout-land" folder which contains both fragments, so, when viewing in landscape, you see both the ListView and the "Details" view. The problem is the header layout is shown in both fragments.
My question is what is the best practice for covering my layouts to the new fragment setup? I'm thinking, instead of having the header in each fragment's layout, I just add it to the layout that contains both fragments?
Sorry if this is just obvious question, I'm still trying to wrap my head around the whole fragments paradigm.
That's why the use of ActionBar is recommended. If you include an action bar in your app with a refresh button , users will know (and learn) to use that button. The onClick implementation is all yours and you could refresh the list adapters accordingly. There is no need to then build these UI elements in all your controls.
I'm currently developing an application that has a tab bar, and 3
different views: the first is a master-detail, the second one a
gallery, the third is a simple webview.
I was using the TabActivity, but since Android 3.0, this class is
deprecated and Android reference
suggests to use Fragments.
I switched then to an ActionBar, with Tabs and Action
Items. Inseide the first tab item I have a layout with 2 fragments (my
master-detail view). When I switch through tabs I want that my layout
change as I described above, so I thought to hide the left fragment
(the master listview) and work only in the detail fragment.. but with
this solution I have only one main activity with a lot of fragments
attached to it, and for each fragment displayed I need to modify the
Action Item shown and handle different actions in
OnOptionItemSelected.
Is this a good way to implement this kind of
application or should I consider different solutions?
You should have a single fragment container where the fragments are replaced depending on the tab selected.
One activity and multiple fragments is the right approach.