Search in sources :

Example 1 with OnItemSwipeListener

use of com.chad.library.adapter.base.listener.OnItemSwipeListener in project BaseRecyclerViewAdapterHelper by CymChad.

the class ItemDragAndSwipeUseActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_item_touch_use);
    setBackBtn();
    setTitle("ItemDrag  And Swipe");
    mRecyclerView = (RecyclerView) findViewById(R.id.rv_list);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    mData = generateData(50);
    OnItemDragListener listener = new OnItemDragListener() {

        @Override
        public void onItemDragStart(RecyclerView.ViewHolder viewHolder, int pos) {
            Log.d(TAG, "drag start");
            BaseViewHolder holder = ((BaseViewHolder) viewHolder);
        //                holder.setTextColor(R.id.tv, Color.WHITE);
        }

        @Override
        public void onItemDragMoving(RecyclerView.ViewHolder source, int from, RecyclerView.ViewHolder target, int to) {
            Log.d(TAG, "move from: " + source.getAdapterPosition() + " to: " + target.getAdapterPosition());
        }

        @Override
        public void onItemDragEnd(RecyclerView.ViewHolder viewHolder, int pos) {
            Log.d(TAG, "drag end");
            BaseViewHolder holder = ((BaseViewHolder) viewHolder);
        //                holder.setTextColor(R.id.tv, Color.BLACK);
        }
    };
    final Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setTextSize(20);
    paint.setColor(Color.BLACK);
    OnItemSwipeListener onItemSwipeListener = new OnItemSwipeListener() {

        @Override
        public void onItemSwipeStart(RecyclerView.ViewHolder viewHolder, int pos) {
            Log.d(TAG, "view swiped start: " + pos);
            BaseViewHolder holder = ((BaseViewHolder) viewHolder);
        //                holder.setTextColor(R.id.tv, Color.WHITE);
        }

        @Override
        public void clearView(RecyclerView.ViewHolder viewHolder, int pos) {
            Log.d(TAG, "View reset: " + pos);
            BaseViewHolder holder = ((BaseViewHolder) viewHolder);
        //                holder.setTextColor(R.id.tv, Color.BLACK);
        }

        @Override
        public void onItemSwiped(RecyclerView.ViewHolder viewHolder, int pos) {
            Log.d(TAG, "View Swiped: " + pos);
        }

        @Override
        public void onItemSwipeMoving(Canvas canvas, RecyclerView.ViewHolder viewHolder, float dX, float dY, boolean isCurrentlyActive) {
            canvas.drawColor(ContextCompat.getColor(ItemDragAndSwipeUseActivity.this, R.color.color_light_blue));
        //                canvas.drawText("Just some text", 0, 40, paint);
        }
    };
    mAdapter = new ItemDragAdapter(mData);
    mItemDragAndSwipeCallback = new ItemDragAndSwipeCallback(mAdapter);
    mItemTouchHelper = new ItemTouchHelper(mItemDragAndSwipeCallback);
    mItemTouchHelper.attachToRecyclerView(mRecyclerView);
    //mItemDragAndSwipeCallback.setDragMoveFlags(ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT | ItemTouchHelper.UP | ItemTouchHelper.DOWN);
    mItemDragAndSwipeCallback.setSwipeMoveFlags(ItemTouchHelper.START | ItemTouchHelper.END);
    mAdapter.enableSwipeItem();
    mAdapter.setOnItemSwipeListener(onItemSwipeListener);
    mAdapter.enableDragItem(mItemTouchHelper);
    mAdapter.setOnItemDragListener(listener);
    //        mRecyclerView.addItemDecoration(new GridItemDecoration(this ,R.drawable.list_divider));
    mRecyclerView.setAdapter(mAdapter);
    mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {

        @Override
        public void onSimpleItemClick(final BaseQuickAdapter adapter, final View view, final int position) {
            ToastUtils.showShortToast("点击了" + position);
        }
    });
}
Also used : BaseViewHolder(com.chad.library.adapter.base.BaseViewHolder) OnItemClickListener(com.chad.library.adapter.base.listener.OnItemClickListener) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) ItemDragAdapter(com.chad.baserecyclerviewadapterhelper.adapter.ItemDragAdapter) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) Paint(android.graphics.Paint) ItemTouchHelper(android.support.v7.widget.helper.ItemTouchHelper) BaseViewHolder(com.chad.library.adapter.base.BaseViewHolder) OnItemDragListener(com.chad.library.adapter.base.listener.OnItemDragListener) BaseQuickAdapter(com.chad.library.adapter.base.BaseQuickAdapter) OnItemSwipeListener(com.chad.library.adapter.base.listener.OnItemSwipeListener) ItemDragAndSwipeCallback(com.chad.library.adapter.base.callback.ItemDragAndSwipeCallback)

Example 2 with OnItemSwipeListener

use of com.chad.library.adapter.base.listener.OnItemSwipeListener in project BaseRecyclerViewAdapterHelper by CymChad.

the class DragAndSwipeUseActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_universal_recycler);
    setBackBtn();
    setTitle("Drag And Swipe");
    mRecyclerView = findViewById(R.id.rv);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    // 拖拽监听
    OnItemDragListener listener = new OnItemDragListener() {

        @Override
        public void onItemDragStart(RecyclerView.ViewHolder viewHolder, int pos) {
            Log.d(TAG, "drag start");
            final BaseViewHolder holder = ((BaseViewHolder) viewHolder);
            // 开始时,item背景色变化,demo这里使用了一个动画渐变,使得自然
            int startColor = Color.WHITE;
            int endColor = Color.rgb(245, 245, 245);
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                ValueAnimator v = ValueAnimator.ofArgb(startColor, endColor);
                v.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        holder.itemView.setBackgroundColor((int) animation.getAnimatedValue());
                    }
                });
                v.setDuration(300);
                v.start();
            }
        }

        @Override
        public void onItemDragMoving(RecyclerView.ViewHolder source, int from, RecyclerView.ViewHolder target, int to) {
            Log.d(TAG, "move from: " + source.getAdapterPosition() + " to: " + target.getAdapterPosition());
        }

        @Override
        public void onItemDragEnd(RecyclerView.ViewHolder viewHolder, int pos) {
            Log.d(TAG, "drag end");
            final BaseViewHolder holder = ((BaseViewHolder) viewHolder);
            // 结束时,item背景色变化,demo这里使用了一个动画渐变,使得自然
            int startColor = Color.rgb(245, 245, 245);
            int endColor = Color.WHITE;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                ValueAnimator v = ValueAnimator.ofArgb(startColor, endColor);
                v.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        holder.itemView.setBackgroundColor((int) animation.getAnimatedValue());
                    }
                });
                v.setDuration(300);
                v.start();
            }
        }
    };
    // 侧滑监听
    OnItemSwipeListener onItemSwipeListener = new OnItemSwipeListener() {

        @Override
        public void onItemSwipeStart(RecyclerView.ViewHolder viewHolder, int pos) {
            Log.d(TAG, "view swiped start: " + pos);
            BaseViewHolder holder = ((BaseViewHolder) viewHolder);
        }

        @Override
        public void clearView(RecyclerView.ViewHolder viewHolder, int pos) {
            Log.d(TAG, "View reset: " + pos);
            BaseViewHolder holder = ((BaseViewHolder) viewHolder);
        }

        @Override
        public void onItemSwiped(RecyclerView.ViewHolder viewHolder, int pos) {
            Log.d(TAG, "View Swiped: " + pos);
        }

        @Override
        public void onItemSwipeMoving(Canvas canvas, RecyclerView.ViewHolder viewHolder, float dX, float dY, boolean isCurrentlyActive) {
            canvas.drawColor(ContextCompat.getColor(DragAndSwipeUseActivity.this, R.color.color_light_blue));
        }
    };
    List<String> mData = generateData(50);
    mAdapter = new DragAndSwipeAdapter(mData);
    mAdapter.getDraggableModule().setSwipeEnabled(true);
    mAdapter.getDraggableModule().setDragEnabled(true);
    mAdapter.getDraggableModule().setOnItemDragListener(listener);
    mAdapter.getDraggableModule().setOnItemSwipeListener(onItemSwipeListener);
    mAdapter.getDraggableModule().getItemTouchHelperCallback().setSwipeMoveFlags(ItemTouchHelper.START | ItemTouchHelper.END);
    // mAdapter.getDraggableModule().getItemTouchHelperCallback().setDragMoveFlags(ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT | ItemTouchHelper.UP | ItemTouchHelper.DOWN);
    mRecyclerView.setAdapter(mAdapter);
    mAdapter.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
            Tips.show("点击了:" + position);
        }
    });
}
Also used : BaseViewHolder(com.chad.library.adapter.base.viewholder.BaseViewHolder) OnItemClickListener(com.chad.library.adapter.base.listener.OnItemClickListener) Canvas(android.graphics.Canvas) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) ValueAnimator(android.animation.ValueAnimator) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) DragAndSwipeAdapter(com.chad.baserecyclerviewadapterhelper.adapter.DragAndSwipeAdapter) BaseViewHolder(com.chad.library.adapter.base.viewholder.BaseViewHolder) OnItemDragListener(com.chad.library.adapter.base.listener.OnItemDragListener) BaseQuickAdapter(com.chad.library.adapter.base.BaseQuickAdapter) OnItemSwipeListener(com.chad.library.adapter.base.listener.OnItemSwipeListener)

Aggregations

Canvas (android.graphics.Canvas)2 View (android.view.View)2 BaseQuickAdapter (com.chad.library.adapter.base.BaseQuickAdapter)2 OnItemClickListener (com.chad.library.adapter.base.listener.OnItemClickListener)2 OnItemDragListener (com.chad.library.adapter.base.listener.OnItemDragListener)2 OnItemSwipeListener (com.chad.library.adapter.base.listener.OnItemSwipeListener)2 ValueAnimator (android.animation.ValueAnimator)1 Paint (android.graphics.Paint)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 ItemTouchHelper (android.support.v7.widget.helper.ItemTouchHelper)1 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 DragAndSwipeAdapter (com.chad.baserecyclerviewadapterhelper.adapter.DragAndSwipeAdapter)1 ItemDragAdapter (com.chad.baserecyclerviewadapterhelper.adapter.ItemDragAdapter)1 BaseViewHolder (com.chad.library.adapter.base.BaseViewHolder)1 ItemDragAndSwipeCallback (com.chad.library.adapter.base.callback.ItemDragAndSwipeCallback)1 BaseViewHolder (com.chad.library.adapter.base.viewholder.BaseViewHolder)1