赞
踩
实现长按条目调整位置和侧滑删除功能,核心是ItemTouchHelper类,直接贴代码了。
DefaultItemTouchHelper
- public class DefaultItemTouchHelper extends ItemTouchHelper {
- private DefaultItemTouchHelperCallback mDefaultItemTouchHelperCallback;
- private OnItemMoveListenter itemMoveListenter;
-
-
- public DefaultItemTouchHelper() {
- this(new DefaultItemTouchHelperCallback());
- }
-
- private DefaultItemTouchHelper(DefaultItemTouchHelperCallback callback) {
- super(callback);
- this.mDefaultItemTouchHelperCallback = callback;
- }
-
- public void setItemMoveListenter(OnItemMoveListenter itemMoveListenter) {
- mDefaultItemTouchHelperCallback.setCallback(itemMoveListenter);
- }
-
- /**
- * 是否可以滑动删除
- * @param b
- */
- public void setItemViewSwipeEnabled(boolean b) {
- mDefaultItemTouchHelperCallback.setItemViewSwipeEnabled(b);
- }
- /**
- *是否可以长按
- */
- public void setLongPressDragEnabled(boolean b) {
- mDefaultItemTouchHelperCallback.setLongPressDragEnabled(b);
- }
- }
DefaultItemTouchHelperCallback
- public class DefaultItemTouchHelperCallback extends ItemTouchHelper.Callback {
- private OnItemMoveListenter callback;
- private boolean isLongPressDragEnabled;
- private boolean isItemViewSwipeEnabled;
-
- public void setCallback(OnItemMoveListenter callback) {
- this.callback = callback;
- }
- /**
- * 返回一个复合标志,该标志定义每个状态下启用的移动方向
- * @param recyclerView
- * @param viewHolder
- * @return
- */
- @Override
- public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
- RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
- if (layoutManager instanceof GridLayoutManager) {
- LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
- if (linearLayoutManager.getOrientation() == LinearLayoutManager.HORIZONTAL) {
- int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
- int swipeFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
- return makeMovementFlags(dragFlags, swipeFlags);
- } else {
- int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
- int swipeFlags = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
- return makeMovementFlags(dragFlags, swipeFlags);
- }
- } else if (layoutManager instanceof LinearLayoutManager) {
- LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
- if (linearLayoutManager.getOrientation() == LinearLayoutManager.HORIZONTAL) {
- int dragFlags = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
- int swipeFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
- return makeMovementFlags(dragFlags, swipeFlags);
- } else {
- int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
- int swipeFlags = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
- return makeMovementFlags(dragFlags, swipeFlags);
- }
- }
- return makeMovementFlags(0,0);
- }
-
- /**
- * 当ItemTouchHelper希望将拖动的项从原来的位置移动到新位置
- * @param recyclerView
- * @param viewHolder
- * @param target
- * @return
- */
- @Override
- public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
- if (callback!=null){
- return callback.onItemMove(viewHolder,target);
- }
- return false;
- }
-
- /**
- * 当用户滑动ViewHolder时调用
- * @param viewHolder
- * @param direction
- */
- @Override
- public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
- if (callback!=null){
- callback.onItemSwipe(viewHolder,direction);
- }
- }
-
- /**
- * 拖动过程中状态变化的监听
- * ItemTouchHelper.ACTION_STATE_IDLE--闲置状态
- * ItemTouchHelper.ACTION_STATE_SWIPE--刷新状态
- * ItemTouchHelper.ACTION_STATE_DRAG--拖动状态
- * @param viewHolder
- * @param actionState
- */
- @Override
- public void onSelectedChanged(@Nullable RecyclerView.ViewHolder viewHolder, int actionState) {
- super.onSelectedChanged(viewHolder, actionState);
- if (callback!=null && actionState != ItemTouchHelper.ACTION_STATE_IDLE){
- callback.onSelectedChanged(viewHolder,actionState);
- }
- }
-
- /**
- * 完成交互后,调用
- * @param recyclerView
- * @param viewHolder
- */
- @Override
- public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
- super.clearView(recyclerView, viewHolder);
- if (callback!=null ){
- callback.onSelectedChanged(viewHolder,ItemTouchHelper.ACTION_STATE_IDLE);
- }
- }
-
- public void setLongPressDragEnabled(boolean canDrag) {
- this.isLongPressDragEnabled = canDrag;
- }
-
- @Override
- public boolean isLongPressDragEnabled() {
- return isLongPressDragEnabled;
- }
-
- public void setItemViewSwipeEnabled(boolean canSwipe) {
- this.isItemViewSwipeEnabled = canSwipe;
- }
-
- @Override
- public boolean isItemViewSwipeEnabled() {
- return isItemViewSwipeEnabled;
- }
- }
OnItemMoveListenter
- public interface OnItemMoveListenter {
- boolean onItemMove(RecyclerView.ViewHolder srcHolder, RecyclerView.ViewHolder targetHolder);
- void onItemSwipe(RecyclerView.ViewHolder srcHolder, int direction);
- void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState);
- }
SwipeRecycleViewActivity
-
- public class SwipeRecycleViewActivity extends AppCompatActivity {
-
- private SwipeRecyclerView mRecyclerView;
- private RecycleItemAdapter mAdapter;
- private ArrayList<String> strings;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_swipe_recycle_view);
- mRecyclerView = findViewById(R.id.recycle_view);
- mRecyclerView.setLayoutManager(createLayoutManager());
- mRecyclerView.addItemDecoration(createItemDecoration());
- // 长按拖拽,默认关闭
- mRecyclerView.setLongPressDragEnabled(true);
- // 滑动删除,默认关闭
- mRecyclerView.setItemViewSwipeEnabled(true);
- mRecyclerView.setOnItemMoveListener(new OnItemMoveListenter() {
- @Override
- public boolean onItemMove(RecyclerView.ViewHolder srcHolder, RecyclerView.ViewHolder targetHolder) {
- if (srcHolder.getItemViewType() != targetHolder.getItemViewType()) {
- return false;
- }
- int fromPosition = srcHolder.getAdapterPosition();
- int toPosition = targetHolder.getAdapterPosition();
- Collections.swap(strings, fromPosition, toPosition);
- mAdapter.notifyItemMoved(fromPosition, toPosition);
- // 返回true表示处理了并可以换位置,返回false表示你没有处理并不能换位置
- return true;
- }
-
- @Override
- public void onItemSwipe(RecyclerView.ViewHolder srcHolder, int direction) {
- //左滑、右滑 删除,如果要一个方向删除,getMovementFlags方法中处理
- int adapterPosition = srcHolder.getAdapterPosition();
- mAdapter.notifyItemRemoved(adapterPosition);
- }
-
- @Override
- public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
- if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
- // 拖拽的时候添加一个背景
- viewHolder.itemView.setBackgroundColor(ContextCompat.getColor(SwipeRecycleViewActivity.this, R.color.white_pressed));
- } else if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
- } else if (actionState == ItemTouchHelper.ACTION_STATE_IDLE) {
- // 在手松开的时候还原背景
- ViewCompat.setBackground(viewHolder.itemView, ContextCompat.getDrawable(SwipeRecycleViewActivity.this, R.color.white));
- }
- }
- });
- makeDataList();
- mAdapter = new RecycleItemAdapter(this,strings);
- mRecyclerView.setAdapter(mAdapter);
- }
- private ArrayList<String> makeDataList(){
- strings = new ArrayList<>();
- for (int i=0;i<10;i++){
- strings.add("条目-"+i);
- }
- return strings;
- }
- /**
- * 布局管理类
- *
- * @return
- */
- protected RecyclerView.LayoutManager createLayoutManager() {
- return new LinearLayoutManager(this);
- }
-
- /**
- * 条目效果类
- * @return
- */
- protected RecyclerView.ItemDecoration createItemDecoration() {
- return new SimplePaddingDecoration(this);
- }
- /**
- * 添加条目的间隔效果
- */
- public class SimplePaddingDecoration extends RecyclerView.ItemDecoration {
- private int dividerHeight;
-
- public SimplePaddingDecoration(Context context) {
- dividerHeight = 5;
- }
-
- @Override
- public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
- super.getItemOffsets(outRect, view, parent, state);
- //类似加了一个bottom padding
- outRect.bottom = dividerHeight;
- }
- }
- }
RecyclerItemAdapter
-
- public class RecyclerItemAdapter extends RecyclerView.Adapter<RecyclerItemAdapter.ViewHolder> {
- private ArrayList<String> mData;
- private Context mContext;
-
- public RecyclerItemAdapter(Context mContext, ArrayList<String> mData) {
- this.mData = mData;
- this.mContext = mContext;
- }
-
- @NonNull
- @Override
- public RecyclerItemAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
- ViewHolder viewHolder = new ViewHolder(View.inflate(mContext, R.layout.item_drag_touch, null));
- return viewHolder;
- }
-
- @Override
- public void onBindViewHolder(@NonNull RecyclerItemAdapter.ViewHolder holder, int position) {
- holder.tvTitle.setText(mData.get(position));
- }
-
- @Override
- public int getItemCount() {
- return mData.size();
- }
- static class ViewHolder extends RecyclerView.ViewHolder{
- private TextView tvTitle;
- public ViewHolder(@NonNull View itemView) {
- super(itemView);
- tvTitle=itemView.findViewById(R.id.tv_title);
- }
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".activity.RecycleViewDragItemActivity">
- <androidx.recyclerview.widget.RecyclerView
- android:id="@+id/recycle_view"
- android:background="@color/white"
- android:layout_width="match_parent"
- android:layout_height="match_parent"/>
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="5dp"
- android:layout_marginBottom="5dp"
- android:background="@drawable/select_white"
- android:padding="15dp">
-
- <TextView
- android:id="@+id/tv_title"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:layout_marginLeft="10dp"
- android:textSize="18dp"/>
-
- </RelativeLayout>
- <color name="white_pressed">#FFCFCFCF</color>
- <color name="white">#fff</color>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。