【发布时间】:2018-09-26 07:43:49
【问题描述】:
我已在片段中应用了 recyclerView,我在片段中展示了运行良好的 api 产品。当我单击产品并转到下一个片段时,我在 recyclerView 上执行了单击侦听器。但是当我转到上一个片段时,我的 recyclerView 会一次又一次地加载数据。我怎么能避免这种情况。我希望它加载一次数据。 这是我的代码 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);
}
});
}
}
【问题讨论】:
-
可以添加以前的片段更改代码吗?
-
在回收视图代码所在的位置显示您的完整片段代码!
-
请您解释一下
-
在 for 循环中设置
Adapter可能会有所帮助?在那之后,adapter.notifydatachange();另外,add(new Category(Id, name));也可能会创建另一个项目。这是可疑的。 -
显示完整的片段代码。你有共享回收视图代码。显示回收视图正在加载数据的片段的所有代码。
标签: android android-studio android-fragments android-recyclerview