如何在另一个RecyclerView的项目中实现RecyclerView

时间:2019-09-16 09:05:48

标签: android android-recyclerview

我应该实现一个RecyclerView,它的项包含一个TextView和另一个内部水平RecyclerView

我开始考虑内部RecyclerView与其他View相似。当我尝试将上下文传递给LinearLayoutManager()RecyclerView.setAdapter()时发生了问题。

问题是: 我无法传递上下文 ,因为我试图在第一个RecyclerView适配器内部传递上下文

我无法传递上下文,也不知道这种方法是否是最佳方法,或者最终是否可以使用。

这是我的代码:


public class TheFirstAdapter extends RecyclerView.Adapter<TheFirstAdapter.TheFirstViewHolder> {

    private Context context;
    private List<FirstItemModel> firstItemModelList;
    private List<SecondItemModel> secondItemModelList;
    SecondItemModel secondItemModel;
    TheSecondAdapter theSecondAdapter;

    public TheFirstAdapter(Context context, List<FirstItemModel> firstItemModel) {
        this.context = context;
        this.firstItemModelList = firstItemModelList;
    }

    @NonNull
    @Override
    public TheFirstViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.add_routine_list_item, parent,
                false);
        return new TheFirstAdapter.TheFirstViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull TheFirstViewHolder holder, int position) {

//there is an error in this line:
////java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference

//////the solution: secondItemModelList= new ArrayList<>(); 
            for (int i = 1; i < 5; i++) {
            secondItemModel = new AddRoutineRoutines("Morning", "#9697D6");
            secondItemModelList.add(secondItemModel);
        }

        FirstItemModel firstItemModel = firstItemModelList.get(position);
        holder.textView.setText(addRoutineItemModel.getFirstItemText());
        holder.theSecondRecyclerView.setLayoutManager(new LinearLayoutManager(context));
        TheSecondAdapter theSecondAdapter = new TheSecondAdapter(context, secondItemModelList);
        holder.theSecondRecyclerView.setAdapter();
    }

    @Override
    public int getItemCount() {
        if (firstItemModelList == null) return 0;
        return firstItemModelList.size();
    }

    class TheFirstViewHolder extends RecyclerView.ViewHolder {

        TextView textView;
        RecyclerView theSecondRecyclerView;

        public TheFirstViewHolder(@NonNull View itemView) {

            super(itemView);
            textView = itemView.findViewById(R.id.first_item_text);
            theSecondRecyclerView = itemView.findViewById(R.id.the_second_recycler_view);
        }
    }
}

Edit1: 我通过了上下文,但是上面仍然显示了一个问题。

Edit2: 问题是我没有初始化secondItemModelList

*在讨论之后,此代码有效:*


public class TheFirstAdapter extends RecyclerView.Adapter<TheFirstAdapter.TheFirstViewHolder> {

    private Context context;
    private List<FirstItemModel> firstItemModelList;
    private List<SecondItemModel> secondItemModelList;
    SecondItemModel secondItemModel;

    public TheFirstAdapter(Context context, List<FirstItemModel> firstItemModel) {
        this.context = context;
        this.firstItemModelList = firstItemModelList;
    }

    @NonNull
    @Override
    public TheFirstViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.add_routine_list_item, parent,
                false);
        return new TheFirstAdapter.TheFirstViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull TheFirstViewHolder holder, int position) {

        FirstItemModel firstItemModel = firstItemModelList.get(position);
        holder.textView.setText(addRoutineItemModel.getFirstItemText());
        holder.updateView(secondItemModelList);
    }

    @Override
    public int getItemCount() {
        if (firstItemModelList == null) return 0;
        return firstItemModelList.size();
    }

    class TheFirstViewHolder extends RecyclerView.ViewHolder {

        TextView textView;
        RecyclerView theSecondRecyclerView;

        public TheFirstViewHolder(@NonNull View itemView) {

            super(itemView);
            textView = itemView.findViewById(R.id.first_item_text);
            theSecondRecyclerView = itemView.findViewById(R.id.the_second_recycler_view);
        }

        public updateView(List<SecondItemModel> secondItemModelList) {
            secondItemModelList = new ArrayList<>();

            for (int i = 1; i < 5; i++) {
                secondItemModel = new SecondItemModel("Morning", "#9697D6");
                secondItemModelList.add(secondItemModel);
            }

            TheSecondAdapter theSecondAdapter = new TheSecondAdapter(context, secondItemModelList);
            LinearLayoutManager layoutManager
                    = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
            theSecondRecyclerView.setLayoutManager(layoutManager);
            theSecondRecyclerView.setAdapter(theSecondAdapter);
        }
    }
}

1 个答案:

答案 0 :(得分:-1)

您发送给构造函数LinearLayoutManager的arg的上下文是什么? 如果我了解您的问题。您正在使用父级的上下文。请使用itemView.context

更新:

您的代码非常糟糕。

  1. 请勿在{{1​​}}方法中创建适配器。
  2. 请勿在{{1​​}}方法中设置/删除数据。
  3. bindView仅用于绑定数据以供查看。只有那样。

ViewHolder应该是这样的:

bindView

BindView应该是这样的:class TheFirstViewHolder extends RecyclerView.ViewHolder { TextView textView; RecyclerView theSecondRecyclerView; TheFirstAdapter adapter; public TheFirstViewHolder(@NonNull View itemView) { super(itemView); textView = itemView.findViewById(R.id.first_item_text); theSecondRecyclerView = itemView.findViewById(R.id.the_second_recycler_view); } public void updateView(....) { //create new adapter if need. //set data for adapter if need ... } }