回收器视图正在加载两次产品点击事件在回收器查看在安卓中



我已经在片段中应用了回收器视图,其中我显示了来自 API 的产品,工作正常。 我已经在回收器视图上执行了单击侦听器,当我单击产品并转到下一个片段时。但是当我转到上一个片段时,我的 recycelerView 一次又一次地加载数据。我怎么能避免这种情况。我想让它加载一次数据。 这是我的代码 XML 代码:

<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" />

这是我的回收器查看代码:

public class HomeFragment extends Fragment implements View.OnClickListener {
private List<Category> categories = new ArrayList<>();
private CategoryAdapter categoryAdapter;
public void getInfo(View view){
lsCategory =  view.findViewById(R.id.lsCategory);
}
public void setInfo(){
getData();
}
public void getData(){
final StringRequest request = new StringRequest(Request.Method.GET, URLs.homeMainURL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//categories
JSONArray categoryArray = jsonObject.getJSONArray("ListCategories");
setCategoryAdapter(categoryArray);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
VolleySingleton.getInstance(context).addToRequestQueue(request);
}
private void setCategoryAdapter(JSONArray array) {
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);
lsCategory.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false));
lsCategory.setAdapter(categoryAdapter);
}

这是我的类别适配器

@Override
public void onBindViewHolder(@NonNull CategoryAdapter.ViewHolder holder, int position) {
holder.setData(categories.get(position));
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView textViewItemName;
private int Id ;
public ViewHolder(View itemView) {
super(itemView);
textViewItemName = itemView.findViewById(R.id.tvCategoris);
}
public void setData(final Category category){
textViewItemName.setText(category.getCategoryName());
Id = category.getId();
textViewItemName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ExpandableCategoryList fragment = new ExpandableCategoryList();
Bundle bundle = new Bundle();
bundle.putInt("Id",Id);
fragment.setArguments(bundle);
((MainActivity)context).replaceFragment(fragment,
"TAG",null,false);
}
});
}
}

我希望这对你有用。

在将数据添加到数组列表之前清除数组列表,并在尝试在代码中捕获之前添加。 如下所示并通知您的适配器。

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();

使用 fragmentTransaction.add(containerViewId, fragment, tag(;而不是fragmentTransaction.replace(containerViewId, fragment, tag(;

添加替换之间的区别是:

替换删除现有片段并添加新片段。这意味着当您按下后退按钮时,将被替换的片段创建并调用其onCreateView。

add会保留现有片段并添加新片段,这意味着现有片段将处于活动状态,并且它们不会处于"暂停"状态,因此当按下后退按钮时,不会为现有片段调用 CreateView(添加新片段之前存在的片段(。

就片段的生命周期事件而言,暂停时,恢复,创建视图和其他生命周期事件将在替换时调用,但在添加时不会调用它们。

这是大多数Android开发人员面临的常见问题,可以通过以下方法解决。
1. 首先,每次加载/调用此片段时清除类别对象,然后再向其添加数据。
例。类别.清除((;如果是列表
2。在你的适配器中,在构造函数中添加这个setHasStableIds(true(。它将停止复制列表中的数据。
3. 覆盖适配器(IMP(中的以下方法

@Override
public int getItemViewType(int position) {
return position;
}
@Override
public long getItemId(int position){
return position;
}
@Override
public int getItemCount() {
return category.size();
}

让我知道它是否有效!

有两种方法:

第一:

如果您使用的是替换,那么您也应该使用它的AddToBackStack(null(方法。从下一个片段返回到上一个片段时,它不会再次调用上一个片段onCreate方法。确保在创建方法中调用 API。

第二:

尝试在片段中阅读有关setRetain(True(的信息,也许它可以帮助您再次停止加载片段oncreate方法。确保在创建方法中调用 API。

希望这有帮助。快乐编码:)

最新更新