刷卡RecyclerView项下显示的自定义视图 [英] Showing custom View under swiped RecyclerView item

查看:220
本文介绍了刷卡RecyclerView项下显示的自定义视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我看到了这个问题:<一href="http://stackoverflow.com/questions/30820806/adding-a-colored-background-with-text-icon-under-swiped-row-when-using-androids">Adding文字/图标采用了Android的RecyclerView 当彩色背景刷卡排在

First of all, I saw this question: Adding a colored background with text/icon under swiped row when using Android's RecyclerView

不过,尽管标题指出用文本/图标,答案只包括使用画布对象来绘制一个矩形。

However, even though the title states "with text/icon", the answer only covers using Canvas object to draw a rectangle.

现在我已经绘制实现一个绿色的长方形;不过,我想使它更加明显发生了什么事情发生,加上完成按钮,就像在下面的截图。

Now I have drawing a green rectangle implemented; however I want to make it more obvious what is going to happen by adding "done" button, just like on the following screenshot.

我创建了一个自定义视图:

I created a custom view:

public class ChoosePlanView extends LinearLayout{

    public ChoosePlanView(Context context) {
        super(context);
        init();
    }

    public ChoosePlanView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ChoosePlanView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init(){
        View v = inflate(getContext(), R.layout.choose_plan_view, null);
        addView(v);
    }
}

和布局非常简单,由绿色背景和图标:

And the layout is very simple, consisting of green background and the icon:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/choose_green">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/doneIcon"
        android:layout_gravity="center_horizontal|right"
        android:src="@mipmap/done"/>

</LinearLayout>

我试图重写方法 OnChildDraw

// ChoosePlanView choosePlanView; <- that was declared and initialized earlier, so I don't have to create a new ChoosePlanView everytime in OnChildDraw();

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

/*
    onMove, onSwiped omitted
*/
  public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
       View itemView = viewHolder.itemView;

       if(dX < 0){
            choosePlanView.invalidate();
            //choosePlanView.setBackgroundResource(R.color.delete_red);
            choosePlanView.measure(itemView.getWidth(), itemView.getHeight());
            choosePlanView.layout(itemView.getRight() + (int) dX, itemView.getTop(), itemView.getRight(), itemView.getBottom());
            c.save();
            c.translate(choosePlanView.getRight() + (int) dX, viewHolder.getAdapterPosition()*itemView.getHeight());

            choosePlanView.draw(c);
            c.restore();
       }

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

和它还挺工程(平我的 ChoosePlanView ),但有两个问题。

And it kinda works (draws my ChoosePlanView), but there are two problems.

首先它绘制只有一小绿地广场环绕我的做图标(看到下面的截图来自注释行 choosePlanView.setBackgroundResource视图的红色部分(R.color.delete_red ); )。该观点有正确的宽度不过,当调试器检查。

First of all it draws only a small green square wrapping my "done" icon (the red part of the view seen on the screenshot below comes from commented line choosePlanView.setBackgroundResource(R.color.delete_red);). The view has the correct width though, when checked in debugger.

第二件事是那里的图标放置。我指定我希望它是水平居中,并始终以贴到右侧视图。然而,移动,当 RecyclerView 项刷卡,并有红色背景显示它不是放置在中心无论是。

Second thing is where the icon is placed. I specified that I want it to be centered horizontally and always "sticked" to the right side of the view. However it moves, when the RecyclerView item is is swiped, and having red background shows it isn't placed in the center either.

我尝试添加行 choosePlanView.invalidate(),因为我认为出现这种情况,是因为有人在前面创建,但似乎重绘改变什么。

I tried adding line choosePlanView.invalidate(), because I thought that happens, because the view was created earlier, but it seems redrawing changes nothing.

推荐答案

画代替了,我相信你应该使用viewType

Instead of draw over, I believe you should use viewType

器具 getItemViewType(INT位置)适配器,返回不同的类型,如: 0为正常,1 swipped

Implements getItemViewType(int position) in adapter, returning different types, e.g. 0 for normal, 1 for swipped

和改变onCreateViewHolder等返回不同的布局不同viewType

and change onCreateViewHolder etc to return different layout on different viewType

这篇关于刷卡RecyclerView项下显示的自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆