1. 程式人生 > >RecyclerView 實現側滑刪除和拖動排序

RecyclerView 實現側滑刪除和拖動排序

在使用ListView的時候實現拖動排序,主要靠繼承別人第三方的,在網上有很多例子,

這裡不介紹,主要是介紹一下recycleview的拖動排序和滑動刪除

一、主要知識點:

            1、 ItemTouchHelper.Callback的使用

             2、介面傳值

ItemTouchHelper.Callback

      getMovementFlags:回撥監聽時先呼叫的,用來判斷當前是什麼動作,比如判斷方向

      isLongPressDragEnabled  是否允許長按拖拽效果

      onMove   當移動的時候回撥的方法 拖拽

     onSelectedChanged  當前選中的條目拖拽或滑動時執行

    clearView  當用戶與itemview互動完且動畫執行完,呼叫

    onSwiped 側滑的時候回撥

    onChildDraw  在RecyclerView呼叫onDraw方法的時候,呼叫此方法

二、流程

    1、建立adapter繫結在RecyclerView上

     2、給RecyclerView繫結ItemTouchHelper

           ItemTouchHelper   itemTouchHelper = new ItemTouchHelper(new ItemTouchHelperCallback(myAdapter));
           itemTouchHelper.attachToRecyclerView(recyclerView);

    3、在構造ItemTouchHelper物件時候,需要實現它的Callback,可以直接建立,也可以建立Class實現

    4、繫結startDrag方法

    5、然後再ItemTouchHelper.Callback完成相應的操作

三、以下是程式碼:

     建立一個這樣的item代

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="80dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/item_test_imageview_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:src="@mipmap/logo_1" />


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center_vertical"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/item_test_textView_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="我是測試"
                    android:textColor="#000000"
                    android:textSize="16sp" />

                <TextView
                    android:id="@+id/item_test_textView_descirption"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:text="我是測試"
                    android:textColor="#858585"
                    android:textSize="14sp" />

            </LinearLayout>
        </LinearLayout>

        <TextView

            android:layout_width="100dp"
            android:layout_height="80dp"
            android:layout_alignParentRight="true"
            android:background="#f33213"
            android:gravity="center"
            android:text="刪除"
            android:textColor="#ffffff"
            android:textSize="16sp"
            android:visibility="gone" />
    </RelativeLayout>


    <TextView
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:background="#585858" />

</LinearLayout>

  建立ItemTouchHelperCallback類 public class ItemTouchHelperCallback extends ItemTouchHelper.Callback {
    private ItemTouchMoveListener moveListener;

    public ItemTouchHelperCallback(ItemTouchMoveListener moveListener) {
        this.moveListener = moveListener;
    }

    //Callback 回撥監聽時先呼叫的,用來判斷當前是什麼動作,比如判斷方向
    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        //up down ,left,right

        //拖拽方向是那兩個
        int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
        //0不監聽,側滑方向
        int swipeFlags = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;

        int flags = makeMovementFlags(dragFlags, swipeFlags);
        return flags;
    }

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

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

        return moveListener.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
    }

    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {

        if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
            viewHolder.itemView.setBackgroundColor(viewHolder.itemView.getContext().getResources().getColor(R.color.colorPrimary));

        }

        super.onSelectedChanged(viewHolder, actionState);
    }

    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        //恢復
        viewHolder.itemView.setBackgroundColor(Color.WHITE);
//        viewHolder.itemView.setAlpha(1);//1~0
//        viewHolder.itemView.setScaleX(1);
//        viewHolder.itemView.setScaleY(1);
        super.clearView(recyclerView, viewHolder);
    }

    //側滑的時候回撥
    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        //側滑監聽,呼叫notifyItemRemoved(position)
        moveListener.onItemRemove(viewHolder.getAdapterPosition());
    }

    @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);
            viewHolder.itemView.setScaleY(alpha);
        }

        if(alpha==0){
            viewHolder.itemView.setAlpha(1);//1~0
            viewHolder.itemView.setScaleX(1);
            viewHolder.itemView.setScaleY(1);
        }

        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
    }
}

 建立 adapter   

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements ItemTouchMoveListener {
    private List<MessageEntity> list;
    private StartDragInterface dragInterface;

    public MyAdapter(List<MessageEntity> list, StartDragInterface dragInterface) {
        this.list = list;
        this.dragInterface = dragInterface;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_test, parent, false));
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {

            ((MyViewHolder) holder).textView_name.setText(list.get(position).getName());
            ((MyViewHolder) holder).text_description.setText(list.get(position).getDescription());
            ((MyViewHolder) holder).imageView.setImageResource(list.get(position).getIcon());

            ((MyViewHolder) holder).imageView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        //按下
                        if (dragInterface != null) {
                            dragInterface.onStartDrag(holder);
                        }
                    }
                    return false;
                }
            });
        }
    }

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

    @Override
    public boolean onItemMove(int formPosition, int toPosition) {
        Collections.swap(list, formPosition, toPosition);
        notifyItemMoved(formPosition, toPosition);
        return true;
    }

    @Override
    public boolean onItemRemove(int position) {
        list.remove(position);
        notifyItemRemoved(position);
        return true;
    }


    private class MyViewHolder extends RecyclerView.ViewHolder {

        public ImageView imageView;
        public TextView textView_name, text_description;

        public MyViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.item_test_imageview_icon);
            textView_name = (TextView) itemView.findViewById(R.id.item_test_textView_name);
            text_description = (TextView) itemView.findViewById(R.id.item_test_textView_descirption);

        }
    }

public interface ItemTouchMoveListener {
    boolean onItemMove(int formPosition, int toPosition);

    boolean onItemRemove(int position);
}

public interface StartDragInterface {
        void onStartDrag(RecyclerView.ViewHolder viewHolder);
}

public class MessageEntity {
    private int icon;
    private String name;
    private String description;


    public MessageEntity(int icon, String name, String description) {
        this.icon = icon;
        this.name = name;
        this.description = description;
    }

    public int getIcon() {
        return icon;
    }

    public void setIcon(int icon) {
        this.icon = icon;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

public class MainActivity extends AppCompatActivity implements StartDragInterface {
    private RecyclerView recyclerView;
    private MyAdapter myAdapter;
    private List<MessageEntity> listData = new ArrayList<>();
    private ItemTouchHelper itemTouchHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = (RecyclerView) findViewById(R.id.activity_main_recyclerView);
        initData();
        myAdapter = new MyAdapter(listData,this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(myAdapter);
        //條目觸控幫助類
        itemTouchHelper = new ItemTouchHelper(new ItemTouchHelperCallback(myAdapter));
        itemTouchHelper.attachToRecyclerView(recyclerView);

//        itemTouchHelper.startDrag();
//        itemTouchHelper.startSwipe();
    }


    @Override
    public void onStartDrag(RecyclerView.ViewHolder viewHolder) {
        itemTouchHelper.startDrag(viewHolder);
    }

    private void initData() {
        for (int i = 0; i < 20; i++) {
            MessageEntity entity = new MessageEntity(R.mipmap.logo_1, "Test" + i, "我是描述我是描述" + i);
            listData.add(entity);
        }
    }

程式碼在我的資源中可以免費下載