Android----ViewDragHelper(自定義拖拽) ---- 之一

  1. 功能
    自定義可以拖拽的view
  2. 創建流程
    創建自定義 可以拖拽的 viewGroup
    實例化 viewDragHelper
    callback 內部方法 重寫
    xml佈局中添加
  3. 簡單的代碼實現
package com.field.dragdemo;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;
import androidx.annotation.NonNull;
import androidx.customview.widget.ViewDragHelper;

/**
 * 1.創建自定義 可以拖拽的 viewGroup
 * 2.實例化 viewDragHelper
 * 3.callback 內部方法 重寫
 * 4.xml佈局中添加
 */
public class MyRelativeDragLayout extends RelativeLayout {
    //輸入 logt 快速生成TAG
    private static final String TAG = "MyRelativeDragLayout";
    //
    private ViewDragHelper mViewDragHelper;


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

    public MyRelativeDragLayout(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MyRelativeDragLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //實例化拖拽幫助類  1.this -- 當前對象 2.當前對象 內部處理拖拽事件的回調
        mViewDragHelper = ViewDragHelper.create(this, new ViewDragHelper.Callback() {

            //嘗試捕獲當前ViewGroup內部的view,利用多態 獲取 child的 id 如果滿足條件 就返回 true,不滿足就 返回 false
            @Override
            public boolean tryCaptureView(@NonNull View child, int pointerId) {
                int id = child.getId();
                if (id == R.id.drag_img){
                    return true;
                }else {
                    return false;
                }
            }
            //可以垂直拖動的距離
            @Override
            public int clampViewPositionVertical(@NonNull View child, int top, int dy) {
                return top;
            }
            //可以水平拖動的距離
            @Override
            public int clampViewPositionHorizontal(@NonNull View child, int left, int dx) {
                return left;
            }
        });
    }


    //viewDragHelper攔截 ev
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return mViewDragHelper.shouldInterceptTouchEvent(ev);
    }
    //viewDragHelper處理event 一定要返回true
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mViewDragHelper.processTouchEvent(event);
        return true;
    }
}
package com.field.dragdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <!--創建自定義view-->
    <com.field.dragdemo.MyRelativeDragLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:id="@+id/drag_img_1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_centerInParent="true"
            android:src="@color/colorPrimary"/>
    </com.field.dragdemo.MyRelativeDragLayout>

</RelativeLayout>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章