Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.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 循环器视图使用背景色刷卡,刷卡时文本不显示矩形_Android_Android Recyclerview_Swipe - Fatal编程技术网

Android 循环器视图使用背景色刷卡,刷卡时文本不显示矩形

Android 循环器视图使用背景色刷卡,刷卡时文本不显示矩形,android,android-recyclerview,swipe,Android,Android Recyclerview,Swipe,如何实现如下图所示: 右击(向左) 左扫(向右) ) 代码: 这是我尝试编写的代码,但无法实现与图中相同的效果。如何设计如图所示的背景色和圆角文字 @Override public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {

如何实现如下图所示:

右击(向左)

左扫(向右)

)

代码:

这是我尝试编写的代码,但无法实现与图中相同的效果。如何设计如图所示的背景色和圆角文字

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

        View itemView = viewHolder.itemView;
        Paint p = new Paint();

        if (dX > 0) {          

            c.drawRect((float) itemView.getLeft(), (float) itemView.getTop(), dX,
                    (float) itemView.getBottom(), p);

        } else {

            RectF rightButton = new RectF(itemView.getRight() , itemView.getTop(), itemView.getRight(), itemView.getBottom());
            p.setColor(Color.BLACK);
            c.drawRoundRect(rightButton, dX, dY, p);
            drawText(mContext.getString(R.string.cancel), c, rightButton, p);
        }

        // Fade out the view when it is swiped out of the parent
        final float alpha = ALPHA_FULL - Math.abs(dX) / (float) viewHolder.itemView.getWidth();
        viewHolder.itemView.setAlpha(alpha);
        viewHolder.itemView.setTranslationX(dX);

    } else {
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
    }
}

private void drawText(String text, Canvas c, RectF button, Paint p) {
    float textSize = 30;
    p.setColor(Color.WHITE);
    p.setAntiAlias(true);
    p.setTextSize(textSize);

    float textWidth = p.measureText(text);
    c.drawText(text, button.centerX() - (textWidth / 2), button.centerY() + (textSize / 2), p);
}

您可以通过在
Recyclerview
上设置
ItemTouchHelper.SimpleCallback
来实现它

您可以在这里查看这个很好的示例:


您的矩形没有绘制,因为
new Rect()
的左侧和右侧与
itemView.getRight()重合。请尝试以下代码。根据需要更改Rect的值

RectF rightButton = new RectF(itemView.getRight() - 200 , itemView.getTop(), itemView.getRight(), itemView.getBottom());
你可以这样做

像这样更改你的gradle文件

compile 'ru.rambler.android:swipe-layout:1.0.15'
在适配器中添加以下内容

        holder.swipe_layout.setOnSwipeListener(new SwipeLayout.OnSwipeListener() {
            @Override
            public void onBeginSwipe(SwipeLayout swipeLayout, boolean moveToRight) {
                try {

                    if (lastSwipeLayout != holder.swipe_layout) {
                        lastSwipeLayout.animateReset();
                    }


                } catch (Exception e) {
                }

                lastSwipeLayout = holder.swipe_layout;

                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            holder.swipe_layout.animateReset();
                        } catch (Exception e) {
                        }
                    }
                }, 3000);
            }

            @Override
            public void onSwipeClampReached(SwipeLayout swipeLayout, boolean moveToRight) {

            }

            @Override
            public void onLeftStickyEdge(SwipeLayout swipeLayout, boolean moveToRight) {

            }

            @Override
            public void onRightStickyEdge(SwipeLayout swipeLayout, boolean moveToRight) {

            }
        });
在回收项目xml中添加以下内容

<ru.rambler.libs.swipe_layout.SwipeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/swipe_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/layout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">

 <!--put your layout codes here-->

    </LinearLayout>

           <!--this is your hidden button behind the item-->

    <FrameLayout
        android:id="@+id/item_delete"
        android:layout_width="125dp"
        android:layout_height="match_parent"
        android:background="@color/colorAccent"
        app:gravity="right"
        app:bring_to_clamp="150dp"
        app:clamp="self"
        app:sticky="100dp">


            <ImageView
                android:layout_width="56dp"
                android:layout_height="56dp"
                android:padding="8dp"
                android:tint="@color/white"
                android:layout_gravity="center"
                android:src="@mipmap/ic_delete"
                />

    </FrameLayout>


</ru.rambler.libs.swipe_layout.SwipeLayout>


我不想使用任何库。这一更改没有帮助最终实现,参考上面的链接几乎晚了2年……您是如何通过左扫或右扫来处理两种不同的背景的?该示例仅显示一种用于删除的背景类型。