【发布时间】:2015-03-21 19:45:48
【问题描述】:
我有一个布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@android:color/black"
android:layout_height="match_parent">
<RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:id="@+id/progress_bar"
android:layout_centerInParent="true"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RelativeLayout
android:id="@+id/long_press_container"
android:layout_centerInParent="true"
android:background="#40000000"
android:visibility="visible"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
我想在 long_press_container 相对布局中长按时画一个圆圈,但还要在触摸事件的地方获得对回收器网格视图中对象的引用。
public class CircleView extends View {
private float x;
private float y;
private int r;
// setup initial color
private final int paintColor = Color.WHITE;
// defines paint and canvas
private Paint drawPaint;
public CircleView(Context context, float x, float y, int r) {
super(context);
setupPaint();
this.x = x;
this.y = y;
this.r = r;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(x, y, r, drawPaint);
}
// Setup paint with color and stroke styles
private void setupPaint() {
drawPaint = new Paint();
drawPaint.setColor(paintColor);
drawPaint.setAntiAlias(true);
drawPaint.setStrokeWidth(3);
drawPaint.setStyle(Paint.Style.STROKE);
drawPaint.setStrokeJoin(Paint.Join.ROUND);
drawPaint.setStrokeCap(Paint.Cap.ROUND);
}}
我试过了
mMyRecyclerView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
Timber.e("recycler " + (motionEvent.getX()) + "," + (motionEvent.getY()));
mRelativeLayout.addView(new CircleView(getContext(), motionEvent.getX(), motionEvent.getY(), 25));
Timber.e("recycler " + view.getId());
}
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
mRelativeLayout.removeAllViews();
}
return false;
}
});
我画了一个圆圈,但我无法在列表视图中获取对该坐标上的项目的引用。有没有办法从回收器视图或适配器中获取该对象?
【问题讨论】:
标签: android gridview touch-event android-recyclerview motionevent