【发布时间】:2020-09-14 23:22:03
【问题描述】:
首先看到这个问题:Adding a colored background with text/icon under swiped row when using Android's RecyclerView
但是,即使标题声明“带有文本/图标”,答案也仅涵盖使用Canvas 对象绘制矩形。
现在我已经绘制了一个绿色矩形;但是我想通过添加“完成”按钮来更清楚地表明会发生什么,就像下面的屏幕截图一样。
我创建了一个自定义视图:
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);
}
}
而且布局非常简单,由绿色背景和图标组成:
<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),但有两个问题。
首先它只画了一个绿色的小方块包裹着我的“完成”图标(下面屏幕截图中的红色部分来自注释行choosePlanView.setBackgroundResource(R.color.delete_red);)。但是,在调试器中检查时,视图具有正确的宽度。
第二件事是放置图标的位置。我指定我希望它水平居中并始终“粘”在视图的右侧。但是它移动时,当RecyclerView 项目被滑动时,红色背景表明它也没有放在中心。
我尝试添加行choosePlanView.invalidate(),因为我认为会发生这种情况,因为视图是较早创建的,但重绘似乎没有任何改变。
【问题讨论】:
-
在您的 onChildDraw 方法中,您正在为复选标记图像绘制背景?如果是这样,图像有 wrap_content/wrap_content 大小......因此背景只是小方块
-
定位使用相对布局和子级:android:layout_centerVertical="true" android:layout_alignParentRight="true"
-
@VizGhar 在我的 onChildDraw 中,
choosePlanView.setBackgroundResource(R.color.delete_red);仅用于调试目的,以在布局中显示错误的复选标记图像位置。当我评论这一行时,该图像仍然具有 wrap_content/wrap_content 大小,并且根本没有背景。将 LinearLayout 更改为 RelativeLayout 不会更改图像位置。 -
"我试过重写方法 OnChildDraw" 什么是父类?我只是不确定你为什么以编程方式显示 ChoosePlanView
-
@VizGhar 啊,对不起,我已经编辑过了。父类为
ItemTouchHelper.SimpleCallback,用于实现刷卡。
标签: android android-recyclerview