在recyclerview中的另一个动态recyclerview中添加动态数据

时间:2019-09-11 15:41:07

标签: android android-recyclerview

我正在尝试将recyclerview添加到另一个recyclerview。 类似于其Category RecyclerViews中的Sub Category Recyclerview。 问题是我在类别列表的最后一个项目中获取所有类别的子类别项目列表。

表示 猫1 猫2 catn(然后在此subcat1,subcat2 ....列表下方) 但是应该像cat1,然后在subcat1下面,依此类推...

这是我调用api并将适配器传递给recyclerview的MainActivity代码

private void getCategory() {

    StringRequest stringRequest = new StringRequest(Request.Method.GET, CategoryUrl, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

            try {
                JSONObject obj = new JSONObject(response);
                JSONArray jsonArray = obj.getJSONArray("DATA");
                Log.v("Cat Response", response.toString());

                for (int i = 0; i < jsonArray.length(); i++) {
                    DataModel Model = new DataModel();
                    JSONObject jsonObject = jsonArray.getJSONObject(i);

                    catId = jsonObject.getString("ID");
                    catTitle = jsonObject.getString("TITLE");

                    Model.setTITLE(catTitle);
                    Model.setID(catId);

                    catList.add(Model);
                    categoryAdaptor.getSubCategory(catId);

                }
                categoryAdaptor.notifyDataSetChanged();

            } catch (JSONException e) {
                System.out.print("Exception is :" + e.getMessage());

            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);


}

private void setCatAdaptor() {
    @SuppressLint("WrongConstant") LinearLayoutManager gridLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayout.VERTICAL, false);
    recyclerView1.setLayoutManager(gridLayoutManager); // set LayoutManager to RecyclerView
    //  call the constructor of CustomAdapter to send the reference and data to Adapter
    categoryAdaptor = new CategoryAdaptor(this, catList);
    recyclerView1.setAdapter(categoryAdaptor);

}

这是类别适配器

public class CategoryAdaptor extends RecyclerView.Adapter<CategoryAdaptor.MyViewHlder> {
private Activity activity;
private ArrayList<DataModel> list;
private SubCategoryAdaptor subcategoryAdaptor;
private ArrayList<SubCatModel> subCatList = new ArrayList<>();
private String SubCategoryUrl = "http://philiabeauty.com/reader-choice/controller-api.php?action=get_subcategory_by_category&id=";
private String Token = "&access_token=Ym9va19kZXRhaWxz";
private String imgRoot = "http://philiabeauty.com/reader-choice/";
private String subCatId, subCatTitle, subCatImage;

public CategoryAdaptor(Activity activity, ArrayList<DataModel> list) {
    this.activity = activity;
    this.list = list;
}

@NonNull
@Override
public MyViewHlder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.category_list_layout, viewGroup, false);
    MyViewHlder my = new MyViewHlder(view);

    return my;
}

@Override
public void onBindViewHolder(@NonNull MyViewHlder myViewHlder, int i) {

    final DataModel model = list.get(i);

    myViewHlder.catText.setText(model.getTITLE());
    myViewHlder.catId.setText(model.getID());


    setSubCatAdaptor(myViewHlder);
}

@Override
public int getItemCount() {
    return list.size();
}


public void getSubCategory(String ID) {

    String SubCategoryUrlFinal = SubCategoryUrl + ID + Token;
    StringRequest stringRequest = new StringRequest(Request.Method.GET, SubCategoryUrlFinal, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

            try {
                JSONObject obj = new JSONObject(response);
                JSONArray jsonArray = obj.getJSONArray("DATA");
                Log.v("SubCat Response", response.toString());

                for (int i = 0; i < 3; i++) {
                    SubCatModel Model = new SubCatModel();
                    JSONObject jsonObject = jsonArray.getJSONObject(i);

                    subCatId = jsonObject.getString("ID");
                    subCatTitle = jsonObject.getString("TITLE");
                    subCatImage = imgRoot + jsonObject.getString("IMG_SRC");
                    Model.setSubCatIMG(subCatImage);
                    Model.setSubCatTITLE(subCatTitle);
                    Model.setSubCatID(subCatId);

                    subCatList.add(Model);

                }
                subcategoryAdaptor.notifyDataSetChanged();

            } catch (JSONException e) {
                System.out.print("Exception is :" + e.getMessage());

            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
    RequestQueue requestQueue = Volley.newRequestQueue(activity);
    requestQueue.add(stringRequest);


}

private void setSubCatAdaptor(MyViewHlder myView) {
    Log.v("test", "test" );
    @SuppressLint("WrongConstant") LinearLayoutManager gridLayoutManager = new LinearLayoutManager(activity, LinearLayout.HORIZONTAL, false);
    myView.recyclerviewSubCat.setLayoutManager(gridLayoutManager); // set LayoutManager to RecyclerView
    //  call the constructor of CustomAdapter to send the reference and data to Adapter
    subcategoryAdaptor = new SubCategoryAdaptor(activity, subCatList);
    myView.recyclerviewSubCat.setAdapter(subcategoryAdaptor);

}

class MyViewHlder extends RecyclerView.ViewHolder {

    private TextView catText, viewAllBtn, catId, tag;
    private RecyclerView recyclerviewSubCat;

    public MyViewHlder(@NonNull View itemView) {
        super(itemView);
        catText = itemView.findViewById(R.id.cat_name);
        viewAllBtn = itemView.findViewById(R.id.viewAllBtn);
        catId = itemView.findViewById(R.id.cat_id);
        recyclerviewSubCat = itemView.findViewById(R.id.recyclerviewSubCat);

    }
}

}

这是子类别适配器

public class SubCategoryAdaptor extends RecyclerView.Adapter<SubCategoryAdaptor.MyViewHlder> {
private Activity activity;
private ArrayList<SubCatModel> list;

public SubCategoryAdaptor(Activity activity, ArrayList<SubCatModel> list) {
    this.activity = activity;
    this.list = list;
}

@NonNull
@Override
public MyViewHlder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.subcat_list, viewGroup, false);
    MyViewHlder my = new MyViewHlder(view);

    return my;
}

@Override
public void onBindViewHolder(@NonNull MyViewHlder myViewHlder, int i) {

    final SubCatModel model = list.get(i);

    Log.v("data", model.getSubCatTITLE());

    myViewHlder.subCatTitle.setText(model.getSubCatTITLE());
    myViewHlder.subCatId.setText(model.getSubCatID());
    String img = model.getSubCatIMG();

    Picasso.get()
            .load(img)
            .placeholder(R.drawable.ic_book_black_24dp)
            .fit()
            .into(myViewHlder.subCatImage);
}

@Override
public int getItemCount() {
    return list.size();
}


class MyViewHlder extends RecyclerView.ViewHolder {

    private TextView subCatTitle, subCatId, subCatTag;
    private ImageView subCatImage;

    public MyViewHlder(@NonNull View itemView) {
        super(itemView);
        subCatTitle = itemView.findViewById(R.id.subcat_name);
        subCatId = itemView.findViewById(R.id.subcat_id);
        subCatTag = itemView.findViewById(R.id.subcat_tag);
        subCatImage = itemView.findViewById(R.id.subcat_image);

    }
}

}

1 个答案:

答案 0 :(得分:2)

将子类别适配器声明更改为viewHolder,

help