RecyclerView在android的RecyclerView中加载了两次产品点击事件
我已经在片段中应用了recyclerView,在片段中我展示了api中运行良好的产品。当我单击产品并转到下一个片段时,我已经在recyclerView上执行了click listener。但当我转到前面的片段时,我的recycelerView会一次又一次地加载数据。我怎样才能避免这种情况。我想让它加载数据一次。 以下是我的代码xml代码:RecyclerView在android的RecyclerView中加载了两次产品点击事件,android,android-studio,android-fragments,android-recyclerview,Android,Android Studio,Android Fragments,Android Recyclerview,我已经在片段中应用了recyclerView,在片段中我展示了api中运行良好的产品。当我单击产品并转到下一个片段时,我已经在recyclerView上执行了click listener。但当我转到前面的片段时,我的recycelerView会一次又一次地加载数据。我怎样才能避免这种情况。我想让它加载数据一次。 以下是我的代码xml代码: <android.support.v7.widget.RecyclerView android:id="@+id/lsCategory"
<android.support.v7.widget.RecyclerView
android:id="@+id/lsCategory"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginLeft="2dp"
android:layout_marginTop="4dp"
android:layout_marginRight="2dp"
android:background="@color/colorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
这是大多数android开发人员面临的一个常见问题,可以通过以下方法解决。
1.首先,每次加载/调用此片段时,在向其添加数据之前清除类别对象。
例子。类别。清除();如果是列表
2.在适配器中,添加此setHasStableIds(true)在构造函数内部。它将停止复制列表中的数据。
3.覆盖适配器中的以下方法(IMP)
让我知道它是否有效 这是大多数android开发者面临的一个常见问题,可以通过以下方法解决。
1.首先,每次加载/调用此片段时,在向其添加数据之前清除类别对象。
例子。类别。清除();如果是列表
2.在适配器中,添加此setHasStableIds(true)在构造函数内部。它将停止复制列表中的数据。
3.覆盖适配器中的以下方法(IMP)
让我知道它是否有效 我希望这对你有用 在将数据添加到arraylist之前清除它,并在代码中尝试捕获之前添加。如下所示,并通知您的适配器
if(categories!=null){
categories.clear();
}
try {
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
final String name = object.getString("Name");
final int Id = object.getInt("ID");
categories.add(new Category(Id, name));
}
}catch(JSONException e) {
e.printStackTrace();
}
categoryAdapter = new CategoryAdapter(context, categories);
rvCategory.setLayoutManager(new
LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false));
rvCategory.setAdapter(categoryAdapter);
categoryAdapter.notifyDataSetChanged();
if(类别!=null){
类别。清除();
}
试一试{
对于(int i=0;i
我希望这对您有用
在将数据添加到arraylist之前清除它,并在代码中尝试捕获之前添加。如下所示,并通知您的适配器
if(categories!=null){
categories.clear();
}
try {
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
final String name = object.getString("Name");
final int Id = object.getInt("ID");
categories.add(new Category(Id, name));
}
}catch(JSONException e) {
e.printStackTrace();
}
categoryAdapter = new CategoryAdapter(context, categories);
rvCategory.setLayoutManager(new
LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false));
rvCategory.setAdapter(categoryAdapter);
categoryAdapter.notifyDataSetChanged();
if(类别!=null){
类别。清除();
}
试一试{
对于(int i=0;i
有两种方法:
第一:
如果您使用的是replace,那么还应该使用addtobackstack(null)方法。当从下一个片段返回到上一个片段时,它不会再次调用上一个片段onCreate方法。确保您正在onCreate方法中调用API
第二:
试着在片段中阅读关于setRetain(True)的内容,也许这会帮助您停止再次加载fragmentoncreate方法。确保您正在onCreate方法中调用API
希望这有帮助。快乐编码:)有两种方法:
第一:
如果您使用的是replace,那么还应该使用addtobackstack(null)方法。当从下一个片段返回到上一个片段时,它不会再次调用上一个片段onCreate方法。确保您正在onCreate方法中调用API
第二:
试着在片段中阅读关于setRetain(True)的内容,也许这会帮助您停止再次加载fragmentoncreate方法。确保您正在onCreate方法中调用API
希望这有帮助。快乐编码:)使用fragmentTransaction.add(containerWebId、fragment、tag);代替fragmentTransaction.replace(containerWebID、fragment、tag) 添加和替换之间的区别是: 替换删除现有片段并添加新片段。这意味着当您按下后退按钮时,被替换的片段将被创建,其onCreateView将被调用 然而,添加保留现有片段并添加新片段,这意味着现有片段将处于活动状态,并且不会处于“暂停”状态,因此,当按下后退按钮时,不会为现有片段调用CreateView(添加新片段之前存在的片段)
就片段的生命周期事件而言,替换时将调用onPause、onResume、onCreateView和其他生命周期事件,但添加时不会调用它们。使用fragmentTransaction.add(containerWebId、fragment、tag);代替fragmentTransaction.replace(containerWebID、fragment、tag) 添加和替换之间的区别是: 替换删除现有片段并添加新片段。这意味着当您按下后退按钮时,被替换的片段将被创建,其onCreateView将被调用 然而,添加保留现有片段并添加新片段,这意味着现有片段将处于活动状态,并且不会处于“暂停”状态,因此,当按下后退按钮时,不会为现有片段调用CreateView(添加新片段之前存在的片段)<
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public long getItemId(int position){
return position;
}
@Override
public int getItemCount() {
return category.size();
}
if(categories!=null){
categories.clear();
}
try {
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
final String name = object.getString("Name");
final int Id = object.getInt("ID");
categories.add(new Category(Id, name));
}
}catch(JSONException e) {
e.printStackTrace();
}
categoryAdapter = new CategoryAdapter(context, categories);
rvCategory.setLayoutManager(new
LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false));
rvCategory.setAdapter(categoryAdapter);
categoryAdapter.notifyDataSetChanged();