SwipeView that starts with the layout of an intermediary fragment - android

How can creat a SwipeView that starts with the layout of an intermediary fragment from the collection instead of the first?
e.g.
I have a list of 10 emails of the same person and I click the 3th most recent to show the content in another Activity.
After that, I want to swipe to left and go to the 2nd most recent and swipe to right and go to 4th most recent.
I am following the tutorial
but it seems that the first layout displayed to the user always will correspond to first position from the collection of Fragments.
In my example the user will just be able to swipe to righ and see the emails from 3 to 10.

You can use ViewPager's setCurrentItem() method to specify the initially shown fragment by index, although you should only do this only on the first initialization (i.e. when savedInstanceState is null) so that the ViewPager can persist it's selection through Activity restarts.

Related

What is the best practice to use multiple layouts in one Activity or Fragment?

I have a fragment for user registration in that fragment's XML, I have a fixed header and a footer. The header consists of steps for showing the current progress or number of the visible fragment. The footer consist of two buttons for next and back, when I click on next button I want to show the second layout of the registration form and change the colour of the header step so that user can check that he has completed one step and is on the second step, and back button appears because I'm on the second layout. When the user finishes entering all the fields I want to check for validations like email patters is correct, password pattern is correct etc, then send all that form data to the server for user registration. My Question is what is the best practice to do such thing, should I use one fragment and in that fragment I include all the layouts and play with the visibility of the layouts, like if i press the next button the second layout gets visible and first gets gone but the data filled by the user must not get lost because i want that data to get validated and send to server. Or i should make a MainActivity in that main activity i call all the three fragments on button click like if I press the next button the second fragment replaces the first. But again the question is if I go with the activity containing multiple fragments method what will happen to the data of the fragments when user will click on next and back button?
I know it's a long summary of my question but I want to know what's the best practice to do in such cases.
Any help would be appreciated.
You should use fragments within a viewpager. This will also additionally give you the dots at the bottom visualizing what page and how many pages in total.
read more about viewpager

How to show fragment with data again when view is swiped back to in Viewpager having a tab layout?

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.

Implement some functionality like in Instagram android app

There are two things which I like on the Instagram for Android app and I'd like to implement them in my app.
1. Infinite go back in history of fragments
If you tap on a user, you can see his details, taping on followers will return a list of followers, pressing on another user will show his details... and so on. Basically you can do this thing for many times BUT when you go back everything is instant without loading. How can this be implemented? My initial thought was to have only one activity with a top actionbar and for the rest use fragments (one fragment for user details, one fragment for users list) and so on. The problem is I can't think of a good way to allow going back in history. The only way I can see is by caching all the data (user data / list adapters) is an ArrayList so when the user presses back, take the last item from the list and instantiate the fragment. Is there a better way of doing it ? I'm thinking I could start a new activity for each user interaction and them when the user presses back, simply finish the current one. My only worry in this case would be running out of memory. Is there a way to cache fragments with their state ?
2.GridView inside ScrollView
On user details there are two main layouts: a layout with user details and a gridview of images. When the user scrolls, at the scroll's end, the gridview gets new set of items (load as you scroll). While I know how to implement load as I scroll for the gridview, I don't know how to add the gridview inside a scrollview and keep listening for scroll events
Haven't got a quick answer for number 2 but for the first question why not just add the fragments to the backstack with FragmentTransaction.addToBackStack ?
That way you get the natural back-action with fragments without having to start new activities for every action.

How to swipe to same Fragment but with other data:?

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.

Dynamically ViewPager flow of pages

I've used ViewPager to let the user flip left and right to navigate among pages of a given activity.
Is it possible to programatically control the flow of pages of the ViewPager?
For exemple:
If the user didn't fill a given EditText on the current page, I don't want him to be able to navigate to the next one.
Another case: If the user is currently on page 1 and if hi filled a given EditText with a specific value and the user flip right to left, I want him to go straight to the 5th page instead of the 2nd one.
By "flipping" you mean dragging with a finger or just using the ViewPager for animation?
If it's the former, you'll have to dynamically edit the children of the ViewPager to get the effect you want. You can override onInterceptTouchEvent() to block touch events until a page is completed.
However, I recommend disabling finger-dragging in the ViewPager (perhaps using onInterceptTouchEvent()) and then using setCurrentItem(4, true) to show the animation and navigate to a specific page. The logic you need can be implemented using buttons which will call setCurrentItem(4, true). For example, you can have a "Next->" button that is grayed out unless the form is completely completed. That will be more intuitive then the first option anyway.
Edit: 4 is just whatever child index you want to switch to.

Categories

Resources