您好,登錄后才能下訂單哦!
Recyclerview現在基本已經替代Listview了,RecyclerView也越來越好用了 當我們有實現條目的拖拽排序和側滑刪除時 可以直接時候Recyclerview提供的API就可以直接實現了
先貼上主要代碼
private void initveiw() { ArrayList<String> items = new ArrayList<>(Arrays.asList("itme1", "item2", "itme3", "item4", "item5", "item6", "item7", "item8", "itme9", "item10", "itme11", "item12", "item13", "item14", "item15", "item16")); recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)); SimpleAdapter adapter = new SimpleAdapter(items); recyclerView.setAdapter(adapter); ItemTouchHelper helper = new ItemTouchHelper(new MyItemTouchCallback(adapter)); helper.attachToRecyclerView(recyclerView); } public class MyItemTouchCallback extends ItemTouchHelper.Callback{ private SimpleAdapter adapter; public MyItemTouchCallback(SimpleAdapter adapter) { this.adapter = adapter; } @Override public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { int dragFlag; int swipeFlag; RecyclerView.LayoutManager manager = recyclerView.getLayoutManager(); if (manager instanceof GridLayoutManager){ dragFlag = ItemTouchHelper.DOWN | ItemTouchHelper.UP | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT; swipeFlag = 0; }else{ dragFlag = ItemTouchHelper.DOWN | ItemTouchHelper.UP; swipeFlag = ItemTouchHelper.END | ItemTouchHelper.START; } return makeMovementFlags(dragFlag,swipeFlag); } @Override public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { int fromPosition = viewHolder.getAdapterPosition(); int toPosition = target.getAdapterPosition(); if (fromPosition < toPosition ){ for (int i = fromPosition ;i<toPosition ;i++){ Collections.swap(adapter.getDataList(),i,i+1); } }else{ for (int i= fromPosition; i>toPosition; i--){ Collections.swap(adapter.getDataList(),i ,i-1); } } recyclerView.getAdapter().notifyItemMoved(fromPosition,toPosition); return true; } @Override public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { int position = viewHolder.getAdapterPosition(); if (direction == ItemTouchHelper.END | direction==ItemTouchHelper.START){ adapter.getDataList().remove(position); adapter.notifyItemRemoved(position); } } @Override public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) { super.onSelectedChanged(viewHolder, actionState); if (actionState==ItemTouchHelper.ACTION_STATE_DRAG){ viewHolder.itemView.setBackgroundColor(Color.BLUE); } } @Override public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { super.clearView(recyclerView, viewHolder); viewHolder.itemView.setBackgroundColor(0); } }
步驟:
創建 ItemTouchHelper 對象時候,需要我們傳入一個實現了 ItemTouchHelper.Callback 接口的對象。而排序和刪除的邏輯都封裝在了這個 ItemTouchHelper.Callback 的對象里面了。
private void initveiw() { ArrayList<String> items = new ArrayList<>(Arrays.asList("itme1", "item2", "itme3", "item4", "item5", "item6", "item7", "item8", "itme9", "item10", "itme11", "item12", "item13", "item14", "item15", "item16")); recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)); SimpleAdapter adapter = new SimpleAdapter(items); recyclerView.setAdapter(adapter); ItemTouchHelper helper = new ItemTouchHelper(new MyItemTouchCallback(adapter)); helper.attachToRecyclerView(recyclerView); }
實現ItemTouchHelper.Callback 接口后有三個方法需要重寫:
getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) :設置滑動類型的標記。需要設置兩種類型的 flag ,即 dragFlags 和 swipeFlags ,分別代表著拖拽標記和滑動標記。最后需要調用 makeMovementFlags(dragFlags,
swipeFlags)方法來合成返回。
onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) :當用戶拖拽列表某個 item 時會回調。很明顯,拖拽排序的代碼應該在這個方法中實現。
onSwiped(RecyclerView.ViewHolder viewHolder, int direction) :當用戶滑動列表某個 item 時會回調。所以側滑刪除的代碼應該在這個方法中實現。
下面是重寫的幾個方法:
第一個,getMovementFlags方法
@Override public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { int dragFlag; int swipeFlag; RecyclerView.LayoutManager manager = recyclerView.getLayoutManager(); if (manager instanceof GridLayoutManager){ dragFlag = ItemTouchHelper.DOWN | ItemTouchHelper.UP | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT; swipeFlag = 0; }else{ dragFlag = ItemTouchHelper.DOWN | ItemTouchHelper.UP; swipeFlag = ItemTouchHelper.END | ItemTouchHelper.START; } return makeMovementFlags(dragFlag,swipeFlag); }
這個方法里面根據LayoutManager分了兩種情況,根據自己的情況去分
在GridLayoutManager中只能上下左右拖拽但是不能側滑刪除,所以swipFlag = 0;swipeFlag的值ItemTouchHelper.END是右滑刪除,ItemTouchHelper.START是左滑刪除
最后調用makeMovementFlags方法合成返回
第二個,onMove方法
@Override public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { int fromPosition = viewHolder.getAdapterPosition(); int toPosition = target.getAdapterPosition(); if (fromPosition < toPosition ){ for (int i = fromPosition ;i<toPosition ;i++){ Collections.swap(adapter.getDataList(),i,i+1); } }else{ for (int i= fromPosition; i>toPosition; i--){ Collections.swap(adapter.getDataList(),i ,i-1); } } recyclerView.getAdapter().notifyItemMoved(fromPosition,toPosition); return true; }
這個方法是用戶在拖拽 item 的時候調用。所以關于列表排序的代碼應該寫在這里。方法參數中的 viewHolder 代表的是用戶當前拖拽的 item ,而 target 代表的是被用戶拖拽所覆蓋的那個 item 。所以在 onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) 方法中的邏輯就是把 fromPosition 至 toPosition 為止改變它們的位置。
第三個,onSwiped方法
@Override public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { int position = viewHolder.getAdapterPosition(); if (direction == ItemTouchHelper.END | direction==ItemTouchHelper.START){ adapter.getDataList().remove(position); adapter.notifyItemRemoved(position); } }
這個方法是在用戶側滑的時候調用的 ,在里面對adapter的數據進行刪除就可以了
最后兩個方法
@Override public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) { super.onSelectedChanged(viewHolder, actionState); if (actionState==ItemTouchHelper.ACTION_STATE_DRAG){ viewHolder.itemView.setBackgroundColor(Color.BLUE); } } @Override public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { super.clearView(recyclerView, viewHolder); viewHolder.itemView.setBackgroundColor(0); } }
就是優化的方法,第一個是選擇條目改變狀態,第二個是手指抬起之后恢復條目狀態
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。