Android开发:使用ViewDragHelper实现抽屉拉伸效果

其实,有很多方法可以实现一个Layout的抽屉拉伸效果,最经常的方法就是自定义一个ViewGroup,然后控制点击事件,控制移动之类的,这种方法的代码量多,而且实现起来复杂,后期维护增加其他效果也很麻烦,直到今天看到了 ViewDragHelper这个类,就是专门为实现View的移动而生的,我就试着开发了一个抽屉拉伸的效果,效果图如下:



所有移动的控制在ViewDragHelper.Callback里面来实现,移动就用dragHelper.smoothSlideViewTo来实现,而且Callback集成了许多的方法,方便后期的维护或者增加其他功能。

首先看下最核心的DragLayout的代码


public class DragLayout extends LinearLayout {
    private ViewDragHelper dragHelper;
    private View mDragView, contentView;
    private int dragRange;

    public DragLayout(Context context) {
        super(context);
        init();
    }

    public DragLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public DragLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    public DragLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        dragHelper = ViewDragHelper.create(this, callback);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mDragView = findViewById(R.id.dragView);
        contentView = findViewById(R.id.contentView);
    }

    private ViewDragHelper.Callback callback = new ViewDragHelper.Callback() {
        @Override
        public boolean tryCaptureView(View child, int pointerId) {
            return child == mDragView;
        }

        @Override
        public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) 
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值