Как обнаружить щелчок на представлении ниже пункта повторного просмотра после удара? - PullRequest
1 голос
/ 05 июля 2019

enter code here У меня есть 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>

// RecyclerItemTouchHelper.java

public class RecyclerItemTouchHelper extends ItemTouchHelper.SimpleCallback {
    private RecyclerItemTouchHelperListener listener;

    public RecyclerItemTouchHelper(int dragDirs, int swipeDirs, RecyclerItemTouchHelperListener listener) {
        super(dragDirs, swipeDirs);
        this.listener = listener;
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        return true;
    }

    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
        if (viewHolder != null) {
            final View foregroundView = ((CategoryRecyclerAdapter.MyViewHolder) viewHolder).viewForeground;

            getDefaultUIUtil().onSelected(foregroundView);
        }
    }

    @Override
    public void onChildDrawOver(Canvas c, RecyclerView recyclerView,
                                RecyclerView.ViewHolder viewHolder, float dX, float dY,
                                int actionState, boolean isCurrentlyActive) {
        final View foregroundView = ((CategoryRecyclerAdapter.MyViewHolder) viewHolder).viewForeground;
        getDefaultUIUtil().onDrawOver(c, recyclerView, foregroundView, dX, dY,
                actionState, isCurrentlyActive);
    }

    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        final View foregroundView = ((CategoryRecyclerAdapter.MyViewHolder) viewHolder).viewForeground;
        getDefaultUIUtil().clearView(foregroundView);

    }

    @Override
    public void onChildDraw(Canvas c, RecyclerView recyclerView,
                            RecyclerView.ViewHolder viewHolder, float dX, float dY,
                            int actionState, boolean isCurrentlyActive) {
        final View foregroundView = ((CategoryRecyclerAdapter.MyViewHolder) viewHolder).viewForeground;

        getDefaultUIUtil().onDraw(c, recyclerView, foregroundView, dX, dY,
                actionState, isCurrentlyActive);
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        listener.onSwiped(viewHolder, direction, viewHolder.getAdapterPosition());
    }

    @Override
    public int convertToAbsoluteDirection(int flags, int layoutDirection) {
        return super.convertToAbsoluteDirection(flags, layoutDirection);
    }

    public interface RecyclerItemTouchHelperListener {
        void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position);
    }
}

// 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();
            }
        });
...
...

Теперь onclick слушатель onBindViewHolder не работает. Как решить эту проблему ???

...