ViewDragHelper使用介绍

  • ViewDragHelper是用来干什么的
  • 我们看一下官网介绍
  • 1.他是自定义ViewGroup的工具类
  • 2.提供了操作和状态常量
  • 3.拖放和恢复
  • Callback
  • Callback是ViewDragHelper的内部类
  • 1.是子类父类沟通的桥梁
  • 2.管理拖放的范围
  • 3.为ViewDragHelper提供了事件和方法
  • 具体使用
  • 1.实现Callback的方法
  • 2.通过ViewDragHelper.create创建对象,并传入callback
  • 3.在onInterceptTouchEvent中shouldInterceptTouchEvnet
  • 4.在onTouchEvent中接受 processTouchEvent(event)
  • CallBack方法介绍
  • 1.onViewDragStateChanged    状态改变的时候调用
  • 2.onViewPositionChanged  位置改变
  • 3.onViewCaptured ziView捕获
  • 4.onViewReleased 释放View
  • 5.onEdgeToched 边界触摸
  • 6.onEdgeLock 边界锁
  • 7.tryCaptureView 是否捕获是关键
  • 8.clampViewPostionHorzontal/Vertical   限制水平或者垂直的距离
  • 具体代码
  • public class DragView extends FrameLayout {
        private View mCurrent;
        private boolean b;
        private ViewDragHelper.Callback callback = new ViewDragHelper.Callback() {
    
            //这里是捕获的关键
            @Override
            public boolean tryCaptureView(View child, int pointerId) {
    
                return true;
            }
    
            //捕获view
            @Override
            public void onViewCaptured(View capturedChild, int activePointerId) {
                String tag = (String) capturedChild.getTag();
                switch (tag) {
    
                    case "1":
    
    
                        break;
                    case "2":
    
    
                        break;
    
    
                }
    
    
            }
    
            //限制水平方向
            @Override
            public int clampViewPositionHorizontal(View child, int left, int dx) {
    
    
                return left;
            }
    
            // 限制垂直方向
            @Override
            public int clampViewPositionVertical(View child, int top, int dy) {
    
                return top;
            }
    
            @Override
            public void onViewReleased(View releasedChild, float xvel, float yvel) {
                super.onViewReleased(releasedChild, xvel, yvel);
    //            这里是回到原来的位置
                helper.settleCapturedViewAt(px / 2, py / 2);
                invalidate();
    
    
            }
        };
        private final int px;
        private final int py;
    
        @Override
        public void computeScroll() {
    //        配合回到原来的位置
            if (helper.continueSettling(true)) {
                invalidate();
            }
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            int mode = MeasureSpec.getMode(widthMeasureSpec);
            int sizeW = MeasureSpec.getSize(widthMeasureSpec);
            int sizeH = MeasureSpec.getSize(heightMeasureSpec);
            System.out.println(sizeH);
        }
    
        private final ViewDragHelper helper;
    
        @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
        public DragView(Context context, AttributeSet attrs) {
            super(context, attrs);
            helper = ViewDragHelper.create(this, 1f, callback);
    
            Configuration configuration = context.getResources().getConfiguration();
    // px = dip * density/160
            int screenWidthDp = configuration.screenWidthDp;
            int screenHeightDp = configuration.screenHeightDp;
            px = screenWidthDp * configuration.densityDpi / 160;
    //       得到当前屏幕的密度
            py = screenHeightDp * configuration.densityDpi / 160;
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
    //        处理时间
            helper.processTouchEvent(event);
            return true;
        }
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
    //  helper拦截事件
            return helper.shouldInterceptTouchEvent(ev);
        }
    
    
    }
    


发布了10 篇原创文章 · 获赞 4 · 访问量 2959
展开阅读全文

ViewDragHelper做拖动,View会跳回到原位置,why?

10-29

使用ViewDragHelper做拖动,当动态addView的时候,原本已经拖动到另一位置的View会回到原位置,why? 我有一个简单的VHLayout,继承自FrameLayout,里面通过setDragableView方法可以指定哪个子View可以拖动 ``` import android.content.Context; import android.support.v4.view.MotionEventCompat; import android.support.v4.widget.ViewDragHelper; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.widget.FrameLayout; public class VHLayout extends FrameLayout { private View mView; private ViewDragHelper mDragHelper; public VHLayout(Context context, AttributeSet attrs) { super(context, attrs); mDragHelper = ViewDragHelper.create(this, 1.0f, new ViewDragHelper.Callback() { // 返回ture则表示可以捕获该view;可以根据传入的view参数决定哪些可以拖动 @Override public boolean tryCaptureView(View child, int pointerId) { return child == mView; } // 控制水平拖动的边界 @Override public int clampViewPositionHorizontal(View child, int left, int dx) { final int leftBound = getPaddingLeft(); final int rightBound = getWidth() - child.getWidth() - leftBound; final int newLeft = Math.min(Math.max(left, leftBound), rightBound); return newLeft; } // 控制垂直拖动的边界 @Override public int clampViewPositionVertical(View child, int top, int dy) { final int topBound = getPaddingTop(); final int bottomBound = getHeight() - child.getHeight() - topBound; final int newTop = Math.min(Math.max(top, topBound), bottomBound); return newTop; } @Override public void onViewReleased(View releasedChild, float xvel, float yvel) { super.onViewReleased(releasedChild, xvel, yvel); } // 解决ViewDragHelper中添加button,在控制button拖动的同时又不影响button的点击事件! @Override public int getViewHorizontalDragRange(View child) { return mView == child ? child.getWidth() : 0; } @Override public int getViewVerticalDragRange(View child) { return mView == child ? child.getHeight() : 0; } @Override public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) { super.onViewPositionChanged(changedView, left, top, dx, dy); changedView.layout(left, top, left + changedView.getMeasuredWidth(), top + changedView.getMeasuredHeight()); } }); } @Override protected void onFinishInflate() { super.onFinishInflate(); } @Override public boolean onInterceptTouchEvent(MotionEvent event) { final int action = MotionEventCompat.getActionMasked(event); if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) { mDragHelper.cancel(); return false; } return mDragHelper.shouldInterceptTouchEvent(event); } @Override public boolean onTouchEvent(MotionEvent event) { try { mDragHelper.processTouchEvent(event); } catch (Exception e) { // TODO: handle exception } return true; } public void setDragableView(View view) { mView = view; } } ``` 拖动一切没问题 但是当我实际使用的时候却有问题了,如下,我在布局里的一个叫container的LinearLayout动态添加一个View(addView),就出现所说的现象。事实上,只要弹出输入法,也会出现这种回到原位置的问题。怎么解决的? 使用方式: ``` public void addFloatView(int bottomMargin){ FrameLayout.LayoutParams params=new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT); params.gravity=Gravity.RIGHT|Gravity.BOTTOM;//让加入的View出现在右下角 params.bottomMargin=bottomMargin; ImageView iv=new ImageView(context); iv.setImageResource(R.drawable.ic_scan); iv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub onFloatViewClick(); } }); VHLayout rootView=(VHLayout)findViewById(R.id.rootView); rootView.setDragableView(iv); rootView.addView(iv,params); } ``` 布局: ``` <com.mb.goods.widget.VHLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/rootView" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <include layout="@layout/actionbar_comm" /> </LinearLayout> </com.mb.goods.widget.VHLayout> ``` 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览