滑动以获取更多选项

时间:2017-12-29 02:04:35

标签: android xamarin android-recyclerview

我觉得我非常接近,但我也认为我有点迷失,我有一个RecylerView我想向左右滑动以获得更多选项

RecyclerView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:paddingTop="?android:attr/actionBarSize">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

RecyclerViewRow

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <RelativeLayout
        android:id="@+id/view_background"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/bg_row_background">
        <ImageView
            android:id="@+id/delete_icon"
            android:layout_width="@dimen/ic_delete"
            android:layout_height="@dimen/ic_delete"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="@dimen/padd_10"
            android:src="@drawable/ic_delete" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginRight="@dimen/padd_10"
            android:layout_toLeftOf="@id/delete_icon"
            android:text="@string/delete"
            android:textColor="#fff"
            android:textSize="13dp" />
    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/view_foreground"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:padding="@dimen/padd_10">
        <ImageView
            android:id="@+id/photoImageView"
            android:layout_width="@dimen/thumbnail"
            android:layout_height="@dimen/thumbnail"
            android:layout_marginRight="@dimen/activity_padding_horizontal"
            android:src="@drawable/ddg"
            android:scaleType="centerCrop" />
        <TextView
            android:id="@+id/nameTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/photoImageView"
            android:ellipsize="end"
            android:fontFamily="sans-serif-medium"
            android:maxLines="1"
            android:textColor="@color/item_name"
            android:textSize="17dp" />
        <TextView
            android:id="@+id/departmentTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/nameTextView"
            android:layout_marginTop="5dp"
            android:layout_toRightOf="@+id/photoImageView"
            android:textColor="@color/description"
            android:textSize="12dp" />
        <TextView
            android:id="@+id/price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_toRightOf="@+id/photoImageView"
            android:textColor="@color/accent"
            android:textStyle="bold" />
    </RelativeLayout>
</FrameLayout>

请注意@+id/view_foreground@+id/view_background

我的RecyclerView填充正常,如屏幕截图所示

enter image description here

  

我能够向左或向右滑动,如下面的屏幕截图所示,但请注意背景全是白色 - 它显示为白色背景而不是触发@+id/view_background

enter image description here

我的ItemTouchHelper是以下

 public class RecyclerTouchHelper : ItemTouchHelper.Callback
    {
        private Paint paint = new Paint();
        public RecyclerTouchHelper()
        {
            // you can pass any thing in your contractor , may be your RecyclerView adapter 
        }
        public override int GetMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder)
        {
            int dragFlags = ItemTouchHelper.Up | ItemTouchHelper.Down;
            int swipeFlags = ItemTouchHelper.Start | ItemTouchHelper.End;
            return MakeMovementFlags(dragFlags, swipeFlags);
        }

        public override bool OnMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target)
        {
            return true;
        }

        public override void OnSwiped(RecyclerView.ViewHolder viewHolder,  int direction)
        {
            // do what ever you want here 
            Log.Info("AA:", "OnSwiped");



        }

        public override void OnChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, bool isCurrentlyActive)
        {
            Log.Info("AA:", "OnChildDraw");

        }
    }

在LOG条目中,我可以看到OnSwiped事件

触发的日志

刷卡时如何显示更多选项?这是我想要实现的最终结果

enter image description here

我不知道该怎么做

1 个答案:

答案 0 :(得分:0)

检查此代码

        ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {

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

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


                if (direction == ItemTouchHelper.RIGHT) {
                    //Todo . Implement Right Swipe logic
                    adapter.notifyDataSetChanged();
                }

                if (direction == ItemTouchHelper.LEFT) {

                    //Todo . Implement Left Swipe logic
                    adapter.notifyDataSetChanged();

                }
            }

            @Override
            public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {

                Bitmap icon;
                if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {

                    View itemView = viewHolder.itemView;
                    float height = (float) itemView.getBottom() - (float) itemView.getTop();
                    float width = height / 3;
                    if (dX > 0) {


                     //Right Swipe Paint Background


                    } else {

                     //Left Swipe Paint Background where p is Paint Variable


                        p.setColor(Color.parseColor("#D32F2F"));
                        RectF background = new RectF((float) itemView.getRight() + dX, (float) itemView.getTop() + height / 6, (float) itemView.getRight(), (float) itemView.getBottom() - height / 6);
                        c.drawRect(background, p);
                        icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_delete);
                        RectF icon_dest = new RectF((float) itemView.getRight() - 2 * width, (float) itemView.getTop() + width, (float) itemView.getRight() - width, (float) itemView.getBottom() - width);
                        c.drawBitmap(icon, null, icon_dest, p);
                        //adapter.notifyDataSetChanged();
                    }
                }
                super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
            }
        };
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
        itemTouchHelper.attachToRecyclerView(recycler_Remainder);