Android ---- ViewDragHelper (drag personnalisé) un ----

  1. Il dispose
    vue draggable personnalisé
  2. Création de processus
    créer personnalisés glisser viewGroup peut
    instancier viewDragHelper
    la méthode de rappel prioritaire interne
    mise en page xml ajoutée
  3. la mise en œuvre du code simple
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>
Publié cinq articles originaux · louange gagné 6 · vues 769

Je suppose que tu aimes

Origine blog.csdn.net/huoruohe/article/details/105363169
conseillé
Classement