嵌套 RecyclerView OnclickListener

Nested RecyclerView OnclickListener

我用两个适配器(MainRecyclerviewAdapter 和 ChildRecyclerviewAdapter)嵌套了 RecyclerView(MainRecyclerview 和 ChildRecyclerview),我创建了一个接口来处理 childRecyclerview 的点击,但我只能访问 MainRecyclerviewAdapter 中的点击。 我想知道如何处理 MainActivity 中 childRecyclerview 中项目的点击,谢谢。

这是我的代码:

//主活动

public class MainActivity 扩展 AppCompatActivity {

List<Section> sectionList = new ArrayList<>();
RecyclerView mainRecyclerView;
LinearLayoutManager layoutManager;
MainRecyclerAdapter mainRecyclerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile_properties);

    initData();
    initRecyclerView();



}

}

//ChildRecyclerAdapter

public class ChildRecyclerAdapter 扩展 RecyclerView.Adapter {

private final List<String> items;
private OnItemClickListener mListener;

public ChildRecyclerAdapter(List<String> items) {
    this.items = items;
}

public void setOnItemClickListener(OnItemClickListener listener) {
    mListener = listener;
}

@NonNull
@Override
public ChildViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row, parent, false);
    return new ChildViewHolder(view, mListener);
}

@Override
public void onBindViewHolder(@NonNull ChildViewHolder holder, int position) {
    holder.itemTextView.setText(items.get(position));
}

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


public class ChildViewHolder extends RecyclerView.ViewHolder {

    TextView itemTextView;

    ChildViewHolder(final View itemView, OnItemClickListener listener) {
        super(itemView);
        itemTextView = itemView.findViewById(R.id.txt_item_view);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    int position = getBindingAdapterPosition();
                    if (position != RecyclerView.NO_POSITION) {
                        String item = items.get(position);
                        listener.onItemClick(item);
                    }
                }
            }
        });
    }
}

}

//MainRecyclerAdapter

public class MainRecyclerAdapter 扩展 RecyclerView.Adapter {

List<Section> sectionList;

public MainRecyclerAdapter(List<Section> sectionList) {
    this.sectionList = sectionList;
}

@NonNull
@Override
public MainViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View view = inflater.inflate(R.layout.section_row, parent, false);
    return new MainViewHolder(view);
}

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

    Section section = sectionList.get(position);
    String sectionName = section.getSectionName();
    List<String> items = section.getSectionItems();

    holder.sectionNameTextView.setText(sectionName);
    ChildRecyclerAdapter childRecyclerAdapter = new ChildRecyclerAdapter(items);
    holder.recyclerChild.setAdapter(childRecyclerAdapter);
    childRecyclerAdapter.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(String item) {
            // TODO: 07/02/2022 handle click

        }
    });

}

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


public static class MainViewHolder extends RecyclerView.ViewHolder {

    TextView sectionNameTextView;
    RecyclerView recyclerChild;

    public MainViewHolder(final View itemView) {
        super(itemView);

        sectionNameTextView = itemView.findViewById(R.id.txt_section_name);
        recyclerChild = itemView.findViewById(R.id.child_recyclerview);
        LinearLayoutManager layoutManager = new LinearLayoutManager(itemView.getContext());
        recyclerChild.setLayoutManager(layoutManager);

    }
}

}

//OnItemClickListener

public 接口 OnItemClickListener {

void onItemClick(String item);

}

你可以简单地多做一个接口,在嵌套适配器的onclick方法中调用它。

因此,当您单击任何适配器项时,它会将数据从适配器传递到 activity。

像这样:

1.在 activity 或片段中创建用于获取子 recyclerview 项目点击的界面。

首先,您需要从这里更改您的子适配器的点击侦听器

public interface OnItemClickListener {
    void onItemClick(String item);
}

对此:

public interface OnItemClickListener {
    void onItemClick(int childPosition);
}

然后传递子 Recyclerview 的适配器位置而不是字符串值。

现在,制作一个新界面,让 activity 或片段中的父项和子项都获得点击。

public interface RvItemClickListener {
    void onChildItemClick(int parentPosition, int childPosition, String item);
}

2。定义侦听器并在父 recyclerview 适配器中单击子项时调用它:

RvItemClickListener rvItemClickListener;
    
public void setRvItemClickListener(RvItemClickListener rvItemClickListener){
    this.rvItemClickListener = rvItemClickListener;
}

3。在子项目点击时调用它:

childRecyclerAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(int childPosition) {
                    String item = items.get(childPosition);
                    rvItemClickListener.onChildItemClick(getAdapterPosition(), childPosition, item);
                  }
               });

4.最后一步是像往常一样覆盖 activity 或片段中的方法:

@Override
public void onItemClick(int parentPosition, int childPosition, String item) {

}

我希望这足以让你理解我想说的话。

这只是一个例子。请根据您的需要进行更改。

我建议您避免嵌套回收站视图。您可以向适配器添加不同的视图以区分单个回收器视图中的不同视图

class RecyclerViewAdapter(context: Context, list: ArrayList<Data>) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    companion object {
        const val VIEW_TYPE_ONE = 1
        const val VIEW_TYPE_TWO = 2
    }

    private val context: Context = context
    var list: ArrayList<Data> = list

    private inner class View1ViewHolder(itemView: View) :
        RecyclerView.ViewHolder(itemView) {
        var message: TextView = itemView.findViewById(R.id.textView)
        fun bind(position: Int) {
            val recyclerViewModel = list[position]
            message.text = recyclerViewModel.textData
        }
    }

    private inner class View2ViewHolder(itemView: View) :
        RecyclerView.ViewHolder(itemView) {
        var message: TextView = itemView.findViewById(R.id.textView)
        fun bind(position: Int) {
            val recyclerViewModel = list[position]
            message.text = recyclerViewModel.textData
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (viewType == VIEW_TYPE_ONE) {
            return View1ViewHolder(
                LayoutInflater.from(context).inflate(R.layout.item_view_1, parent, false)
            )
        }
        return View2ViewHolder(
            LayoutInflater.from(context).inflate(R.layout.item_view_2, parent, false)
        )
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (list[position].viewType === VIEW_TYPE_ONE) {
            (holder as View1ViewHolder).bind(position)
        } else {
            (holder as View2ViewHolder).bind(position)
        }
    }

    override fun getItemViewType(position: Int): Int {
        return list[position].viewType
    }
}

这使得操作点击侦听器和测试代码变得更加容易