yarolegovich / DiscreteScrollView

A scrollable list of items that centers the current element and provides easy-to-use APIs for cool item animations.
5.78k stars 782 forks source link

Listview scrolls to Top on Item Removed #177

Open weihansheng opened 5 years ago

weihansheng commented 5 years ago

I use the following Code in my RecyclerView.Adapter to remove an Item. If the current scroll-position is at the top, everything works fine.

If the scroll-position is somewhere in the middle of the List, the List automatically scrolls to the Top of the View.

How to avoid this behaviour?

notifyItemRemoved(position); if (position != dataList.size()) { notifyItemRangeChanged(position, dataList.size() - position); }

ZeroCandy commented 4 years ago

I use the following Code in my RecyclerView.Adapter to remove an Item. If the current scroll-position is at the top, everything works fine.

If the scroll-position is somewhere in the middle of the List, the List automatically scrolls to the Top of the View.

How to avoid this behaviour?

notifyItemRemoved(position); if (position != dataList.size()) { notifyItemRangeChanged(position, dataList.size() - position); }

try this:

DiscreteScrollLayoutManager.java

@Override
public void onItemsRemoved(RecyclerView recyclerView, int positionStart, int itemCount) {
    int newPosition = currentPosition;
    if (recyclerViewProxy.getItemCount() == 0) {
        newPosition = NO_POSITION;
    } else if (currentPosition >= positionStart) {
        //if (currentPosition < positionStart + itemCount) {
        if (currentPosition < itemCount) {
            //If currentPosition is in the removed items, then the new item became current
            currentPosition = NO_POSITION;
        }
        newPosition = Math.max(0, currentPosition - itemCount);
    }
    onNewPosition(newPosition);
}