Page fixed at a specific post in recyclerview - android

I have an activity A. From that activity I go to activity B. Now there are some image descriptions in A and the corresponding images are in B. I want to make a system where based on the descriptions from activity A, the screen gets fixed to the post with the specific image among all the posts when I move from A to B. In other words I want to fix recyclerview to a specific child when the activity B opens.
P.S.- I have the postkeys of all the posts in recyclerview in B in my database.
Thanks in advance.

find the row position associated to your postKey from your activity B recyclerViewAdapter then auto scroll to that postion
recyclerView.smoothScrollToPosition(rowPosition); //if you want a smooth scroll
or
recyclerview.scrollToPosition(rowPosition);

Related

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

What is the best way to pass data between fragments in to and fro situation with crash handling

I am new to android development so forgive if i asked a silly question. Below i am narrating my requirement and the solution I thought of please read and help me if you have any solution.
In my application i have a activity that contain a toolbar on top, different options in the bottom and in the center area a framelayout where i will load fragments that will render different advertisements(Mostly images) based on the options the fragments using viewpager shows multiple images. Check the following image.
Now my problem is let's assume i selected Fashion option with featured sub option then title should be changed to Fashion-Featured and Fragment should show images related to Fashion>featured let's call this fragment fragment1. Now if the user tap on an image then the application will render another fragment lets call it fragment2.
In the fragment2 the selected image will be rendered in large with two buttons More and Stores. Here if the user taps on more button then the thumbnail images of fashion>featured option will be displayed so that user can select another image. If the user taps on store button then the another fragment will load let's call it fragment3.
In fragment3 the stores selling that particular product will be listed.
Now my problem is how can i pass the data between activity to fragment1 then fragment1 to fragment2 then from fragment2 to fragment3 so that the if the user press backbutton still the fragment2 should show the fashion>featured images with the tapped image on large and if the user press another backbutton then fragment1 should show fashion>featured option images. The data passing should be fail safe that is if the app goes in the background then come to foreground then also it should work.
Please help me out. I am planning to implement using sqlite database and reference in shared preference. I think this is not an exceptional requirement other application also handles this kind of requirement so i need a better way to achieve this.
If you would like to pass some data to a new fragment see this answer: How to pass a variable from Activity to Fragment, and pass it back?
If your fragment is already created you can add some method to it and call this method with required argument.

How should I save state of Fragment that contain a listview on android?

In my app there are two fragments and one activity. On all of them I used listview and fill the content from database. When rotate screen the position of content in activity don't change and start on correct way (as I googled and understand it is normal for activity because of "Bundle savedInstanceState"). My problem is with fragment part that both of the list on fragments are too long list and it could be annoying for end user when press back and came back to fragments start navigate to appropriate item. I googled and find some answer to my question but they don't work for me.
In this case, the states saving or restoring won't work anymore, because the listview was dynamically fill up by database or internet. when I facing this problem, I have a long size dataset for that listview, the rest datasets are coming because my data was show as pagination mode, user may scroll out far from first page, so when fragment destroying, all you can do just save the particular parameters to State Bundle, e.g. the page number, the selection index, the selection y-axis, finally restore the state by your own hands.

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.

Categories

Resources