iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android----ViewDragHelper(自定义拖拽) ---- 之一
  • 485
分享到

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

自定义viewdraghelperAndroid 2022-06-06 13:06:15 485人浏览 泡泡鱼
摘要

功能 自定义可以拖拽的view 创建流程 创建自定义 可以拖拽的 viewGroup 实例化 viewDragHelper callback 内部方法 重写 xml布局中添加 简单的代码实现 package com.field.dr

功能
自定义可以拖拽的view 创建流程
创建自定义 可以拖拽的 viewGroup
实例化 viewDragHelper
callback 内部方法 重写
xml布局中添加 简单的代码实现
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;

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);
    }
}

    

作者:huoruohe


--结束END--

本文标题: Android----ViewDragHelper(自定义拖拽) ---- 之一

本文链接: https://www.lsjlt.com/news/29406.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作