There is a recyclerview, each element of which contains a pair of text fields and a button. With the help of the adapter this whole thing is filled:

public class RVAdapterStart extends RecyclerView.Adapter<RVAdapterStart.TaskViewHolder> { public static class TaskViewHolder extends RecyclerView.ViewHolder { RelativeLayout relativeLayout; private TextView title; private TextView status; private Button button; TaskViewHolder(View view) { super(view); relativeLayout = (RelativeLayout) view.findViewById(R.id.frameLayout); title = (TextView) view.findViewById(R.id.title); status = (TextView) view.findViewById(R.id.status); button = (Button) view.findViewById(R.id.tasks_finish); } } private List<Task> tasksStart; RVAdapterStart() { } @Override public void onAttachedToRecyclerView(RecyclerView recyclerView) { super.onAttachedToRecyclerView(recyclerView); } @Override public void onBindViewHolder(TaskViewHolder holder, final int position) { holder.title.setText(tasksStart.get(position).getTitle()); holder.status.setText(tasksStart.get(position).getStatus()); } @Override public int getItemCount() { return tasksStart.size(); } @Override public TaskViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_tasks_start, viewGroup, false); TaskViewHolder tvh = new TaskViewHolder(view); return tvh; } 

How to get access to these elements from the activation (you need to hang the listeners on the buttons)?

  • one
    But is it not better to lead the listeners immediately when they are created in the onCreateViewHolder method? - pavel
  • Perhaps, but the situation is more complicated, there is another recycler and they both interact with each other, i.e. when you click on the button in the first recycler, the element jumps to the second, and in the second there is a cancel button and when you click on it, the element must jump back to the first. So I can't figure out how to do it - Jaive

2 answers 2

If the markup elements are the same in both lists and represent the same data, then you only need to change the list of data displayed by the adapters.

Those.

  1. from one we delete data, we add it to another.
  2. On both we call the methods that notify about deleting / adding data at a certain position.
  3. Adapters themselves are redrawn accordingly.
  4. At the same time, if click listeners are assigned directly in the onBindViewHolder method, then when you add a new item, a click listener will be assigned to it.

    You should create a listener (listener) in the Activity and then transfer it to the adapter. Then for each button you create your own OnClickListener , from which the appropriate method will be called in the first listener.