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.
Related
I have vertical scrolling list in Activity 'A' and view pager in Activity 'B'. When a user clicks on an item in Activity 'A' it goes to Activity 'B' for detail. Now user can swipe left or right to view details of next item inside list directly from Activity 'B'.
List in Activity 'A' is endless scrolling, when it reaches to end it loads more items automatically.
I am using fragment which represents one page inside view pager in Activity 'B' which takes the same object as a parameter as in RecyclerView item in Activity 'A'.
This pattern is commonly used for reading articles.
Now my question has two parts:
First is how to use the same data list for both activities? I want to keep data at one place as both activities using same data, only representation and navigation is different, one inside RecyclerView and other is inside ViewPager and data list is already loaded for RecyclerView in Activity A. Moreover data is also large so i don't want to duplicate data in memory for each activity.
Second is when i swipe next from view pager and go to next item's detail in activity 'B' the recycler view in Activity 'A' should also scroll to next item synchronously, although it's not visible to user. so that RecyclerView can load more items when reaches till end and ViewPager can also behave endless swipe like endless scrolling list. Is there any way to achieve this behavior?
Thanks in advance.
You can use Local Database for this purpose. So both activity will access data from DB. Only pass index of item to second activity and onPageScroll listener you can change that index and get data from DB according to page swipe(left/right).
I have a ListView that represents a list of folders and when I click one item, I want to load another list that shows the content of this folder. How can I link these views together and to be able to go back to the first one with the back button ?
Well, since you didn't provide a code in your question, I will try giving an answer in a descriptive manner.
You can use fragments to do this. Your base Activity's layout must have a fragment container which you will use to display your fragment containing the first ListView data. Once after you click on a cell, you call the constructor of the second ListView, and replace the current content of the fragment container with the newly created fragment.
You may implement a back feature by implementing an ArrayList in your Activity and appending the fragments into that array list as the user navigates through the list. onBack pressed you can call the top most fragment from that Array list and assign it to the fragment container.
This should work well, given there are not too many types of ListViews that you may want to implement.
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.
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.
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.