如何在android recyclerview刷卡下显示2个部分

时间:2017-10-30 11:27:23

标签: java android android-recyclerview

我想在recyclelerview下用图标和文字显示2个部分。

这是我的代码

@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;
                    float action_item_width = viewHolder.itemView.getWidth() / 5;
                    Paint p = new Paint();
                    if (dX > 0) {
                        Log.i("actionState", dX + " " + action_item_width);
                        dX = (dX-1 > action_item_width) ? action_item_width : dX;
                        p.setColor(getResources().getColor(R.color.colorAccent));
                        c.drawRect((float) itemView.getLeft(), (float) itemView.getTop(), dX,
                                (float) itemView.getBottom(), p);
                    } else {
                        dX = 0;
                    }

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

它只创建一个带颜色的部分。我需要添加图标和文字。

1 个答案:

答案 0 :(得分:0)

使用不同的方法可能更容易实现您的需求。

您可以为回收站视图项实现两个视图(前景和背景):

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/bg">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/icon_text_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/bg1">

        <!--icon and text layout (background)-->

    </android.support.constraint.ConstraintLayout>

    <android.support.constraint.ConstraintLayout
        android:id="@+id/item_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/bg2">

        <!--recyclerview item layout (foreground)-->

    </android.support.constraint.ConstraintLayout>

</FrameLayout>

并且在recyclerview的视图持有者中:

class MyViewHolder extends RecyclerView.ViewHolder {
    private ConstraintLayout mItemLayout;
    private ConstraintLayout mIconTextLayout;

    MyViewHolder(View v) {
        super(v);
        mItemLayout = (ConstraintLayout) v.findViewById(R.id.item_layout);
        mIconTextLayout = (ConstraintLayout) v.findViewById(R.id.icon_text_layout);
    }    
    View getViewToSwipe() {
        return mItemLayout;
    }
}

最后,使用这些回调将itemToucHelper附加到recyclerview:

ItemTouchHelper.SimpleCallback mItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT) {

    // override methods

    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        if (viewHolder instanceof MyViewHolder) {
            int swipeFlags = ItemTouchHelper.RIGHT;
            return makeMovementFlags(0, swipeFlags);
        } else {
            return 0;
        }
    }

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

    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        getDefaultUIUtil().clearView(((MyViewHolder) viewHolder)
                .getViewToSwipe());
    }

    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
        if (viewHolder != null) {
            getDefaultUIUtil().onSelected(((MyViewHolder) viewHolder)
                    .getViewToSwipe());
        }
    }

    public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
                            float dX, float dY, int actionState, boolean isCurrentlyActive) {
        getDefaultUIUtil().onDraw(c, recyclerView, ((MyViewHolder) viewHolder)
            .getViewToSwipe(), dX, dY, actionState, isCurrentlyActive);
    }

    public void onChildDrawOver(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
                                float dX, float dY, int actionState, boolean isCurrentlyActive) {               
        getDefaultUIUtil().onDrawOver(c, recyclerView, ((MyViewHolder) viewHolder)
            .getViewToSwipe(), dX, dY, actionState, isCurrentlyActive);
    }
};

ItemTouchHelper mItemTouchHelper = new ItemTouchHelper(mItemTouchCallback);

通过这种方法,您可以灵活地在背景布局中放置任何内容,f.i。带有textview的图标,如你所愿。