SoFunction
Updated on 2025-03-11

A method to implement single-page floating layer draggable view on Android

The previous article talks about the method of dragging through the official ViewDragHelper tool of goolge (see previous articlehttps:///article/), then there is a problem that the onTouchEvent in DragframeLayout cannot receive the touch message, and there is no trigger method when onInterceptTouchEvent, so another primitive method was born: to implement it by customizing the draggable view

Main methods:

initEdge: Sets the initial boundary of the draggable view that can drag range, which is generally the boundary of the parent layout. Note... I will get 0 later. I set the boundary when the network data is returned and display it. There is also a way to open a child thread to obtain it.

onTouchEvent: dragging calculation and relayout

Code:

import ;
import ;
import .;
import ;
import ;

/**
  * Created by hq on 2017/10/10.
  * Reference: /zane_xiao/article/details/51188867
  */

public class DragImageView extends AppCompatImageView {
  String TAG = "DragImageView";

  public DragImageView(Context context) {
    this(context, null);
  }

  public DragImageView(Context context, @Nullable AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public DragImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }

  /**
    * Set boundaries in parent layout
    * @param l
    * @param t
    * @param r
    * @param b
    */
  public void initEdge(int l,int t,int r,int b) {
    edgeLeft = l;
    edgeTop = t;
    edgeRight = r;
    edgeBottom = b;
  }

  int edgeLeft, edgeTop, edgeRight, edgeBottom;
  int lastX, lastY, movex, movey, dx, dy;

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    switch (()) {
      case MotionEvent.ACTION_DOWN:
        lastX = (int) ();
        lastY = (int) ();
        movex = lastX;
        movey = lastY;
        break;
      case MotionEvent.ACTION_MOVE:
        dx = (int) () - lastX;
        dy = (int) () - lastY;

        int left = getLeft() + dx;
        int top = getTop() + dy;
        int right = getRight() + dx;
        int bottom = getBottom() + dy;
        if (left < edgeLeft) {
          left = edgeLeft;
          right = left + getWidth();
        }
        if (right > edgeRight) {
          right = edgeRight;
          left = right - getWidth();
        }
        if (top < edgeTop) {
          top = edgeTop;
          bottom = top + getHeight();
        }
        if (bottom > edgeBottom) {

          bottom = edgeBottom;
          top = bottom - getHeight();
        }

        layout(left, top, right, bottom);
        lastX = (int) ();
        lastY = (int) ();
        break;
      case MotionEvent.ACTION_UP:
        //Avoid sliding out triggering click event        if ((int) (() - movex) != 0
          || (int) (() - movey) != 0) {
          return true;
        }
        break;
      default:
        break;
    }
    return (event);
  }
}

layout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:andro
xmlns:custom="/apk/res-auto"
xmlns:tools="/tools"
android:
android:layout_width="match_parent"
android:layout_height="match_parent">

<
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@color/default_white"
  android:scrollbars="none">

  <RelativeLayout
    android:
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/default_white">

    ...........

  </RelativeLayout>
</>

<
  android:
  android:layout_width="40dp"
  android:layout_height="40dp"
  android:layout_gravity="right|top"
  android:src="@drawable/ic_launcher" />
</FrameLayout>

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.