1. 程式人生 > >android:RecyclerView互動動畫(上下拖動,左右滑動刪除)

android:RecyclerView互動動畫(上下拖動,左右滑動刪除)

  • 效果

在這裡插入圖片描述

RecyclerView互動動畫主要使用的是ItemTouchHelper這個類

  • 建立MyItemTouchHelperCallback繼承系統ItemTouchHelper.Callback
import android.graphics.Canvas;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;

import tsou.cn.studyrecycler.R;

/**
 * Created by Administrator on 2018/10/11 0011.
 */

public class MyItemTouchHelperCallback extends ItemTouchHelper.Callback {
    private ItemTouchMoveCallback mMoveCallback;

    public MyItemTouchHelperCallback(ItemTouchMoveCallback callback) {
        this.mMoveCallback = callback;
    }

    //Callback回撥監聽時先呼叫的,用來判斷當前是什麼動作,比如判斷方向(監聽哪個方向的拖動)
    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        //放向:up、down、left、right
        //常量:
//        int up = ItemTouchHelper.UP;//1  0x0001
//        int down = ItemTouchHelper.DOWN;//2 0x0010
//        int left = ItemTouchHelper.LEFT;
//        int right = ItemTouchHelper.RIGHT;

        //要監聽的拖拽方向,不監聽為0
        int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
        //要監聽的側滑方向,不監聽為0
//        int swipeFlags = 0;
        int swipeFlags = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
        int flags = makeMovementFlags(dragFlags, swipeFlags);
        return flags;//即監聽向上也監聽向下
    }

    //是否允許長按拖拽
    @Override
    public boolean isLongPressDragEnabled() {
        return true;
    }

    //移動的時候回撥的方法(拖拽等)
    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        if (viewHolder.getItemViewType() != target.getItemViewType()) {
            return false;
        }
        //在拖拽的過程中不斷地呼叫adapter.notifyItemMoved(from,to)
        mMoveCallback.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
        return true;
    }

    //側滑的時候回撥的方法
    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        //監聽側滑;1.刪除資料,2.呼叫adapter.notifyItemRemoved(position)
        mMoveCallback.onItemRemove(viewHolder.getAdapterPosition());
    }

    //改變選中的Item
    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
        //判斷狀態
        if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
            viewHolder.itemView.findViewById(R.id.textview).setBackgroundColor(viewHolder.itemView.getContext().getResources().getColor(R.color.black));
        }
        super.onSelectedChanged(viewHolder, actionState);
    }

    //恢復改變選中的Item
    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        //恢復
        viewHolder.itemView.findViewById(R.id.textview).setBackgroundColor(viewHolder.itemView.getContext().getResources().getColor(R.color.colorPrimary));
        //透明度動畫
        viewHolder.itemView.setAlpha(1);//1~0
        //縮放動畫
        viewHolder.itemView.setScaleX(1);//1~0
        viewHolder.itemView.setScaleY(1);//1~0
        super.clearView(recyclerView, viewHolder);
    }

    //在拖拽的時候做效果
    @Override
    public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
        //dx  水平方向移動的增量(負:向左;正:向右)範圍:0~View.getWidth
        float alpha=1 - Math.abs(dX) / viewHolder.itemView.getWidth();
        if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
            //透明度動畫
            viewHolder.itemView.setAlpha(alpha);//1~0
            //縮放動畫
            viewHolder.itemView.setScaleX(alpha);//1~0
            viewHolder.itemView.setScaleY(alpha);//1~0
        }
//        if (alpha==0){
//            //透明度動畫
//            viewHolder.itemView.setAlpha(1);//1~0
//            //縮放動畫
//            viewHolder.itemView.setScaleX(1);//1~0
//            viewHolder.itemView.setScaleY(1);//1~0
//        }
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
    }
}

  • MainAdapter
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Collections;
import java.util.List;

import tsou.cn.studyrecycler.R;
import tsou.cn.studyrecycler.callback.ItemTouchMoveCallback;
import tsou.cn.studyrecycler.callback.StartDragCallback;


/**
 * Created by Administrator on 2017/7/11 0011.
 */

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> implements ItemTouchMoveCallback {
    private Context mContext;
    private List<String> mLists;
    private StartDragCallback mCallback;

    public MainAdapter(Context context, List<String> lists, StartDragCallback callback) {
        this.mContext = context;
        this.mLists = lists;
        this.mCallback = callback;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_main, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.mTextview.setText(mLists.get(position));
        //解決觸控移動
        holder.mImageview.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    //傳遞觸控情況(觸控移動)
                    mCallback.onStartDrag(holder);
                }
                return false;
            }
        });
    }

    @Override
    public boolean onItemMove(int fromPosition, int toPosition) {
        //1.資料交換;2.重新整理
        Collections.swap(mLists, fromPosition, toPosition);
        notifyItemMoved(fromPosition, toPosition);
        return true;
    }

    @Override
    public boolean onItemRemove(int position) {
        //1.刪除資料,2.重新整理
        mLists.remove(position);
        notifyItemRemoved(position);
        return true;
    }

    @Override
    public int getItemCount() {
        return mLists.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView mTextview;
        ImageView mImageview;

        ViewHolder(View view) {
            super(view);
            this.mTextview = (TextView) view.findViewById(R.id.textview);
            this.mImageview = (ImageView) view.findViewById(R.id.imageview);
        }
    }

}

  • MainActivity

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;

import java.util.ArrayList;

import tsou.cn.lib_hxgioc.HxgBind;
import tsou.cn.lib_hxgioc.HxgContentView;
import tsou.cn.lib_hxgioc.HxgViewUtils;
import tsou.cn.studyrecycler.adatper.MainAdapter;
import tsou.cn.studyrecycler.callback.MyItemTouchHelperCallback;
import tsou.cn.studyrecycler.callback.StartDragCallback;

@HxgContentView(R.layout.activity_main)
public class MainActivity extends AppCompatActivity implements StartDragCallback {

    @HxgBind(R.id.recyclerView)
    private RecyclerView mRecyclerView;
    private ArrayList<String> list = new ArrayList<>();
    private ItemTouchHelper itemTouchHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        HxgViewUtils.getView().inject(this);
        for (int i = 0; i < 100; i++) {
            list.add("huangxiaoguo" + i);
        }
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        MainAdapter adapter = new MainAdapter(this, list, this);
        mRecyclerView.setAdapter(adapter);
        //條目觸控幫助類
        ItemTouchHelper.Callback callback = new MyItemTouchHelperCallback(adapter);
        itemTouchHelper = new ItemTouchHelper(callback);
        itemTouchHelper.attachToRecyclerView(mRecyclerView);
    }

    /**
     * 解決觸控移動
     *
     * @param viewHolder
     */
    @Override
    public void onStartDrag(RecyclerView.ViewHolder viewHolder) {
        itemTouchHelper.startDrag(viewHolder);
    }
}

-手指觸控上下滑動監聽StartDragCallback


public interface StartDragCallback {
    public void onStartDrag(RecyclerView.ViewHolder viewHolder);
}
  • 拖拽和側滑回調 ItemTouchMoveCallback

import android.graphics.Canvas;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;

import tsou.cn.studyrecycler.R;

/**
 * Created by Administrator on 2018/10/11 0011.
 */

public class MyItemTouchHelperCallback extends ItemTouchHelper.Callback {
    private ItemTouchMoveCallback mMoveCallback;

    public MyItemTouchHelperCallback(ItemTouchMoveCallback callback) {
        this.mMoveCallback = callback;
    }

    //Callback回撥監聽時先呼叫的,用來判斷當前是什麼動作,比如判斷方向(監聽哪個方向的拖動)
    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        //放向:up、down、left、right
        //常量:
//        int up = ItemTouchHelper.UP;//1  0x0001
//        int down = ItemTouchHelper.DOWN;//2 0x0010
//        int left = ItemTouchHelper.LEFT;
//        int right = ItemTouchHelper.RIGHT;

        //要監聽的拖拽方向,不監聽為0
        int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
        //要監聽的側滑方向,不監聽為0
//        int swipeFlags = 0;
        int swipeFlags = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
        int flags = makeMovementFlags(dragFlags, swipeFlags);
        return flags;//即監聽向上也監聽向下
    }

    //是否允許長按拖拽
    @Override
    public boolean isLongPressDragEnabled() {
        return true;
    }

    //移動的時候回撥的方法(拖拽等)
    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        if (viewHolder.getItemViewType() != target.getItemViewType()) {
            return false;
        }
        //在拖拽的過程中不斷地呼叫adapter.notifyItemMoved(from,to)
        mMoveCallback.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
        return true;
    }

    //側滑的時候回撥的方法
    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        //監聽側滑;1.刪除資料,2.呼叫adapter.notifyItemRemoved(position)
        mMoveCallback.onItemRemove(viewHolder.getAdapterPosition());
    }

    //改變選中的Item
    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
        //判斷狀態
        if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
            viewHolder.itemView.findViewById(R.id.textview).setBackgroundColor(viewHolder.itemView.getContext().getResources().getColor(R.color.black));
        }
        super.onSelectedChanged(viewHolder, actionState);
    }

    //恢復改變選中的Item
    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        //恢復
        viewHolder.itemView.findViewById(R.id.textview).setBackgroundColor(viewHolder.itemView.getContext().getResources().getColor(R.color.colorPrimary));
        //透明度動畫
        viewHolder.itemView.setAlpha(1);//1~0
        //縮放動畫
        viewHolder.itemView.setScaleX(1);//1~0
        viewHolder.itemView.setScaleY(1);//1~0
        super.clearView(recyclerView, viewHolder);
    }

    //在拖拽的時候做效果
    @Override
    public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
        //dx  水平方向移動的增量(負:向左;正:向右)範圍:0~View.getWidth
        float alpha=1 - Math.abs(dX) / viewHolder.itemView.getWidth();
        if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
            //透明度動畫
            viewHolder.itemView.setAlpha(alpha);//1~0
            //縮放動畫
            viewHolder.itemView.setScaleX(alpha);//1~0
            viewHolder.itemView.setScaleY(alpha);//1~0
        }
//        if (alpha==0){
//            //透明度動畫
//            viewHolder.itemView.setAlpha(1);//1~0
//            //縮放動畫
//            viewHolder.itemView.setScaleX(1);//1~0
//            viewHolder.itemView.setScaleY(1);//1~0
//        }
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
    }
}

Demo地址:[https://download.csdn.net/download/huangxiaoguo1/10714254]