在 RecyclerView 的单个界面中使用多个 onClick 方法

Posted

技术标签:

【中文标题】在 RecyclerView 的单个界面中使用多个 onClick 方法【英文标题】:Using multiple onClick methods in a single interface for RecyclerView 【发布时间】:2021-01-11 09:53:15 【问题描述】:

上下文:

我已经在我的待办事项列表应用中实现了一个 RecyclerView。

我希望能够对 RecyclerView 中的项目使用各种 onClick 方法,因此我创建了一个名为 onTaskListener 的接口。

这个接口有两个方法存根,一个用于onClick,一个用于onLongClick。在我的 ViewHolder 中,我实现了 onClick() 和 onLongClick() 方法,它们只是将控制权传递给我的 onTaskClickListener()。

在我的适配器中,我创建了一个 onTaskClickListener()。 然后在我的主要活动中,我在 onTaskClickListener() 中实现方法。

我的问题是,虽然我的 onTaskClick() 运行良好,但我的 onTaskLongClick 似乎根本无法运行。我设置 RecyclerView/Adapter/ViewHolder/ViewModel 模式的方式有问题吗?

问题:如果我实现界面的方式不对,如何在一个界面中包含多种类型的点击事件?

这里是每个文件的相关内容(我知道很多,代码墙很抱歉):

onTaskClickListener.java:

public interface OnTaskListener 
    void onTaskClick(int position); // Interfaces are implicitly abstract
    void onTaskLongClick(int position);

itemViewHolder.java:

public class itemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener 

    View itmView; // This is the general view
    TextView txtView; // This is the specific text view that shows up as a singular task in the list of to-do tasks
    OnTaskListener onTaskListener; // Create an OnTaskListener inside our view holder which allows the view holder to realize it's been clicked

    public itemViewHolder(@NonNull View itemView, OnTaskListener inputOnTaskListener) 
        super(itemView);
        itmView = itemView;
        txtView = itemView.findViewById(R.id.txtTask);
        this.onTaskListener = inputOnTaskListener; // Take an onTaskListener that is passed into the object and store it internally
        itemView.setOnClickListener(this); // passes the View.OnClickListener context to the itemView via "this"
    

    @Override
    public void onClick(View view) 
        onTaskListener.onTaskClick(getAdapterPosition()); // This says that whenever we register a click event, we pass the logic onto the taskClick event
    

    @Override
    public boolean onLongClick(View view) 
        onTaskListener.onTaskLongClick(getAdapterPosition()); // This says that whenever we register a longClick event, we pass the logic onto the taskClick event
        return true; // This means that we have successfully consumed the long click event. No other click events will be notified
    

dataAdapter.java

public class dataAdapter extends RecyclerView.Adapter<itemViewHolder> 

    List<taskItem> taskItemList;
    private OnTaskListener onTaskListener;

    public dataAdapter(List<taskItem> inputTaskItemList, OnTaskListener inputOnTaskListener)
        this.taskItemList = inputTaskItemList;
        this.onTaskListener = inputOnTaskListener;
    

    @NonNull
    @Override
    public itemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
        View localView = LayoutInflater.from(parent.getContext()).inflate(R.layout.taskholder, parent, false); //Don't even know what this line does, it's all so over my head
        return new itemViewHolder(localView, onTaskListener); // Return an instance of whatever we made directly above this line
    

    @Override
    public void onBindViewHolder(@NonNull itemViewHolder holder, final int position) 
        holder.txtView.setText(taskItemList.get(position).taskTitle);
        // Look inside our ViewModel and get the text for this specific instance of the ViewModel, which corresponds to the current position
    

    @Override
    public int getItemCount() 
        return taskItemList.size();
    

MainActivity.java

public class MainActivity extends AppCompatActivity implements OnTaskListener
    private RecyclerView taskList; // Creates a RecyclerView to hook up to our RecyclerView widget in the UI
    private dataAdapter localAdapter; // Instantiates our custom adapter class
    List<taskItem> myItems; // Stores the items in a list of taskItem's
    private RecyclerView.LayoutManager localLayoutManager; // God knows what this does :(

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

        taskList = findViewById(R.id.taskList); // Connects our list from UI to recycler view code
        localLayoutManager = new LinearLayoutManager(this); // assigns our localLayoutManager to an actual Layout Manager
        taskList.setLayoutManager(localLayoutManager); // connecting our layout manager to our recycler view
        taskList.setHasFixedSize(true);

        myItems = new ArrayList<>(); // Now we FINALLY make our to-do list and populate it with actual tasks
        myItems.add(new taskItem("groceries"));
        myItems.add(new taskItem("practice bjj"));

        localAdapter = new dataAdapter(myItems, this); // Pass the to do list to the adapter so it can feed it to the recycler view
        taskList.setAdapter(localAdapter); // Lastly set the recycler view's adapter to the one we made above
        
    

    @Override
    public void onTaskClick(int position) 
        taskItem currentTask = myItems.get(position);
        if(!(currentTask.taskTitle.startsWith("Done: "))) // Logic that marks a task as done on tap
            currentTask.taskTitle = "Done: " + currentTask.taskTitle;
            //logic that moves the tapped item to bottom of list
            myItems.remove(position);
            myItems.add(myItems.size(), currentTask);
            localAdapter.notifyItemMoved(position, myItems.size());
        
        else if(myItems.get(position).taskTitle.startsWith("Done: ")) // Logic for if user taps a task already marked "done"
            currentTask.taskTitle = currentTask.taskTitle.replaceFirst("Done: ", "");
            myItems.set(position, currentTask); // Remove prefix
            localAdapter.notifyItemChanged(position);
            myItems.remove(position);
            myItems.add(0, currentTask);
        
        localAdapter.notifyDataSetChanged(); // Let the activity know that the data has changed
    

    @Override
    public void onTaskLongClick(int position)  // This branch deals with deleting tasks on long click
        myItems.remove(position);
        localAdapter.notifyItemRemoved(position); // Item has been deleted
    

【问题讨论】:

【参考方案1】:

你永远不会打电话给setOnLongClickListener()

public itemViewHolder(@NonNull View itemView, OnTaskListener inputOnTaskListener) 
    super(itemView);
    itmView = itemView;
    txtView = itemView.findViewById(R.id.txtTask);
    this.onTaskListener = inputOnTaskListener; // Take an onTaskListener that is passed into the object and store it internally
    itemView.setOnClickListener(this); // passes the View.OnClickListener context to the itemView via "this"

    // Add this line
    itemView.setOnLongClickListener(this); // passes the View.OnLongClickListener context to the itemView via "this"

或者,您可以通过内联整个 OnLongClickListener 来完全避免通过 this(对于 OnClickListener 也是如此):

itemView.setOnLongClickListener(new View.OnLongClickListener() 
    @Override
    public boolean onLongClick(View v) 
        onTaskListener.onTaskLongClick(getAdapterPosition()); // This says that whenever we register a longClick event, we pass the logic onto the taskClick event
        return true; // This means that we have successfully consumed the long click event. No other click events will be notified
    
 );

从而避免让您的itemViewHolder 类实现OnLongClickListener 接口并避免忘记调用setOnLongClickListener()

【讨论】:

我不敢相信我错过了这个。谢谢你。如果发生这种情况,有什么方法可以让 android Studio 警告我吗?或者这只是一个小心的问题? OnClickListener 实现直接传递给setOnClickListener() 方法比传入this 更为常见。这样,侦听器的设置和侦听器本身就不会像在您的代码中那样解耦。 这不会将我的 onclicklistener 的实现移动到我的视图持有者中吗?如果我错了,请纠正我,但我认为这被认为是不好的做法? 您传递给setOnClickListener 的匿名OnClickListener 仍会调用您的onTaskClick,就像现在一样。你只是不会通过this重定向。 我在答案中添加了一个示例。

以上是关于在 RecyclerView 的单个界面中使用多个 onClick 方法的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Firebase 中将多个 Recyclerview 设置为单个适配器(多视图类型)?

我们可以根据特定的 recyclerview 项目点击通过单个界面传递多种类型的对象(项目)吗

在 ViewPager 的多个选项卡中使用单个片段

在 RecyclerView 项目上添加多个 onClickListener

使用 AVFoundation 在单个界面中捕获多个图像

notifyItemChanged(int position) 更新 RecyclerView 中的多个项目