I have a view pager and each page selects user input. It is based on views and not fragment and is inside an Activity.
I want at the final step to collect all the user selections from all the pages.
How can I keep the selections and be able to properly update them if the user goes back/forth the viewpager?
Related
I am having a webview inside my application with multiple links inside. On a one page I have a list of items, but when I am going to an another page and going back to this page, my page can't restore the scroll position. How to save scroll position in this case?
am new to android, i have a recycler view in my activity, when i click on an item a new activity opens with details related to that specified item. My need is that when I swipe on the opened activity i need to go to next page of next recycler view item
Here's how I would solve this. For my example, I'll consider an email application, where my RecyclerView shows each email's sender and subject, and my "details" view shows the contents of each email (and allows swiping between emails).
First, I would create only a single Activity. This activity would host my list of data (my emails). This activity's layout would include a FrameLayout that I'd use to host Fragments.
My first Fragment would have a RecyclerView (and its Adapter etc). Whenever I clicked on an email, I'd send that email's position up to my Activity, which would then use a FragmentTransaction to replace() my list Fragment with my details Fragment.
This details Fragment would have a ViewPager (and its Adapter etc). Each page in the ViewPager would show the body of an email. Because I'm using ViewPager, I get automatic swiping between emails.
what you can do pass data array to intent and position then you can setup next position for right swap send specific data similarly previous position for left swap.
I am trying to read RSS feed and show the content of it in card view which is inside ViewPager with tab layout. It shows fragment with data initially, but when swiped back from another tab, the whole card layout with data,disappears.
I had to handle the same situation. I had TabLayout in ViewPager.
I have used FragmentPagerAdapter with off screen page limit set to (NO_OF_TABS - 1). This is to hold all the fragments in memory and they wont be destroyed and recreated as you swipe back and forth. As my fragments are small in size meaning not many views every fragment has so it was okay for me. But this way fragments always holds the latest data. Say, if user had entered text in EditText and swipe to end and come back, then fragment will still have the text that user had entered.
In your case, if you have not set the page limit the default is 1 so the fragment gets destroyed when you swipe away so you lose the data.
IN ADDITION TO ABOVE:
If you can not keep all fragments in memory, you may consider using FragmentStatePagerAdapter. This adapter saves the states of fragments that are being destroyed and it will use the same state to represent in fragment when user swipe back to this fragment. But there are issues around StaePagerAdapter while it destroys adn recreate the fragments, there are indexing issues so you will easily get IndexOutOfBoundException if you swipe. This is with page screen limit set to less than the total fragments.
I have a ViewPager backed by a FragmentStatePagerAdapter. The app is a single Activity app, with fragments for each "page". Currently I'm using the ViewPager to display Categories of Products, and each Category will contain a ListView (or RecyclerView) that lists all the products. Once a user taps on a product, the viewpager fragment is replaced by the ProductDetailsFragment. Which is all fine and dandy but when the user presses back, it reloads all the Category fragments. Is there anyway to avoid that? The issue is that if a user had to paginate over 3 pages they'll want to maintain their scroll state.
You should use this
fragmentTransaction.add(android.R.id.content, fragment, Fragment.class.getSimpleName());
fragmentTransaction.addToBackStack(Fragment.class.getSimpleName()).commit();
I'm building this application where I have 2 activities. Both of them consist of 3 fragment - one for title, one for content and one for tab control. It is shown at image below.
First activity serves for showing list of some data's headers (item name etc.), search, app info etc. When user presses item in list, app takes him to another activity to show him detail of chosen item. This "details" activity has 6 different content fragments and user switch between them via buttons in tab control fragment (I did switching between content fragments by showing chosen one and hiding all others - I don't know if it's right way, it's my firs app so it came to my mind at first :) ).
And what I would like to do is: When I'm in detail and I swipe left/right then I want app to take me to previous/next item's detail, to same fragment where I currently was in (so not to next content fragment, but to detail of next item in 1st activity's list).
Is this somehow possible please? Because I have totally no clue how to do it :)
And what I would like to do is: When I'm in detail and I swipe
left/rigt then I want app to take me to previous/next item's detail,
to same fragment where I currently was in (so not to next content
fragment, but to detail of next item in 1st activity's list).
If you want to swipe left-right then you would need a ViewPager widget. I'm not sure how should your details activity behave so I'm providing you with two options. Do you want to be able to switch to the next/previous item's details only when a certain fragment is the one currently viewed by the user(from the 6 content fragments, which I assume are related and show various data for a single item)? If yes then in that desired fragment you would replace the current content of the fragment(which will only act as a container) with a ViewPager and use nested fragments for the actual content. If the user switches to the details of a previous/next item's details and then suddenly wants to see the data for that item from one of the remaining 5 content fragments then you would need to have some updates method on them to refresh the data to show the current item(a OnPageChangeListener will be useful here).
Second option, is if you want to allow the user to swipe left/right from any of the 6 content fragments. If this is the case you would use the same method as above but you'll modify each of those 6 fragments.
Showing the next/previous item is easy, just get some sort of identifier of the data(a position, id), retrieve the whole used data(as in the first activity) and then cycle between it.