0

I have a layout

<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>

And i want to draw a circle on long press in long_press_container relative layout but also get a reference to object in recycler grid view on the place of touch event.

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);
}}

I have tried this

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;
        }
    });

and i draw the circle but i have no way to get the reference to the item on that coordinates in list view. Is there a way to get that object from recycler view or adapter?

ddog
  • 670
  • 1
  • 10
  • 25

2 Answers2

0

Set the touch listener on the recycler view's viewHolder for every item. So that you get the recycler view item which is touched by the user.

balachandarkm
  • 855
  • 2
  • 9
  • 11
0

Found an answer with a litle help from here

if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                        mRelativeLayout.addView(new CircleView(getContext(), motionEvent.getX(), motionEvent.getY(), 25));
                        View v = mMyRecyclerView.findChildViewUnder(motionEvent.getX(), motionEvent.getY());
                        Timber.d("position" + mMyRecyclerView.getChildAdapterPosition(v));
                    }
                    if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                        mRelativeLayout.removeAllViews();
                    }

Here you get the position of item in the adapter and you get it by calling adapters list and pass the position

ddog
  • 670
  • 1
  • 10
  • 25