gpt4 book ai didi

android - RecyclerView 在项目移除时滚动到顶部

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:02:01 24 4
gpt4 key购买 nike

我在我的 RecyclerView.Adapter 中使用以下代码来删除项目。如果当前滚动位置在顶部,则一切正常。

如果滚动位置在列表的中间某处,列表会自动滚动到 View 的顶部。

如何避免这种行为?

public class MyAdapter extends RecyclerView.Adapter {

...

public void removeItem(int pos){
mDataset.remove(pos); //List with Items
notifyItemRemoved(pos);
notifyItemRangeChanged(pos,mDataset.size());
}
}

    <FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">


<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>
...
</FrameLayout>

最佳答案

添加以下行为我解决了这个问题

recyclerView.setHasFixedSize(true);

关于android - RecyclerView 在项目移除时滚动到顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36400336/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com