F
F
Folsout2018-02-01 12:24:47
Java
Folsout, 2018-02-01 12:24:47

How to properly use RecyclerView in RecyclerView?

In fact, the question is how to properly use RecyclerView in RecyclerView, because with my approach, the elements, instead of being loaded into each list element, are displayed in one element. and the next one is empty.

public class CategoryRecyclerAdapter extends RecyclerView.Adapter<CategoryRecyclerAdapter.CategoryViewHolder> {
    private List<CategoryReference> categoryReferences;
    private Context context;


    public CategoryRecyclerAdapter(List<CategoryReference> categoryReferences, Context context) {
        this.categoryReferences = categoryReferences;
        this.context = context;
    }

    @Override
    public CategoryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler_reference, parent, false);
        CategoryViewHolder categoryViewHolder = new CategoryViewHolder(view);
        return categoryViewHolder;
    }

    @Override
    public void onBindViewHolder(CategoryViewHolder holder, int position) {
        holder.categoryName.setText(categoryReferences.get(position).getCategoryName());
        LinearLayoutManager horizontalLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
        holder.recycleItemReference.setLayoutManager(horizontalLayoutManager);
        holder.recycleItemReference.setAdapter(new ReferenceRecyclerAdapter(categoryReferences.get(position).getReferencesList()));

    }

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

    public static class CategoryViewHolder extends RecyclerView.ViewHolder{
        TextView categoryName;
        RecyclerView recycleItemReference;
        public CategoryViewHolder(View itemView) {

            super(itemView);
            categoryName = itemView.findViewById(R.id.tv_category_name);
            recycleItemReference = itemView.findViewById(R.id.reference_pager);

        }
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question