• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Drag and drop from RecyclerView to Relative layoutView

DoleBob

Lurker
I created a RV where each item has an onClicklistener to drag the item and drop it anywhere on the screen.
Now I have been able to set the OnClickListener, create the drag item but when I release the item it does not stay where it is dropped and just disappears.
I have spent couple of days trying to fix this, looking though Stackover flow and the net. But so far no luck.
Here the is onBind for the RecyclerView which sets the onClick and drag:
Code:
 public void onBindViewHolder(final FootballPitchMainAdapter.ViewHolder holder, int position) {
            holder.rvPlayerNameTop.setText("testing");
        
            holder.linearLayoutFull.setOnTouchListener(new View.OnTouchListener() {
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                        ClipData data = ClipData.newPlainText("", "");
                        View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
                        view.startDrag(data, shadowBuilder, view, 0);
                        view.setVisibility(View.INVISIBLE);
                        return true;
                    } else {
                        return false;
                    }
                }
            });
        }

This works as expected (I think anyway, I can click and drag each item)
I set the onClickListener as such:

Code:
pitchRelativeLayout = findViewById(R.id.mainPitch);
        pitchRelativeLayout.setOnDragListener(new MyDragListener());
     public class MyDragListener implements View.OnDragListener {
 
            int x_cord;
            int y_cord;
 
          @override
            public boolean onDrag(View v, DragEvent event) {
 
                switch(event.getAction()) {
                    case DragEvent.ACTION_DRAG_STARTED:
                        rootLayout = new FrameLayout.LayoutParams(pitchRelativeLayout.getWidth(), pitchRelativeLayout.getHeight());
                        rootLayout.setMargins(300, 0, 300, 0);
                        break;
 
                    case DragEvent.ACTION_DRAG_ENTERED:
                        break;
 
                    case DragEvent.ACTION_DRAG_EXITED :
                        break;
 
                    case DragEvent.ACTION_DRAG_LOCATION  :
                        //x_cord = (int) event.getX();
                        //y_cord = (int) event.getY();
                        break;
 
                    case DragEvent.ACTION_DRAG_ENDED   :
                        x_cord = (int) event.getX();
                        y_cord = (int) event.getY();
                        rootLayout.leftMargin = x_cord;
                        rootLayout.topMargin = y_cord;
                        v.setLayoutParams(rootLayout);
                        break;
 
                    case DragEvent.ACTION_DROP:
                        View view = (View) event.getLocalState();
                        ViewGroup owner = (ViewGroup) view.getParent();
                        owner.removeView(view);
                        RelativeLayout container = (RelativeLayout) v;
                        container.addView(view);
                        view.setVisibility(View.VISIBLE);
 
                        break;
                    default: break;
                }
                return true;
            }
        }
here is my layout xml:
Code:
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linerLayoutPlayerFull"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center_horizontal"
        android:eek:rientation="vertical">
 
            <ImageView
                android:id="@+id/ivPlayerTop"
                android:layout_width="88dp"
                android:layout_height="53dp"
                android:src="@drawable/ic_football_top" />
            <TextView
                android:id="@+id/playerNameTop"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView" />
    </LinearLayout>
I am not sure what I need to do, or what am I missing to get the dragged item to move where it is dragged to.
 
Back
Top Bottom