Android RecyclerView 滚动时出现重复项
Android RecyclerView Duplicate Item When Scrolling
我在 RecyclerView
中遇到问题。当我在 RV 中移动项目然后滚动时,看到一些项目重复了。
RecyclerView
会回收view.When你删除的数据,调用notifyItemChanged(pos)
或notifyDataSetChanged()
方法。
问题出在你的 notifyDataSetChanged()
上。
检查您是否正确使用它。
即:
private void parseJsonFeed(JSONArray response) {
for (int i = 0; i < response.length(); i++)
try {
JSONObject obj = response.getJSONObject(i);
MyData myData = new MyData();
myData.setContent_title(obj.getString("content_title"));
...
...
...
...
// adding content to array
homeList.add(myData);
} catch (JSONException e) {
e.printStackTrace();
}
//Notifying the adapter that data has been added or changed
//this must always be called else the recycler would not understand when to stop or start working.
recyclerViewAdapter.notifyDataSetChanged();
}
我知道它晚了但希望它能帮助别人。在您的适配器中覆盖这两个方法。
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}
我想我来晚了,但无论如何我会建议一种对我有用的方法,也许有人仍然面临这个问题..
因此,我在 nestedScrollView 中添加了我的 recyclerview,然后为我的 recyclerview 禁用了嵌套滚动。
使用此方法,nestedScrollView 将检测到滚动,并且 recyclerview 在滚动时停止复制项目。
这是我的 xml 代码:
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false"/>
</androidx.core.widget.NestedScrollView>
我在 RecyclerView
中遇到问题。当我在 RV 中移动项目然后滚动时,看到一些项目重复了。
RecyclerView
会回收view.When你删除的数据,调用notifyItemChanged(pos)
或notifyDataSetChanged()
方法。
问题出在你的 notifyDataSetChanged()
上。
检查您是否正确使用它。
即:
private void parseJsonFeed(JSONArray response) {
for (int i = 0; i < response.length(); i++)
try {
JSONObject obj = response.getJSONObject(i);
MyData myData = new MyData();
myData.setContent_title(obj.getString("content_title"));
...
...
...
...
// adding content to array
homeList.add(myData);
} catch (JSONException e) {
e.printStackTrace();
}
//Notifying the adapter that data has been added or changed
//this must always be called else the recycler would not understand when to stop or start working.
recyclerViewAdapter.notifyDataSetChanged();
}
我知道它晚了但希望它能帮助别人。在您的适配器中覆盖这两个方法。
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}
我想我来晚了,但无论如何我会建议一种对我有用的方法,也许有人仍然面临这个问题..
因此,我在 nestedScrollView 中添加了我的 recyclerview,然后为我的 recyclerview 禁用了嵌套滚动。
使用此方法,nestedScrollView 将检测到滚动,并且 recyclerview 在滚动时停止复制项目。
这是我的 xml 代码:
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false"/>
</androidx.core.widget.NestedScrollView>