【发布时间】:2014-03-21 11:35:56
【问题描述】:
我有一个自定义视图,扩展RelativeLayout 并将其用作ListView 中的列表项。它基本上是一个矩形,左侧有一个时髦的角度。根据我在onTouchEvent 中返回的内容,我可能会处于两种可能的状态:
- 列表项将正确显示其按下状态并返回未按下状态,但不会将点击发送到 ListView。
- 列表项将保持按下状态(并且无法退出),但会将点击发送到 ListView 非常好。
我以前从未做过这种类型的自定义视图,但非常感谢您的建议。我想我遗漏了一些简单的东西,但是我在这一点上花了太多时间来继续不问问题!
代码如下:
public class ListItemBackgroundView extends RelativeLayout {
private final Path PATH = new Path();
private final Paint BRUSH = new Paint(Paint.ANTI_ALIAS_FLAG);
private static int mBackgroundColor = -1;
private static int mPressedBackgroundColor = -1;
private static int mAngleOffset = -1;
private boolean isPressed;
public ListItemBackgroundView(Context context) {
super(context);
setWillNotDraw(false);
}
public ListItemBackgroundView(Context context, AttributeSet attrs) {
super(context, attrs);
setWillNotDraw(false);
}
public ListItemBackgroundView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setWillNotDraw(false);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
final int w = canvas.getWidth();
final int h = canvas.getHeight();
if (mBackgroundColor == -1) {
mBackgroundColor = getResources().getColor(R.color.color_1);
mPressedBackgroundColor = getResources().getColor(R.color.color_2);
mAngleOffset = getResources().getDimensionPixelSize(R.dimen.list_view_angle_offset);
}
PATH.moveTo(0, 0);
PATH.lineTo(mAngleOffset, h * (0.35f));
PATH.lineTo(0, h);
PATH.lineTo(w, h);
PATH.lineTo(w, 0);
PATH.lineTo(0, 0);
PATH.close();
BRUSH.setStyle(Paint.Style.FILL);
BRUSH.setColor(isPressed ? mPressedBackgroundColor : mBackgroundColor);
canvas.drawPath(PATH, BRUSH);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean result = super.onTouchEvent(event);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
isPressed = true;
result = false;
invalidate();
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
isPressed = false;
result = true;
invalidate();
break;
}
return result;
}
}
【问题讨论】:
-
尝试使用 StateListDrawable 作为背景 Drawable?
-
@pskink 如果我的背景是图像而不是自定义形状,这会很好用(除非我遗漏了什么)。
-
是的,你错过了 SLD 显示的不是位图而是 Drawables 的事实
-
@pskink 啊,我没有意识到您可以通过编程方式定义它们。我会试一试,虽然我不确定如何在没有 onDraw 的情况下创建每个可绘制对象。
-
好问题!答案是 Drawable.draw
标签: android android-listview android-canvas android-custom-view