RecycleViewはitem位置の自由交換と左スライド削除を実現

5377 ワード

ItemTouchHelperは、RecyclerViewを操作する際にスタックItemが長押しして移動し、左右にスライドして効果を削除する補助クラスです.
カスタムインタフェースitem交換と左スライド削除方法の書き出し
public interface ItemTouchHelperAdapter {

    //  
    void onItemMove(int fromPosition,int endPosition);
    //  
    void onItemDelete(int position);
}

まずクラス実装をカスタマイズします.Callbackインタフェース
public class SimpleItemTouchCallBack extends ItemTouchHelper.Callback{

    private ItemTouchHelperAdapter mAdapter;

    SimpleItemTouchCallBack(ItemTouchHelperAdapter adapter) {
        mAdapter = adapter;
    }

    @Override
    public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
        //          ,                    
        int dragFlag = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
        //      ,                 0  
        int moveFlag = ItemTouchHelper.LEFT;

        return makeMovementFlags(dragFlag,moveFlag);
    }

    @Override
    public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder viewHolder1) {
        mAdapter.onItemMove(viewHolder.getAdapterPosition(),viewHolder1.getAdapterPosition());
        return true;
    }

    @Override
    public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        mAdapter.onItemDelete(viewHolder.getAdapterPosition());
    }

    @Override
    public boolean isLongPressDragEnabled() {
        return true;
    }
    
    /**
     *   swipe  ,    swipe(  )  
     */
    @Override
    public boolean isItemViewSwipeEnabled() {
        return true;
    }
}

これらを書いておけば、コードの中で使うことができます.まず自分のRecycleView adapterを書き直します.
 class MyAdapter extends RecyclerView.Adapterimplements ItemTouchHelperAdapter{

        public MyAdapter() {
        }

        @NonNull
        @Override
        public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_item, viewGroup,false);
            return new MyHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull final MyHolder myHolder, int i) {

            myHolder.mTextView.setText(mList.get(i));
            myHolder.mTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = myHolder.getLayoutPosition();
                    Toast.makeText(MainActivity.this,"  "+position+"    ",Toast.LENGTH_SHORT).show();
                }
            });
            myHolder.mTextView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    int position = myHolder.getLayoutPosition();
                    Toast.makeText(MainActivity.this,"  "+position+"     ",Toast.LENGTH_SHORT).show();

                    return false;
                }
            });
        }

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

        @Override
        public void onItemMove(int fromPosition, int endPosition) {
            Collections.swap(mList,fromPosition,endPosition);
            notifyItemMoved(fromPosition,endPosition);
        }

        @Override
        public void onItemDelete(int position) {
            mList.remove(position);
            notifyItemRemoved(position);
        }
    }

    class MyHolder extends RecyclerView.ViewHolder{
        private TextView mTextView;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            mTextView = itemView.findViewById(R.id.id_num);
        }
    }
  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RecyclerView recyclerView = findViewById(R.id.recyclerView);

        mLayoutManager = new LinearLayoutManager(this);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(mLayoutManager);


        for (int i = 'A'; i < 'z'; i++) {
            mList.add(""+(char)i);
        }
        final MyAdapter myAdapter = new MyAdapter();
        recyclerView.setAdapter(myAdapter);
        recyclerView.setItemAnimator(new DefaultItemAnimator());


        SimpleItemTouchCallBack simpleItemTouchCallBack = new SimpleItemTouchCallBack(myAdapter);

        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallBack);
        itemTouchHelper.attachToRecyclerView(recyclerView);

        findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //                         
                mList.add(3,"Insert");
                myAdapter.notifyItemInserted(3);
            }
        });
        findViewById(R.id.delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //    ,        
                mList.remove(3);
                myAdapter.notifyItemRemoved(3);
            }
        });
    }