Scrolling to bottom of recycler view - android

I have a recycler view that loads more content when users scroll to the bottom.
To scroll to the bottom once, I can use something like:
recyclerView.smoothScrollToPosition(adapter.getItemCount() - 1)
Is it possible to continually scroll the recycler view until it reaches the very end? The recycler view does not have thousands of items so it'll only have to scroll a few times before it reaches the end.

You can easily do this by the following logic:
Scroll your Recycler view to bottom once as per your needs.
As per your response from the server maintain a value which tells if there is more data available for the user. Value can be like 0 or 1.
0: No more data is available for the user.
1: More data is available for the user.
If more data is available for the user then after the data is set on the screen again scroll user to bottom of the Recycler View else don't scroll.
Tip: Maintain a delay of about 200-300ms or 500ms(fail safe) after your data is set on the screen and before scrolling to bottom to avoid any errors or crashes.
Hope this helps.

Related

Android RecyclerView scrolls to top when child element is expanded/collapsed

I have a list of elements in a recycler view that are generated dynamically using data binding. Each element has a bottom view that is initially set to View.GONE, however once a user clicks the element and the view is expanded, the recycler view automatically scrolls back to the top of the list. Conversely, if the view is expanded and then clicked again to collapse, the recycler view scrolls to the top of the list again. I have tried keeping track of the ID's of the elements for the adapter (again using data binding), setting focus to the child element when they expand or collapse, and using binding adapters to animate the expand/collapse itself.
My suspicion is that the adapter in the recycler view is receiving a onNotifyDataChanged() alert when the height of one of the child changes, rendering an additional view (still inside one of the children though, not a separate child in the list), and automatically scrolls to the top. Is there a way to override this? Does anyone know what else might be causing the list to snap to the top when one of the elements is clicked -> expanded/collapsed?
So I found the solution to my issue in case someone encounters something similar. I actually ended up finding this answer: RecyclerView notifyDataSetChanged scrolls to top position
and the second solution involving the staggered layout manager was the route I went with, since I want to be able to wrap the content. My suspicions were correct that it had to do with the changing height of the elements in the recycler view. The expandable item's parent wrapped content, so the recycler view was forced to recalculate the size of the item once the expandable portion appeared/disappeared (at least that's what I got out of it).

Swipe and tap to enlarge recycler view

I have a recycler view which has its items in a card view. The recycler view gets its data from a sql database within the app . Now I want to implement two features to this recycler view items. First , whenever the individual cards are tapped , I want them to enlarge and display more data and should shrink back when tapped again. It is not like the case of expandable list view where it expands into a child list view. I want the height of the card to increase and just show some more information and buttons. Second I want the items to swipe left . I know there is a feature for that in recycler view , but I want to customise the swipe feature. Swipe from right should be locked , and when swiped from left , It should not go off list completely. It should show two buttons and should go off list only when one of the buttons is clicked. I have been looking , but there is no proper explaination anywhere. Please guide me through this process.
For your first question, set the adapter layout_height to wrap_content and set the buttons visibility to GONE. Implement the OnClick listener in the RecyclerView adapter and set the visibility of the buttons to VISIBLE if GONE and GONE if VISIBLE or use a boolean to alternate the visibility.
For your second question, this might help:
https://www.learn2crack.com/2016/02/custom-swipe-recyclerview.html
How can I make my Android SwipeableCardViews more like the IOS 7 mail app (swipe to show buttons)
https://github.com/daimajia/AndroidSwipeLayout

Creating a recycler view with certain requirements.

I am trying to create a specific type of recycler view with following requirements:
1) RecyclerView that would show one item per screen (1 row will take the size of screen).
2) When I scroll the recyclerview it should stop scrolling as soon as the next item is visible. (Captures the whole screen).
Please tell me if that is possible or not.
Do you have to use RecyclerView?
Probably you can do that with View Pager.
https://stackoverflow.com/a/22797619

Scroll view flicker during view inflate

I have list of elements in scroll view. When clicking the element, I need to load data from server and inflate it in the clicked view. The data I fetch from server has variable heights. So I cannot fix the view height.
My problem is,
1- I click a view to fetch data.
2- I will be showing progressbar during server fetch.
3- I scroll down to view other data. In the mean time, if the the server fetch is success I will set the data. It will automatically adjust the scrollview. Since the scroll height is changed, the data I am viewing suddenly scrolls down.
I need to lock the scroll to the content I am viewing. I tried this solution, Stop ScrollView from setting focus on EditText but this is not working during scroll.
Pleas advice. Thanks in advance.

Listview with one sticky header for second row

My layout is a bit complex.
I have a SwipeRefreshLayout in which I host a ListView. Whenever the user drags the Listview's top, the SwipeRefreshLayout performs a refresh. I also listen for the last visible item of the ListView to load next page of records (Endless scroll)
In the list's adaptor I have 2 views that I am using. The first one will only be visible in first row, the other view will remain the same for all other rows.
What I want to achieve:
On top of the row with position = 1 I want to have a sticky header. This means that when I scroll Up, the header will scroll to the top of the screen and will remain in there.
This sticky header will only be at one row
if possible I'd like to use a simple implementation as my layouts and adapters are already complex enough.
Waiting for your suggestions.
I didnt quite get your question the first time, heres the answer attempt round 2.
In your layout add an empty viewgroup (whichever you prefer, though linearlayout seems to work just great), add a scrollListener to your listView and check the position of your sticky view. If its top anchor is below (meaning its visible in the listview) the top of the screen you set the viewgroup visibility to gone, if the top anchor is either touching the top of the screen or below it, you add that view or one just like it to the viewgroup and set its visibility to visible.
You can adjust the position 2 view visibility accordingly to allow for this change to appear seamless. Can help you a bit more once you have some code and are on your way with this change.

Categories

Resources