ViewDragHelper实现卡片左右切换

//主要代码

public class CustomStackLayout extends FrameLayout {

    private Adapter mAdapter = Adapter.EMPTY;
    private int mCurrentItem;//当前显示的position
    private View mParent;


    public CustomStackLayout(@NonNull Context context) {
        this(context, null, 0);
    }

    public CustomStackLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomStackLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mParent = this;
    }


    public void setAdapter(Adapter adapter) {
        mAdapter = adapter;
        dataChanged(adapter);
        changeCardStyle();
    }


    //数据更新
    private void dataChanged(Adapter adapter) {

        for (int i = getmCurrentItem(); i < adapter.getItemCount(); i++) {
            if (adapter.getViewHolder(this, i) != null) {
                ViewHolder viewHolder = adapter.getViewHolder(CustomStackLayout.this, i);
                if (viewHolder.itemView != null) {
                    addView(viewHolder.itemView, 0);//frameLayout 叠加
                }
            }
        }
    }

    private void changeCardStyle() {

        int offset = -0;
        for (int i = getmCurrentItem(); i < this.getChildCount(); i++) {

            if (getChildAt(i) != null) {

                getChildAt(i).setTranslationY(offset=offset-10);
                getChildAt(i).setScaleX(0.8f+0.01f*i);
                getChildAt(i).setScaleY(0.8f-0.01f*i);
                Log.i("left",""+getChildAt(i).getLeft());
            }

        }
    }

    public static abstract class Adapter<VH extends ViewHolder> {

        public static Adapter<?> EMPTY = new Adapter<ViewHolder>() {

            @Override
            public ViewHolder onCreateViewHolder(ViewGroup parent, int pos) {
                return null;
            }

            @Override
            public void onBindViewHolder(ViewHolder viewHolder, int pos) {

            }

            @Override
            public int getItemCount() {
                return 0;
            }

        };

        public abstract VH onCreateViewHolder(ViewGroup parent, int pos);

        public abstract void onBindViewHolder(VH vh, int pos);

        public abstract int getItemCount();

        private VH getViewHolder(ViewGroup parent, int position) {

            VH viewHolder = onCreateViewHolder(parent, position);
            if (viewHolder != null) {
                onBindViewHolder(viewHolder, position);
                ViewHolder.setPosition(viewHolder.itemView, position);// 标记当前item的index
                return viewHolder;
            } else {
                throw new IllegalArgumentException("viewHolder is null");
            }
        }

    }

    public static abstract class ViewHolder {

        public View itemView = null;

        public ViewHolder(View itemView) {

            if (itemView == null) {
                throw new IllegalArgumentException("item view may not is null");
            }
            this.itemView = itemView;

        }

        private static void setPosition(View view, int pos) {
            view.setTag(R.id.pos_tg, pos);
        }

        public static int getPosition(View view) {
            return (int) view.getTag(R.id.pos_tg);
        }

    }

    public void setmCurrentItem(int mCurrentItem) {
        this.mCurrentItem = mCurrentItem;
    }

    public int getmCurrentItem() {
        return mCurrentItem;
    }

    public ViewDragHelper mViewDragHelper = ViewDragHelper.create(this, new ViewDragHelper.Callback() {

        @Override
        public boolean tryCaptureView(View child, int pointerId) {

            return mViewDragHelper.getViewDragState() == ViewDragHelper.STATE_IDLE && ViewHolder.getPosition(child) == getmCurrentItem();
        }

        //捕捉拖动view的left位置
        @Override
        public int clampViewPositionHorizontal(View child, int left, int dx) {
            return left;
        }

        // 捕捉拖动view的top位置
        @Override
        public int clampViewPositionVertical(View child, int top, int dy) {
            return top;
        }

        @Override
        public int getViewHorizontalDragRange(View child) {
            Log.i("parent width",""+mParent.getWidth());
            return mParent.getWidth();
        }

        @Override
        public int getViewVerticalDragRange(View child) {
            return mParent.getHeight();
        }

        @Override
        public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {

            int movePercent = left / getWidth();// 向左(-1,0); 向右(0,1)

        }

        @Override
        public void onViewReleased(View releasedChild, float xvel, float yvel) {

            if (Math.abs(releasedChild.getLeft()) < getWidth()/2){
                mViewDragHelper.smoothSlideViewTo(releasedChild,0,0);
                ViewCompat.postOnAnimation(releasedChild,new SettleRunnable(releasedChild, new Callback() {
                    @Override
                    public void onComplete(View view) {
                        //复位

                    }
                }));

            }else{
                mViewDragHelper.smoothSlideViewTo(releasedChild,(releasedChild.getLeft() < 0?-1:1)*getWidth(),releasedChild.getTop());
                ViewCompat.postOnAnimation(releasedChild,new SettleRunnable(releasedChild, new Callback() {
                    @Override
                    public void onComplete(View view) {
                        //移除
                        setmCurrentItem(getmCurrentItem()+1);
                        removeView(view);
                    }
                }));
            }

        }

    });

    public ViewDragHelper getDragViewHelper() {
        return mViewDragHelper;
    }

    private class SettleRunnable implements Runnable {

        private final View mView;
        private final Callback mScrollCallback;

        public SettleRunnable(View view, Callback scrollCallback) {
            mView = view;
            mScrollCallback = scrollCallback;
        }

        @Override
        public void run() {
            if (mViewDragHelper != null){
                if(mViewDragHelper.continueSettling(true)) {
                    ViewCompat.postOnAnimation(mView, this);    //递归调用
                }else{
                    if(mScrollCallback != null)
                        mScrollCallback.onComplete(mView);
                }
            }
        }
    }

    public interface Callback{
        void onComplete(View view);
    }

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

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        getDragViewHelper().processTouchEvent(event);
        return true;
    }
}
下载http://download.csdn.net/detail/u011057161/9914161

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ViewDragHelper是一个帮助我们实现View拖拽和滑动效果的工具类,使用它可以简单地实现一些常见的交互效果,例如拖拽、滑动、边缘拖拽等。以下是使用ViewDragHelper的一般步骤: 1. 创建ViewDragHelper对象 ``` ViewDragHelper mDragHelper = ViewDragHelper.create(parentView, 1.0f, new DragHelperCallback()); ``` 2. 编写DragHelperCallback类 ``` private class DragHelperCallback extends ViewDragHelper.Callback { // 重写tryCaptureView方法,判断是否捕获当前View @Override public boolean tryCaptureView(View child, int pointerId) { return true; } // 重写clampViewPositionHorizontal和clampViewPositionVertical方法,返回拖拽View的位置 @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; } } ``` 3. 在View的onTouchEvent中处理事件 ``` @Override public boolean onTouchEvent(MotionEvent event) { mDragHelper.processTouchEvent(event); return true; } ``` 4. 在View的onDraw方法中绘制View ``` @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 绘制View } ``` 以上是使用ViewDragHelper的一般步骤,具体使用还需要根据实际需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值