How to preserve Recycler View content and scroll position in a fragment? - android-fragments

I have a list fragment (containing RecyclerView) and a details fragment. When navigating from list fragment to details fragment and then back to list fragment (using navigation component), the RecyclerView in the list fragment gets recreated which in my case is a bit expensive task and also the scroll position is lost. How can I save the RecyclerView contents and it's scroll position and prevent it from recreating when navigating back to list fragment...

Related

recyclerview position is back to 0 when navigating to another fragment

So, I have 3 fragments. Explore Fragment, Expanded Version of Explore Fragment, and Detail Fragment.
When I navigate into Detail Fragment from Explore Fragment, everything is fine, the recyclerView position is not back to 0. But when navigating to Detail Fragment from the Expanded Version of Explore Fragment, then returning to it, the position is back to 0. So, how does this happen? Why is this only affecting the Expanded Version of Explore Fragment? And how do I solve this if I want to persist in the scroll position when navigating to Detail Fragment from Expanded Fragment?

How to save reyclerview's scroll position?

In activity1, I used recycler view to store images downloaded from firebase. When an image is clicked, activity2 is opened to show description of image. How to store scroll position of activity1(when i click on up button in act2, it has to go to previous scroll position in act1)
Store firstVisiblePosition from LayoutManager of RecyclerView
firstVisiblePosition = ((LinearLayoutManager)rv.getLayoutManager()).findFirstCompletelyVisibleItemPosition();
or
Store the clicked position.
Then, registerReceiver with an instance of BroadcastReceiver using LocalBroadcastManager.getInstance(this).registerReceiver(broadCastListener, filter) in Activity1
And send a message using LocalBroadcastManager.getInstance(this) from Activity2
Increment the value of firstVisiblePosition when you get a callback in BroadcastReceiver.onReceive
Finally in onResume
((LinearLayoutManager) rv.getLayoutManager()).scrollToPosition(firstVisiblePosition);

NavigationController & hidesBarsOnSwipe with CollectionView subview

I'm pretty new to iOS development and I'm having some trouble with navigation controller and a collection view as a Subview.
Ive started my project with a single view application and embedded in a Navigation controller (The Navigation controller root is my ViewController).
Inside my view controller i've added a Collection view with a custom cell (it doesn't take the whole screen).
when I run my application everything works fine.
when I tried to set the method
navigationController?.hidesBarsOnSwipe = true
nothing happens inside my Collection view even though I'm scrolling, but if I swipe within the View controller and not the Collection view the method works and the Navigation bar gets hidden just fine.
I want the method to work when I swipe inside the Collection view.
Thank...
If your collection view top is set to the top layout guide top then the view behind is has never scrolled, i.e. your view isn't scrolling so the navigation bar is not hiding.
Try setting your collection view top to the "Top Layout Guide.Top". Try changing this to "Superview.Top".

Populate ListView only once on fragment with FragmentTabHost Nested Fragment

I'm developing an app which has a ListView on a Fragment, I'm getting some data from SQLite using AsyncTask and filling a ListView, but every time i switch between the tabs, the ListView is populated all over again, it's not getting all the data again, but it is putting on the List again...
Is there a way to stop that? I want that my ListView is populated only once when i create the fragment. i am using Android ActionbarSherlock Nested Fragment Tabs.
You have to detach fragment an then attach again once you return to that tab, instead of remove fragment and create again.

replace fragments inside fragment displayed by the viewpager

Please see the wireframe image http://i.imgur.com/dPAMA.png first.
I am using a ViewPager to display fragments inside a FragmentActivity. ViewPager gets fragments from the attached FragmentPagerAdapter
mViewPager = (ViewPager) findViewById(R.id.view_pager);
mAdapter = new HomePagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mAdapter);
Suppose viewpager has 3 fragments to display say Fragment1, Fragment2, Fragment3.
In the above image 'displaying fragment1' is the title for the first fragment, "Fragment1". This fragment is a list fragment and displays a list. Fragment2 and Fragment3 have their own content to display. when the user swipes on the screen, ViewPager will display the next fragment, "Fragment2".
What i want is that, when an item from the list(displayed by Fragment1) is clicked, Fragment1 should be completely replaced with some other Fragment, say Fragment4 (this is different fragment and is not returned by the attached adapter). when Fragment4 is displayed, the actionbar should change its content. whenever user works on Fragment4 and onBackPress or an action from the ActionBar, Fragment1 with the list should be displayed again. Meanwhile ViewPager should behave the same i.e on swipe, and the next fragment(in our case Fragment2) will be displayed.
basically, i want to get the same behavior as in example at :
http://developer.android.com/training/basics/fragments/fragment-ui.html#Replace
In this example the ListFragment is displayed directly inside a FragmentActivity. I am displaying the ListFragment with a ViewPager. I want the same functionality from the above example but inside fragments displayed with ViewPager. This was very difficult to achieve before the release of Android 4.2. this version of android supports nested fragments using getChildFragmentManager() so, i see some hope.
I am trying this on my side for many days and also scanned entire stackoverflow for Q & A related to nested fragments but didn't get what i want.
So, is this possible to achieve? if yes, then how to?
This was a big problem before Android 4.2 but it seems to easier now with nested fragments, apart from ensuring the back button works. I posted my solution to your question over here: https://stackoverflow.com/a/13925130/467509
My solution answers everything you asked apart from changing the ActionBar.

Resources