1. Are you ready for the Galaxy S20? Here is everything we know so far!

Drag and drop from RecyclerView to Relative layoutView

Discussion in 'Android Development' started by DoleBob, Jul 17, 2018.

  1. DoleBob

    DoleBob Lurker
    Thread Starter

    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 (Text):
    1.  
    2.  public void onBindViewHolder(final FootballPitchMainAdapter.ViewHolder holder, int position) {
    3.             holder.rvPlayerNameTop.setText("testing");
    4.        
    5.             holder.linearLayoutFull.setOnTouchListener(new View.OnTouchListener() {
    6.                 public boolean onTouch(View view, MotionEvent motionEvent) {
    7.                     if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
    8.                         ClipData data = ClipData.newPlainText("", "");
    9.                         View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
    10.                         view.startDrag(data, shadowBuilder, view, 0);
    11.                         view.setVisibility(View.INVISIBLE);
    12.                         return true;
    13.                     } else {
    14.                         return false;
    15.                     }
    16.                 }
    17.             });
    18.         }
    19.  
    This works as expected (I think anyway, I can click and drag each item)
    I set the onClickListener as such:

    Code (Text):
    1.  
    2. pitchRelativeLayout = findViewById(R.id.mainPitch);
    3.         pitchRelativeLayout.setOnDragListener(new MyDragListener());
    4.      public class MyDragListener implements View.OnDragListener {
    5.  
    6.             int x_cord;
    7.             int y_cord;
    8.  
    9.           @override
    10.             public boolean onDrag(View v, DragEvent event) {
    11.  
    12.                 switch(event.getAction()) {
    13.                     case DragEvent.ACTION_DRAG_STARTED:
    14.                         rootLayout = new FrameLayout.LayoutParams(pitchRelativeLayout.getWidth(), pitchRelativeLayout.getHeight());
    15.                         rootLayout.setMargins(300, 0, 300, 0);
    16.                         break;
    17.  
    18.                     case DragEvent.ACTION_DRAG_ENTERED:
    19.                         break;
    20.  
    21.                     case DragEvent.ACTION_DRAG_EXITED :
    22.                         break;
    23.  
    24.                     case DragEvent.ACTION_DRAG_LOCATION  :
    25.                         //x_cord = (int) event.getX();
    26.                         //y_cord = (int) event.getY();
    27.                         break;
    28.  
    29.                     case DragEvent.ACTION_DRAG_ENDED   :
    30.                         x_cord = (int) event.getX();
    31.                         y_cord = (int) event.getY();
    32.                         rootLayout.leftMargin = x_cord;
    33.                         rootLayout.topMargin = y_cord;
    34.                         v.setLayoutParams(rootLayout);
    35.                         break;
    36.  
    37.                     case DragEvent.ACTION_DROP:
    38.                         View view = (View) event.getLocalState();
    39.                         ViewGroup owner = (ViewGroup) view.getParent();
    40.                         owner.removeView(view);
    41.                         RelativeLayout container = (RelativeLayout) v;
    42.                         container.addView(view);
    43.                         view.setVisibility(View.VISIBLE);
    44.  
    45.                         break;
    46.                     default: break;
    47.                 }
    48.                 return true;
    49.             }
    50.         }
    51.  
    here is my layout xml:
    Code (Text):
    1.  
    2.   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3.         android:id="@+id/linerLayoutPlayerFull"
    4.         android:layout_width="wrap_content"
    5.         android:layout_height="wrap_content"
    6.         android:layout_centerInParent="true"
    7.         android:gravity="center_horizontal"
    8.         android:eek:rientation="vertical">
    9.  
    10.             <ImageView
    11.                 android:id="@+id/ivPlayerTop"
    12.                 android:layout_width="88dp"
    13.                 android:layout_height="53dp"
    14.                 android:src="@drawable/ic_football_top" />
    15.             <TextView
    16.                 android:id="@+id/playerNameTop"
    17.                 android:layout_width="wrap_content"
    18.                 android:layout_height="wrap_content"
    19.                 android:text="TextView" />
    20.     </LinearLayout>
    21.  
    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.
     


Loading...

Share This Page

Loading...