DragView(一)

传承者(Inheritors)打造共同进步生态圈!!!

转载:
http://blog.csdn.net/lmj623565791/article/details/46858663

三段论:
思考,自定义,使用

public class CustomViewGroup2 extends LinearLayout {

    private ViewDragHelper mViewDragHelper;

    public CustomViewGroup2(Context context) {
        this(context,null);
    }
    public CustomViewGroup2(Context context, AttributeSet attrs) {
        super(context, attrs);
        mViewDragHelper = ViewDragHelper.create(this, 1.0f, new ViewDragHelper.Callback() {
            @Override
            public boolean tryCaptureView(View child, int pointerId) {
                return true;
            }

            @Override
            public int clampViewPositionHorizontal(View child, int left, int dx) {

              final  int leftBound = getPaddingLeft();
                final int rightBound = getWidth()-child.getWidth()-leftBound;

                final int mewLeft = Math.min(Math.max(left,leftBound),rightBound);

                return mewLeft;
            }

            @Override
            public int clampViewPositionVertical(View child, int top, int dy) {
                return top;
            }
        });
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return mViewDragHelper.shouldInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        mViewDragHelper.processTouchEvent(event);

        return true;
    }
}
调用
<?xml version="1.0" encoding="utf-8"?>
<com.example.administrator.testapplication.CustomViewGroup2 xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_margin="10dp"
        android:gravity="center"
        android:layout_gravity="center"
        android:background="#44ff0000"
        android:text="I can be dragged !"
        android:layout_width="100dp"
        android:layout_height="100dp"/>

    <TextView
        android:layout_margin="10dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:background="#44ff0000"
        android:text="I can be dragged !"
        android:layout_width="100dp"
        android:layout_height="100dp"/>

    <TextView
        android:layout_margin="10dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:background="#44ff0000"
        android:text="I can be dragged !"
        android:layout_width="100dp"
        android:layout_height="100dp"/>

</com.example.administrator.testapplication.CustomViewGroup2>
部分方法解释
  /**
         * Restrict the motion of the dragged child view along the horizontal axis.
         * The default implementation does not allow horizontal motion; the extending
         * class must override this method and provide the desired clamping.
         *
         *
         * @param child Child view being dragged
         * @param left Attempted motion along the X axis
         * @param dx Proposed change in position for left
         * @return The new clamped position for left
         */
        public int clampViewPositionHorizontal(View child, int left, int dx) {
            return 0;
        }


  /**
         * Restrict the motion of the dragged child view along the vertical axis.
         * The default implementation does not allow vertical motion; the extending
         * class must override this method and provide the desired clamping.
         *
         *
         * @param child Child view being dragged
         * @param top Attempted motion along the Y axis
         * @param dy Proposed change in position for top
         * @return The new clamped position for top
         */
        public int clampViewPositionVertical(View child, int top, int dy) {
            return 0;
        }

效果:
这里写图片描述
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DragLinearLayout是一个LinearLayout, 他可是其子View在其范围内可拖动、可交换位置。默认情况下,子View是不可拖动的,你需要调用DragLinearLayout.setViewDraggable(child, child)方法让其可拖动。项目地址:https://github.com/justasm/DragLinearLayout 效果图:如何使用和使用LinearLayout一样:                        2. 让子View可拖动默认情况下子View是不可拖动的,你需要调用dragLinearLayout.setViewDraggable()让子View变为可拖动的。DragLinearLayout dragLinearLayout = (DragLinearLayout) findViewById(R.id.container); //让子view可拖动,默认是不能拖动的         for(int i = 1; i < dragLinearLayout.getChildCount(); i ){             View child = dragLinearLayout.getChildAt(i);             dragLinearLayout.setViewDraggable(child, child);          }可以动态添加可拖动子viewfinal View view = View.inflate(context, R.layout.view_layout, null); dragLinearLayout.addDragView(viewview.findViewById(R.id.view_drag_handle));   // ..   dragLinearLayout.removeDragView(view);使用OnViewSwapListener检测子view之间的排序变化事件:dragLinearLayout.setOnViewSwapListener(new DragLinearLayout.OnViewSwapListener() {     @Override     public void onSwap(View firstView, int firstPosition,             View secondView, int secondPosition) {         // update data, etc..     } });当在ScrollView中使用DragLinearLayout的时候,如果你想在拖拽的时候ScrollView也能滚动,需要调用setContainerScrollView(Scroll
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值