【问题标题】:Set onClickListener to multiple elements of RecyclerView设置 onClickListener 为 RecyclerView 的多个元素
【发布时间】:2017-04-29 13:33:35
【问题描述】:

我正在尝试使用自定义项目创建 Recycleview,如果我单击项目的 ImageView,它会做一件事(更改图像并选择项目),如果我单击项目内的其他任何位置,它做另一件事(打开新活动)

到目前为止,这是我的代码:

item_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="2dp"
    card_view:cardCornerRadius="4dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp">

        <ImageView
            android:id="@+id/Category"
            android:scaleType="fitCenter"
            android:layout_centerVertical="true"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginRight="25dp"/>

        <TextView
            android:id="@+id/Title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@id/Category"
            android:layout_marginBottom="2dp"
            android:text="Title" />

        <TextView
            android:id="@+id/Date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/Title"
            android:layout_toRightOf="@id/Category"
            android:paddingLeft="5dp"
            android:text="Date"/>

        <TextView
            android:id="@+id/Value"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:text="Value"/>

    </RelativeLayout>


</android.support.v7.widget.CardView>

listitem_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <com.inducesmile.androidmusicplayer.view.ItemLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</FrameLayout>

ItemViewHolder.java

    public class ItemViewHolder extends RecyclerView.ViewHolder {

        public ImageView category;
        public TextView title;
        public TextView date;
        public TextView value;
        public ImageView image;

        public ItemViewHolder(View itemView, ImageView category, TextView title, TextView date, TextView value, ImageView image ) {
            super(itemView);
            this.category = category;
            this.title = title;
            this.date = date;
            this.value = value;
            this.image = image;
        }

        public ItemViewHolder(View itemView) {
            super(itemView);

            category = (ImageView)itemView.findViewById(R.id.Category);
            title = (TextView)itemView.findViewById(R.id.Title);
            date = (TextView)itemView.findViewById(R.id.Date);
            value = (TextView)itemView.findViewById(R.id.Value);
        }
    }

ItemAdapter.java
public class ItemAdapter extends RecyclerView.Adapter<ItemViewHolder> {

    private Context context;
    private List<ItemObject> allItem;

    public ItemAdapter(Context context, List<ItemObject> allItem) {
        this.context = context;
        this.allItem = allItem;
    }

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

    @Override
    public void onBindViewHolder(ItemViewHolder holder, int position) {
        ItemObject items = allItem.get(position);
        //set imageview corresponding to category
        switch (items.getItemCategory()){
            case "Other": holder.category.setImageResource(R.drawable.ic_other ); break;
            case "Bills": holder.category.setImageResource(R.drawable.ic_bills ); break;
            case "Business": holder.category.setImageResource(R.drawable.ic_business ); break;
            case "Clothes": holder.category.setImageResource(R.drawable.ic_clothes ); break;
            case "Commute": holder.category.setImageResource(R.drawable.ic_commute ); break;
            case "Entertainment": holder.category.setImageResource(R.drawable.ic_entertainment ); break;
            case "Food": holder.category.setImageResource(R.drawable.ic_food ); break;
            case "Groceries": holder.category.setImageResource(R.drawable.ic_groceries ); break;
            case "Health": holder.category.setImageResource(R.drawable.ic_health ); break;
            case "Investment": holder.category.setImageResource(R.drawable.ic_investment ); break;
            case "Salary": holder.category.setImageResource(R.drawable.ic_salary ); break;
            case "Travel": holder.category.setImageResource(R.drawable.ic_travel ); break;
            default: holder.category.setImageResource(R.drawable.ic_default ); break;
        }
        holder.title.setText(items.getItemTitle());
        holder.date.setText(String.valueOf(items.getItemDate()));
        holder.value.setText( String.valueOf(items.getItemValue()));
    }

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

我不确定下一步该怎么做,我会寻求任何帮助。 提前致谢!

【问题讨论】:

    标签: android android-recyclerview onclicklistener


    【解决方案1】:

    处理它的最佳方法是将 OnClickListener 实现到您的 ViewHolder 类并在公共构造函数中设置侦听器。无需在运行时多次调用的 onBindViewHolder 中分配监听器。即使您需要在某些情况下在运行时启用禁用,您也可以在侦听器中添加 if/else 以阻止该操作。

    public class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    
        public ImageView category;
        public TextView title;
        public TextView date;
        public TextView value;
        public ImageView image;
    
        public ItemViewHolder(View itemView) {
            super(itemView);
    
            category = (ImageView)itemView.findViewById(R.id.Category);
            title = (TextView)itemView.findViewById(R.id.Title);
            date = (TextView)itemView.findViewById(R.id.Date);
            value = (TextView)itemView.findViewById(R.id.Value);
    
            // Set the listener to your involved views I.E
            category.setOnClickListener(this);
            itemView.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v) {
            // Do your things
            switch(v.getId) {
    
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可能希望在 onBindViewHolder 中的项目上使用 setOnClickListeners。

      例子:

      holder.title.setOnClickListener( (v) -> { .... });
      

      如果您想与 Activity/Fragment 共享点击,另一种方法是使用 Listener。

      interface ClickListener { void onClick(View view, String data) }; 
      
      
      new ItemViewHolder ( context, myListener) { ... }
      

      以后可以使用

      在您的 ViewHolder 中

      public ItemAdapter (Context context, ClickListener listener, List ....) {
        this.myListener = listener;
      }
      
      holder.title.setOnClickListener( v) -> myListener.onClick(view, "whateverdata"));
      

      或者您可能想使用 Android 数据绑定来减少您的样板。

      【讨论】:

      • 错了。您可以在持有人构造函数中尝试一次
      • 是的,对不起。修复了它。当然你需要在你的适配器构造函数中设置参数。
      【解决方案3】:

      只需在适配器中 onBindViewHolder() 方法的末尾添加 holder.image.setOnClickListener() 即可。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-05
        • 1970-01-01
        • 2018-01-10
        • 1970-01-01
        • 2015-02-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多