iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 17
0
Mobile Development

Android Studio 菜鳥筆記本系列 第 17

Android Studio 菜鳥筆記本-Day 17-ItemTouchHelper使用方法

  • 分享至 

  • xImage
  •  

ItemTouchHelper是用來幫助RecycleView做移動或是滑動的物件,今天就分享如何使用ItemTouchHelper讓RecycleView做上下移動、左右滑動的動作。

程式碼

public class ItemTouchHelperCallback extends ItemTouchHelper.Callback {
    private ItemTouchHelperAdapter ITHadapter;
    public ItemTouchHelperCallback(ItemTouchHelperAdapter adapter){
        this.ITHadapter=adapter;
    }
    @Override
    //支持長按ItemView,可執行上下拖曳
    public boolean isLongPressDragEnabled() {
        return true;
    }

    @Override
    //使用者左右滑動ItemView,可執行左右滑動刪除
    public boolean isItemViewSwipeEnabled() {
        return true;
    }
    @Override
    //設置滑動方向。
    public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
    //int dragFlags=ItemTouchHelper.UP|ItemTouchHelper.DOWN;設定可往上、往下移動,
    中間"|"代表上下都可以
    //int swipeFlags=0;設定可往左、往右滑動
        int ddd=ItemTouchHelper.UP|ItemTouchHelper.DOWN;
        int ccc=ItemTouchHelper.RIGHT|ItemTouchHelper.LEFT;
        return makeMovementFlags(ddd,ccc);
    }
    @Override
    //管理上下拖曳
    public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
        ITHadapter.onItemMove(viewHolder.getAdapterPosition(),target.getAdapterPosition());
        return true;
    }
    //設定滑動動作
    @Override
    public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
        ITHadapter.onItemDissmiss(viewHolder.getAdapterPosition());
    }
}

創建一個ItemTouchHelperAdapter與RecyclerAdapter的接口

public interface ItemTouchHelperAdapter {
    //Item交換
    void onItemMove(int FromPosition,int ToPosition);
    //Item刪除
    void onItemDissmiss(int Position);
}

將RecyclerAdapter引入ItemTouchHelperAdapter

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> implements ItemTouchHelperAdapter {
 @Override
     //Item拖曳時adapter的動作
    public void onItemMove(int FromPosition, int ToPosition) {
        Collections.swap(mitem,FromPosition,ToPosition);
        notifyItemMoved(FromPosition,ToPosition);
    }
    @Override
    //Item滑動時adapter的動作
    public void onItemDissmiss(int Position) {
        mitem.remove(Position);
        notifyItemRemoved(Position);
    }

回到MainActivity,將ItemTouchHelper.Callback、ItemTouchHelper綁定RecyclerAdapter,再將這些設定帶入RecyclerView

public class MainActivity extends AppCompatActivity {
    RecyclerAdapter adapter;
    String[] cal ={"January","February","March","April","May","June","July","August","September","October","November","December"};
    private ArrayList<String> mitem=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RecyclerV();
        for (int i=0; i < cal.length; i++){
            mitem.add(cal[i]);
        }
    }
    private void RecyclerV() {
        RecyclerView recycle =findViewById(R.id.recycle);
        recycle.setLayoutManager(new LinearLayoutManager(this));
        recycle.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        adapter=new RecyclerAdapter(this,mitem);
        recycle.setAdapter(adapter);
        ItemTouchHelper.Callback callback=new ItemTouchHelperCallback(adapter);
        ItemTouchHelper itemTouchHelper=new ItemTouchHelper(callback);
        itemTouchHelper.attachToRecyclerView(recycle);
    }
}

成果圖


上一篇
Android Studio 菜鳥筆記本-Day 16-元件介紹-RecyclerView
下一篇
Android Studio 菜鳥筆記本-Day 18-介紹SharedPreference
系列文
Android Studio 菜鳥筆記本30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言