简体   繁体   English

将onClick添加到RecyclerView项的一部分

[英]Adding an onClick to a part of a RecyclerView item

I've been working on an online shop type of application, and I've hit a bump: I've been tasked to add a favorites system, but I can't figure out how to enable pressing a button that's part of the RecyclerView item to add it to favorites. 我一直在从事在线商店类型的应用程序的开发工作,但遇到了一个麻烦:我受命添加一个收藏夹系统,但是我不知道如何启用RecyclerView中的按钮将其添加到收藏夹的项目。 (In this case, the heart, which is supposed to turn to a full heart when clicked) (在这种情况下,单击时应该变成完整的心脏) RecyclerView的图像

Add a boolean value for favourite in your list . 为列表中的收藏夹添加一个布尔值。 Initially , keep it false . 最初,将其保留为假。 You need to have two drawables , one for selected state and another for unselected state . 您需要有两个可绘制对象,一个用于选定状态,另一个用于未选定状态。

In your onBindViewHolder , set the drawable on runtime on the basis of above condition . 在您的onBindViewHolder中,根据上述条件在运行时设置drawable。

 if(list.isfav)
 { 
   holder.ivHeart.setImageDrawable(ContextCompat.getDrawable(context,(R.drawable.selected));
  }else{
   holder.ivHeart.setImageDrawable(ContextCompat.getDrawable(context,(R.drawable.unselected));
  }

Put onClick on this ivHeart eg: 放在此ivHeart上,例如:

holder.ivHeart.setOnClickListener(v -> {
                if(list.isfav) {
                 list[adapterPosition].isfav = false;
                }else{
               list[adapterPosition].isfav = true;
                }
               notifyItemChanged(adapterPosition);
           });

Dont forget to notify the item while changing item . 更改项目时不要忘记通知该项目。

In your RecyclerView adapter's onBindViewHolder() method, add click listener to your view and change the drawable programmatically. 在RecyclerView适配器的onBindViewHolder()方法中,将Click侦听器添加到视图中,并以编程方式更改可绘制对象。

The code will be something like this 该代码将是这样的

@Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);

        holder.your_like_imageview.setOnClickListener{
        holder.your_like_imageview.setImageDrawable(ContextCompat.getDrawable(context,R.drawable.something_else));
       };



    }

I'm assuming you're using an ImageView for the heart. 我假设您对心脏使用ImageView。 What you can do is set a click listener on that ImageView and process that click. 您可以做的是在该ImageView上设置一个点击侦听器并处理该点击。

An ideal way to do this would be to use an interface to handle click events that you pass to the Adapter. 一种理想的方法是使用接口来处理传递给适配器的单击事件。

However you could do something like this in the onBindViewHolder method: 但是,您可以在onBindViewHolder方法中执行以下操作:

imageView.setOnClickListener { // depending your logic change the tint for the icon or the drawable onClick(data[position], addToWishlist) notifyDataSetChanged() }

The onClick method will receive the particular item and a flag to add or remove it from the wishlist: onClick方法将收到特定项目和一个标志,以将其添加到愿望清单或从愿望清单中删除:

fun onClick(data: Data, addToWishlist: Boolean) { // you can perform the addition/ deletion from the wishlist here }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM