I'm building an app that uses tabs with a viewpager and fragments. However, when the user selects an actionbar item, it should show a fragment inside the View where the tabs are displayed.
I tried adding an extra item to the viewpager(For the extra fragment to be displayed), and setting the selected item in the viewpager to the extra fragment when the action item is clicked, it did show the extra fragment, but the last tab remains active below that fragment. Is there any other way to show an arbitrary fragment within the area for the tabs?
Related
I have main activity in which list fragment is shown when the app is launched, then there is floatingbutton in list fragment when that button is clicked it opens next fragment (adding fragment) and there is again one more button in adding fragment when it is liked then again list fragment is shown in the main activity .
Now I want that when the floating button of list fragment is clicked then list fragment should hide and adding fragment should show and when the floating button of adding fragment is clicked then adding fragment should hide and list fragment should show.
How can I do this thing using hide and show a method of fragment class?
how should I call these methods in both fragments when the floating buttons in both fragments are clicked.
Refer this link and check example http://www.java2s.com/Code/Android/Core-Class/Demonstrationofhidingandshowingfragments.htm
Currently I have a ViewPager with 4 Tabs, each one is a different Fragment. I'm now switching my app to Bottom Bar Navigation but there's a problem with keeping the Fragment state and re-using it later.
The Problem :
Initially Tab_1 is visible. If user selects Tab_2 and then re-selects Tab_1, then I can see that the Fragment for Tab_1 goes through onCreate() and re-downloads the data from the server. With ViewPager I was using the setOffScreenPagesLimit() is there something equivalent with bottom bar ?
I have a listview, clicking on one of the item in listview takes me to some fragment (different item may take to different fragment). Once a new fragment is opened I want to swipe to open the the next item in list instead of going back and clicking on the next item. I am thinking of using ViewPagers but not sure how to open the next item in the list.
You can create an Activity which has a ViewPager and Fragments with desired info as its children. On a list item click, take the position info and pass it to the Activity via Bundle. And inside the Activity after setting the ViewPager, you can show the desired Fragment with ViewPager's setCurrentItem(int item) function.
I am having a viewpager and having 3 tabs in it. All the three tabs are having their fragments. What I noticed that each fragment data is getting loaded at the first time when I am setting adapter to viewpager.
What I need is to load the fragment data of the corresponding tab only when the particular tab is getting clicked.
I have noticed one more issue in it. Like when the activity started in which I am having viewPager, then the data of the first tab's fragment get loaded at the same time the data of the second tab's fragment also get loaded and that is shown when I click on that tab.
Same way when I click on Second Tab, third Tab's Fragment date get loaded.
So what I need is that the data of the fragment should be loaded only when I click on the particular tab.
Thank you so much in advance..
You should use the ViewPager.setOffscreenPageLimit(0) to only load the currently displayed content.
Its the View-pager behaviour that it loads one page left/right to current page even if you have specified setOfflinePageLimit(0).
To load each fragment on click on tab, you should use LocalBroadcastReciever.
I have an activity with a ViewPager as part of its layout that creates a series of ListFragments as its pages. When the user clicks on a list item in one of those ListFragments, it takes them to a detail page that allows them to edit the details of that list item.
So in the ListFragment's onListItemClick() method, I first check to see if it is in tablet (two-pane) mode, and if it is, I perform a fragment transaction that replaces the right-hand detail fragment with the detail fragment for the newly clicked list item. If it is running on a normal phone, I just create a new activity that takes up the entire screen. So basically, I currently have the ListFragment perform the new fragment/activity creation for when the user clicks on one of its items.
But instead, should the ListFragment's onListItemClick() method call back to the ViewPager activity, and have the ViewPager activity contain this fragment/activity creation code?
The problem is that a ViewPager activity can only create fragments, so I cannot see how I can have each ListFragment call back to a custom ListFragment parent activity. As far as I can tell, the ListFragment must have the ViewPager activity as its parent activity. So if this is the case, is it good practice for the ListFragment to call back to its ViewPager parent activity to handle the fragment/activity creation for its list item clicks?