27
2017
09

RecyclerView使用ItemTouchHelper

What

**ItemTouchHelper**Google 官方对Item进行拖拽和滑动的工具类

Why

使用ItemTouchHelper可以实现Item拖拽排序,滑动删除等操作

How

  • 效果:

这里写图片描述

1.定义Adapter用到的接口

/** * 为Adapter设计的接口,当触发相应事件时,adapter做对应处理 */
public interface ItemTouchHelper4Adapter {
    /** * 当Item移动时触发 * @param fromPosition * @param toPosition */
    void onItemMove(int fromPosition, int toPosition);
}

2.定义拖拽回调接口

/** * 拖动监听 */
public interface OnStartDragListener {
    /** * 开始拖动 * @param viewHolder */
    void onStartDrag(RecyclerView.ViewHolder viewHolder);
}

3.继承ItemTouchHelper.CallBack,并重写方法

public class MySimpleItemTouchHelper extends ItemTouchHelper.Callback{

    private final ItemTouchHelper4Adapter mAdapter;

    public MySimpleItemTouchHelper(ItemTouchHelper4Adapter adapter) {
        mAdapter = adapter;
    }

    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        // 判断布局类型判断是否支持何种拖动
        if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
            final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
            final int swipeFlags = 0;
            return makeMovementFlags(dragFlags, swipeFlags);
        } else {
            final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
            final int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
            return makeMovementFlags(dragFlags, swipeFlags);
        }
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        if (viewHolder.getItemViewType() != target.getItemViewType()) {
            return false;
        }

        mAdapter.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
        return false;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        //书写对应滑动代码
    }
}

4.自定义Adapter

public class MyRecyclerGridAdapter extends RecyclerView.Adapter<MyRecyclerGridAdapter.ItemViewHolder> implements ItemTouchHelper4Adapter {

    private List<String> mItems;

    private final OnStartDragListener mDragStartListener;

    public MyRecyclerGridAdapter(Context context, OnStartDragListener dragStartListener) {
        mDragStartListener = dragStartListener;
        mItems = new ArrayList<String>(){
            {
                add("1");
                add("2");
                add("3");
                add("4");
                add("5");
                add("6");
                add("7");
                add("8");
                add("9");
                add("10");
            }
        };
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_image, parent, false);
        ItemViewHolder itemViewHolder = new ItemViewHolder(view);
        return itemViewHolder;
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, int position) {

    }

    public void removeItem(int position) {
        mItems.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, mItems.size());
    }

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

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

    /** * Simple example of a view holder that implements {@link ItemTouchHelperViewHolder} and has a * "handle" view that initiates a drag event when touched. */
    public class ItemViewHolder extends RecyclerView.ViewHolder {
        ImageView delImage;

        public ItemViewHolder(View itemView) {
            super(itemView);
            delImage = (ImageView) itemView.findViewById(R.id.delImage);
            delImage.setOnClickListener(deleteListener);
            itemView.setOnTouchListener(onTouchListener);
        }
        View.OnTouchListener onTouchListener = new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
                    mDragStartListener.onStartDrag(ItemViewHolder.this);
                }
                return false;
            }
        };

        View.OnClickListener deleteListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                removeItem(getAdapterPosition());
            }
        };
    }
}

5.对应的item_image.xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:focusable="true"
    android:gravity="center"
    >

    <RelativeLayout
        android:layout_width="110dp"
        android:layout_height="110dp">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:src="@drawable/bbb"
        />
    <ImageView
        android:id="@+id/delImage"
        android:layout_alignParentRight="true"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:scaleType="centerInside"
        android:src="@drawable/dialog_close"
        />
    </RelativeLayout>
</RelativeLayout>

6.注意点

  1. 使用点击事件时最好定义在ViewHolder中,不然会发生意想不到的事情…..(如果发现删除或者点击事件不对,不妨去检查一下是否将事件监听放在了onBindViewHolder中)
  2. ViewHolder中通过getAdapterPosition()获取对应数据,进行操作。
上一篇:表单验证 下一篇:Android MagicaSakura多主题框架