Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将onClickListener设置为RecyclerView的多个元素_Android_Android Recyclerview_Onclicklistener - Fatal编程技术网

Android 将onClickListener设置为RecyclerView的多个元素

Android 将onClickListener设置为RecyclerView的多个元素,android,android-recyclerview,onclicklistener,Android,Android Recyclerview,Onclicklistener,我正在尝试使用自定义项目创建一个Recycleview,如果我单击项目的ImageView,它会做一件事(更改图像并选择项目),如果我单击项目中的任何其他位置,它会做另一件事(打开新活动) 以下是我目前的代码: item_layout.xml <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="http://schemas.android.co

我正在尝试使用自定义项目创建一个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>
<?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>

listitem_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>
<?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();
    }
}
public类ItemViewHolder扩展了RecyclerView.ViewHolder{
公共图像视图类别;
公共文本视图标题;
公共文本查看日期;
公共文本视图值;
公众形象;
公共项目视图持有者(查看项目视图、图像视图类别、文本视图标题、文本视图日期、文本视图值、图像视图图像){
超级(项目视图);
this.category=类别;
this.title=标题;
this.date=日期;
这个值=值;
这个图像=图像;
}
公共项目视图持有者(查看项目视图){
超级(项目视图);
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
公共类ItemAdapter扩展了RecyclerView.Adapter{
私人语境;
私人名单;
公共项适配器(上下文上下文,列表所有项){
this.context=上下文;
this.allItem=allItem;
}
@凌驾
public ItemViewHolder onCreateViewHolder(视图组父级,int-viewType){
视图=LayoutFlater.from(上下文)。充气(R.layout.listitem_布局,父项,false);
返回新的ItemViewHolder(视图);
}
@凌驾
BindViewHolder上的公共无效(ItemViewHolder,int位置){
ItemObject items=allItem.get(位置);
//设置与类别对应的imageview
开关(items.getItemCategory()){
案例“其他”:持有者。类别。设置图像资源(R.drawable.ic_其他);中断;
案例“票据”:持有人。类别。设置图像资源(R.drawable.ic_票据);中断;
案例“业务”:holder.category.setImageResource(R.drawable.ic_业务);break;
箱子“衣服”:支架。类别。setImageResource(R.drawable.ic_衣服);破损;
案例“通勤”:持有者。类别。设置图像资源(R.drawable.ic_通勤);中断;
案例“娱乐”:持有者。类别。setImageResource(R.drawable.ic_娱乐);中断;
案例“食品”:持有者。类别。setImageResource(R.drawable.ic_食品);中断;
案例“杂货”:持有人。类别。setImageResource(R.drawable.ic_杂货);中断;
案例“健康”:holder.category.setImageResource(R.drawable.ic_Health);break;
案例“投资”:持有人。类别。setImageResource(R.drawable.ic_投资);中断;
案例“工资”:持有者。类别。设置图像资源(R.drawable.ic_工资);中断;
案例“旅行”:持有者。类别。设置图像资源(R.drawable.ic_旅行);中断;
默认值:holder.category.setImageResource(R.drawable.ic_默认值);break;
}
holder.title.setText(items.getItemTitle());
holder.date.setText(String.valueOf(items.getItemDate());
holder.value.setText(String.valueOf(items.getItemValue());
}
@凌驾
public int getItemCount(){
返回allItem.size();
}
}
我不确定下一步该怎么办,我希望能得到任何帮助。
提前谢谢

最好的方法是对ViewHolder类实现OnClickListener,并在公共构造函数中设置侦听器。无需在运行时多次调用的onBindViewHolder中分配侦听器。即使在某些情况下需要在运行时启用disable,也可以在侦听器中添加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) {

        }
    }
}

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

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

例如:

holder.title.setOnClickListener( (v) -> { .... });
另一种方法是,如果希望与活动/片段共享单击,则使用侦听器

interface ClickListener { void onClick(View view, String data) }; 


new ItemViewHolder ( context, myListener) { ... }
以后你可以用

在你的取景器里

public ItemAdapter (Context context, ClickListener listener, List ....) {
  this.myListener = listener;
}

holder.title.setOnClickListener( v) -> myListener.onClick(view, "whateverdata"));

或者,您可能希望使用Android数据绑定,这将减少您的样板文件

错。对不起,你可以在壁炉架上拿一次。修复了它。当然,您需要在适配器构造函数中设置参数。