Android top pagination - android

I have a RecyclerView with linear layout manager. There 3 items loaded, current position after first position. I add custom RecyclerView.OnScrollListener and when i scroll down, it loads more item
adapter.updateDays(weeksList.subList(currentItem, items.size()));
adapter.notifyItemRangeChanged(currentItem, currentItem + (items.size() - currentItem));
and i updating in my adapter class list, i don't have problems with down scroll
but, if I scroll up, I use
adapter.updateTopDays(list.subList(0, 5));
adapter.notifyItemRangeChanged(0, 5);
and in my adapter class, i update
this.items.addAll(0,list);
notifyDataSetChanged();
that method cause strange behavior, like some items dissapeared, lagging, how can i fix this?

Related

adapter.notifyItemInserted(position == 0) not animating when using with linearLayoutManager.setreverselayout in android RecyclerView?

Here is the code of how I am updating the list and notify.
adapterlist = newlist;
and then I call notifyiteminserted(postionofinsertion);
I am using setReverseLayout(true) to make it a chat like RecyclerView;
My RV stays still, no change is visible. I need to scroll down to see the item.
Expectations -> If I am at the last item then item insert animation should happen and I am beyond the last item then nothing should happen.
notifyiteminserted(0) is not working. There is no change in the layout and di have to scroll down.
If I use notifyiteminserted(2 or 4) it works the animation is showing.

Scrolling GridView only on Button Click

I want to achieve the following layout -
In this layout, I want to show all the available items to the user. Pressing right arrow will show the next 6 available items. This should happen via horizontal scrolling type motion.
Since items can be many, I want to use GridView for this because of its recycling optimisation. Optimisation is necessary because every item is a Linear Layout in itself.
Now, I can't use GridView because I want to show next 6 items using the arrows only. Also, it will be good if this happens in smooth scrolling type motion.
Can I make any tweak to Gridview so that I can use arrows to scroll GridView to show next 6 items or Is there any other ViewGroup available to achieve this along with the recycling optimisation.
This example is for listview you can refer for gridview also.
For a SmoothScroll with Scroll duration:
getListView().smoothScrollToPositionFromTop(position,offset,duration);
Parameters
position -> Position to scroll to
offset ---->Desired distance in pixels of position from the top of the view when scrolling is finished
duration-> Number of milliseconds to use for the scroll
Note: From API 11.
listview is quite lengthy and also with alphabet scroller. Then I found that the same function can take other parameters as well :)
To position the current selection:
int h1 = mListView.getHeight();
int h2 = v.getHeight();
mListView.smoothScrollToPositionFromTop(position, h1/2 - h2/2, duration);
Or
You can use RecyclerView :
You have to make use of LayoutManager for that. Follow the below steps.
1). First of all, declare LayoutManager in your Activity/Fragment. For example, I have taken LinearLayoutManager
private LinearLayoutManager mLinearLayoutManager;
2). Initialise the LinearLayoutManager and set that to your RecyclerView
mLinearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(mLinearLayoutManager);
3). On your Button onClick, do this to scroll to the bottom of your RecyclerView.
mLinearLayoutManager.scrollToPosition(yourList.size() - 1); // yourList is the ArrayList that you are passing to your RecyclerView Adapter.
Hope this will help..!!
You could try giving corresponding list input to adapter at each click. And then call the notifydatasetchanged to reflect the changes on the View

NotifyDataSetChanged refreshes RecyclerView but scrolls as well.

I have a RecyclerView and one item row needs to be updated like every second (SeekBar). So I call NotifyDataSetChanged once I update the data in the list. UI gets updated but the issue is that the RecyclerView scrolls so that this particular item is either at top or bottom of the screen.
I don't want RecyclerView to scroll.
// Update Data
mData.set(mData.indexOf(cardData), cardData);
// RefreshView
refreshView() {
runOnUiThread(new Runnable(){
notifyDataSetChanged();
}
);
You can update one item using notifyItemChanged(position); and u wont update all data in adapter, you will update only certain item
It's better to use the other notify methods from the RecyclerView.
For your usecase only swap out one item in your Adapter instead of all items and use notifyItemChanged(position).
This will be faster, the View won't scroll and if you ever will add an animation this will only work if you don't use notifyDataSetChanged
I got it working. I was having a card view above the recycler view.I just removed it. Now the whole screen is a recyclerView and I cannot see any auto scrolls.

How to avoid jerk when notifyDataSetChanged method of adapter is called?

I am trying to add pagination in my listView.
I added a method in my custom BaseAdapter which adds next page results at top of list on scroll to first item in list.
public void addEntriesToTop(List<ChatModel> entries) {
// Add entries to the top of the list
this.chatMessages.addAll(0, entries);
notifyDataSetChanged();
}
So if I have to add next page on top of ListView, I am using like this in my activity.
//result contain new items to be added to top of list
adapter.addEntriesToTop(result);
//so that list scroll sets to last visible message to user
final int index = result.size();
listView.post(new Runnable() {
#Override
public void run() {
//go to user's last scroll position
messagesContainer.setSelectionFromTop(index, 0);
}
});
This method is working fine and retains user's last scroll position. But it has a problem, when I call addEntriesToTop() it scrolls the ListView to bottom(because of notifyDataSetChanged() ) and then when I call setSelectionFromTop it scrolls to user's last position. In this transition, there is small jerk.
Please help me to smooth this transition.
Thanks
As ListView is old and now its obselete, now you can use Recyclerview
instead of ListView, here is the link that helps you to convert your
ListView to RecyclerView. RecyclerView have many methods to add your custom animations and also it have not any jerks while adding/removing items.
Replacing ListView with RecyclerView
For Default Animations you can use this link:
RecyclerView Animations – Add & Remove Items

Animate RecyclerView additions from bottom

I am using the latest RecyclerView library to display a list with an option to add items to this list. The list is in reverse order, so newest additions are displayed at the bottom. When adding new items to a recycler view from the top, a nice animation is displayed and the list is automatically scrolled to display the newest item.
However, when adding new items from the bottom, items are not similarly pushed up as the new item is being animated. This leads to odd behavior where a new item is added and the animation cannot be seen unless the user scrolls to the bottom of the list.
As a temporary fix, I've forced the recycler view adapter to scroll to the first position after adding a new item. This isn't as seamless as adding from the top of a recycler view, is there a proper way to implement this?
The corresponding code is below:
la.addItem(0, msg);
layoutManager.scrollToPosition(0);
Add this function to your adapter class
public void addItem(Object aObject)
{
mAdapterList.add(aFormElement);
notifyItemInserted(mAdapterList.size()); // Passing position where insertion happen
}
If you are not getting animation than only use below function.
try to call scroll to position on recyclerview at your required position after calling addItem().

Categories

Resources