Android 如何在刷卡后检测recyclerview项目下方的点击视图?

Android 如何在刷卡后检测recyclerview项目下方的点击视图?,android,android-recyclerview,swipe,Android,Android Recyclerview,Swipe,在此处输入代码我有一个Recyclerview,其中包含每个项目的前景和背景视图。背景项有两个用于删除和撤消的按钮。我想在Recyclerview项目滑动时检测单击按钮 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" andro

在此处输入代码
我有一个Recyclerview,其中包含每个项目的前景和背景视图。背景项有两个用于删除和撤消的按钮。我想在Recyclerview项目滑动时检测单击按钮

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <LinearLayout
        android:id="@+id/category_item_row_background"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorAccent"
        android:orientation="horizontal"
        android:weightSum="1">

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.6"
            android:gravity="center_vertical|center"
            android:text="به سطل زباله منتقل شود؟"
            android:textColor="#fff"
            android:textSize="13dp" />
        <TextView
            android:id="@+id/delete_item"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.2"
            android:gravity="center_vertical|center"
            android:textColor="#fff"
            android:background="@color/colorAccent1"
            android:clickable="true"
            android:text="@string/delete"/>
        <TextView
            android:id="@+id/cancel_item"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.2"
            android:gravity="center_vertical|center"
            android:textColor="#fff"
            android:background="@color/colorAccent2"
            android:clickable="true"
            android:text="@string/cancel"/>

    </LinearLayout>
    <LinearLayout
        android:id="@+id/category_item_row_foreground"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="15dp"
        android:weightSum="1"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/category_list_category_title"
            style="@style/ListItemTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.85"
            android:gravity="right"
            android:paddingStart="20dp"
            android:paddingEnd="20dp"
            android:text="@string/review_tab_title" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.15"
            android:orientation="vertical"
            android:paddingStart="20dp"
            android:paddingEnd="20dp">

            <TextView
                android:id="@+id/category_list_card_count"
                style="@style/ListItemValue"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="@string/zero_value" />

            <TextView
                style="@style/ListItemLable"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="@string/card" />
        </LinearLayout>

    </LinearLayout>
</FrameLayout>
//MainActivity.java

adapter = new CategoryRecyclerAdapter(categories, this, 1);
        recyclerView.setAdapter(adapter);


        ItemTouchHelper.SimpleCallback itemTouchHelperCallback = new RecyclerItemTouchHelper(0, ItemTouchHelper.LEFT| ItemTouchHelper.RIGHT, new RecyclerItemTouchHelper.RecyclerItemTouchHelperListener() {
            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position) {
                if(ps!=-1){
                    if(ps==position)return;
                    adapter.notifyItemChanged(ps);
                }
                ps=position;
            }
        });
        new ItemTouchHelper(itemTouchHelperCallback).attachToRecyclerView(recyclerView);
//RecyclerAdapter.java

public class CategoryRecyclerAdapter extends RecyclerView.Adapter<CategoryRecyclerAdapter.MyViewHolder> {

    private ArrayList<Category> itemList;
    private Context ctx;
    private int itemClickType;

    public CategoryRecyclerAdapter(ArrayList<Category> items, Context context, int type) {//سازنده کلاس آداپتر
        this.itemList = items;
        ctx = context;
        itemClickType = type;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {//یه کلاس viewholder دلخواه بسازید و از viewholder خودِ recyclerview ارث بری کند

        public TextView txtTitle, txtCount, btnDelete, btnCancel;
        public LinearLayout viewBackground, viewForeground;

        public MyViewHolder(View itemView) {//, int state) {//سازنده کلاس viewholder
            super(itemView);
            txtTitle = itemView.findViewById(R.id.category_list_category_title);
            txtCount = itemView.findViewById(R.id.category_list_card_count);
            viewBackground = itemView.findViewById(R.id.category_item_row_background);
            viewForeground = itemView.findViewById(R.id.category_item_row_foreground);

            btnDelete = itemView.findViewById(R.id.delete_item);
            btnCancel = itemView.findViewById(R.id.cancel_item);
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {//دقت کنید اینجا کلاس خروجی از متد دقیقا باید کلاس viewHolder که خودتون نوشتید باشه
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_category_list, parent, false);
        return new MyViewHolder(view);
    }


    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {//و همینطور اینجا ورودی متد باید کلاس viewHolder که خودتون ایجاد کردید باشه
        final Category item = itemList.get(position);
        holder.txtTitle.setText(item.getName());
        holder.txtCount.setText(item.getCardCount() + "");
        if (position % 2 == 0) {
            holder.viewForeground.setBackgroundColor(ctx.getResources().getColor(R.color.colorFirst));
        } else {
            holder.viewForeground.setBackgroundColor(ctx.getResources().getColor(R.color.colorSecond));
        }
        holder.btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(ctx, "Delete!!!", Toast.LENGTH_SHORT).show();
            }
        });
...
...
公共类CategoryRecyclerAdapter扩展了RecyclerView.Adapter{
私有arraylistitemlist;
私有上下文ctx;
私有int项clicktype;
公共类别RecyclerAdapter(数组列表项、上下文、int类型){//
this.itemList=项目;
ctx=上下文;
itemClickType=类型;
}
公共类MyViewHolder扩展了RecyclerView.ViewHolder{//
公共文本视图txtTitle、txtCount、btnDelete、btnCancel;
公共线布局viewBackground、viewForeground;
公共MyViewHolder(视图项目视图){/,国际国家){//视图持有人
超级(项目视图);
txtTitle=itemView.findViewById(R.id.category\u list\u category\u title);
txtCount=itemView.findviewbyd(R.id.category\u list\u card\u count);
viewBackground=itemView.findViewById(R.id.category\u item\u row\u background);
viewForeground=itemView.findViewById(R.id.category\u item\u row\u foreground);
btnDelete=itemView.findviewbyd(R.id.delete\u item);
btnCancel=itemView.findviewbyd(R.id.cancel\u item);
}
}
@凌驾
在2004年6月的各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各د
View View=LayoutInflater.from(parent.getContext()).flate(R.layout.layout\u category\u list,parent,false);
返回新的MyViewHolder(视图);
}
@凌驾
公共无效的绑定视图持有人(我的视图持有人持有人,我的视图持有人,最终的中间点位置的持有人,最终的中间点位置的持有人)公共无效的绑定视图持有人(我的视图持有人持有人,我的视图持有人,我的视图持有人,最终的中间点点的持有人(我的视图持有人,我的视图持有人,最终的中间点点的持有人)公共无效的公开公开公开公开无效的公开公开公开公开公开的公开公开公开公开的公开公开公开的公开公开的公开公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的公开的یدباشه
最终类别项目=itemList.get(位置);
holder.txtTitle.setText(item.getName());
holder.txtCount.setText(item.getCardCount()+);
如果(位置%2==0){
holder.viewForeground.setBackgroundColor(ctx.getResources().getColor(R.color.colorFirst));
}否则{
holder.viewForeground.setBackgroundColor(ctx.getResources().getColor(R.color.colorSecond));
}
holder.btnDelete.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
Toast.makeText(ctx,“Delete!!!”,Toast.LENGTH_SHORT.show();
}
});
...
...
现在onclick侦听器onBindViewHolder不工作。
如何解决这个问题?

你能发布你的适配器代码吗?
setOnClickListener
内部适配器视窗夹吗?它不工作,有什么方法可以这样做吗?有什么解决方法吗?你能发布你的适配器代码吗?
setOnClickListener
内部适配器视窗夹吗?它不工作,有什么方法可以这样做吗没有解决办法吗?
public class CategoryRecyclerAdapter extends RecyclerView.Adapter<CategoryRecyclerAdapter.MyViewHolder> {

    private ArrayList<Category> itemList;
    private Context ctx;
    private int itemClickType;

    public CategoryRecyclerAdapter(ArrayList<Category> items, Context context, int type) {//سازنده کلاس آداپتر
        this.itemList = items;
        ctx = context;
        itemClickType = type;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {//یه کلاس viewholder دلخواه بسازید و از viewholder خودِ recyclerview ارث بری کند

        public TextView txtTitle, txtCount, btnDelete, btnCancel;
        public LinearLayout viewBackground, viewForeground;

        public MyViewHolder(View itemView) {//, int state) {//سازنده کلاس viewholder
            super(itemView);
            txtTitle = itemView.findViewById(R.id.category_list_category_title);
            txtCount = itemView.findViewById(R.id.category_list_card_count);
            viewBackground = itemView.findViewById(R.id.category_item_row_background);
            viewForeground = itemView.findViewById(R.id.category_item_row_foreground);

            btnDelete = itemView.findViewById(R.id.delete_item);
            btnCancel = itemView.findViewById(R.id.cancel_item);
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {//دقت کنید اینجا کلاس خروجی از متد دقیقا باید کلاس viewHolder که خودتون نوشتید باشه
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_category_list, parent, false);
        return new MyViewHolder(view);
    }


    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {//و همینطور اینجا ورودی متد باید کلاس viewHolder که خودتون ایجاد کردید باشه
        final Category item = itemList.get(position);
        holder.txtTitle.setText(item.getName());
        holder.txtCount.setText(item.getCardCount() + "");
        if (position % 2 == 0) {
            holder.viewForeground.setBackgroundColor(ctx.getResources().getColor(R.color.colorFirst));
        } else {
            holder.viewForeground.setBackgroundColor(ctx.getResources().getColor(R.color.colorSecond));
        }
        holder.btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(ctx, "Delete!!!", Toast.LENGTH_SHORT).show();
            }
        });
...
...