Integrating viewpager with a listview - android

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.

Related

click on listview, android studio

I would like to click on an item of a listview to show another listview of other items, what can I do?
in my application I have a main Activity, where I have inserted a bottom view navigation, when you click on an item in the bottom view menu, the corresponding fragment appears in the main activity.
my listview is in the first fragment, and in the same fragment the next screen must appear, after the click in the listview.
do I have to replace the fragment of the listview with another one or can I replace only the listview? how?
Thanks for the attention
Instead of those options (replacing the fragment / list view), you could use the onItemSelected method to repopulate the list view object with the new list. Since you already know how to do this, it would seem the simplest option. A simple switch statement would suffice.

how to make vertical scrolling list in sync with view pager

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).

How can I make a prev and next button to show next and prev element of a recycler view

I have implemented a recyclerview. On clicking a item it would display the related field of that item (eg. name, age).
Now i want to make two buttons prev and next to show the details of previous and next item without going back to the list (in same activity). How can i do that?
From my understanding you want to implement ViewPager. Check ViewPager
Here is the tutorial.

android with viewpager When i click on Button in the first fragment I want the app to navigate to another activity

I have two fragments implemented using View Pager. In fragment 1 I have 2 button and in fragment 2 I have list of item.
When I click on particular button in the first fragment I want the app to navigate to another activity.
but this scenario when I click on particular button in the first fragment then It's click on Second fragment list item and open list item detail.
If anybody knows, please suggest me a solution. I need to do this as soon as possible.
Thanks
One thing that came on top of my head is this:
Store your fragments in a ViewPager.
Listen to button click events in your first fragment.
From your first fragment, notify your activity of said click event.
Your activity (which also houses the ViewPager) should respond to the click event by commanding the ViewPager to move to the second page (which also contains your second fragment). You can do this by calling yourViewPager.setCurrentItem().
And there you have it. A fragment that contains button which move the pager onto the next page/fragment.

Should a ViewPager that creates ListFragments have each ListFragment call back to the ViewPager to handle list item clicks?

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?

Categories

Resources