ItemTouchHelper实现拖拽和侧滑删除
2016-06-03 15:00
543 查看
转载: https://github.com/iPaulPro/Android-ItemTouchHelper-Demo/
1. 编写item布局文件:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/item" android:layout_width="match_parent" android:layout_height="?listPreferredItemHeight" android:clickable="true" android:focusable="true" android:foreground="?selectableItemBackground"> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="16dp" android:textAppearance="?android:attr/textAppearanceMedium" /> <ImageView android:id="@+id/handle" android:layout_width="?listPreferredItemHeight" android:layout_height="match_parent" android:layout_gravity="center_vertical|right" android:scaleType="center" android:src="@drawable/ic_reorder_grey_500_24dp" /> </FrameLayout>
2.编写一个触发滑动的监听
/** * Listener for manual initiation of a drag. */ public interface OnStartDragListener { /** * Called when a view is requesting a start of a drag. * * @param viewHolder The holder of the view to drag. */ /*自定义滑动或拖拽事件的开启回调方法*/ void onStartDrag(RecyclerView.ViewHolder viewHolder); }
3.编写通知Callback监听器item处于激活状态的监听
/** * Interface to notify an item ViewHolder of relevant callbacks from {@link * ItemTouchHelper.Callback}. */ public interface ItemTouchHelperViewHolder { /** * Called when the {@link ItemTouchHelper} first registers an item as being moved or swiped. * Implementations should update the item view to indicate it's active state. */ /*当item被拖拽或者滑动的时候, item背景变为激活状态*/ void onItemSelected(); /** * Called when the {@link ItemTouchHelper} has completed the move or swipe, and the active item * state should be cleared. */ /*当滑动或拖拽完成后,激活的item的背景应恢复为原来状态*/ void onItemClear(); }
4. 监听item滑动或者拖拽:
/** * Interface to listen for a move or dismissal event from a {@link ItemTouchHelper.Callback}. */ public interface ItemTouchHelperAdapter { /** * Called when an item has been dragged far enough to trigger a move. This is called every time * an item is shifted, and <strong>not</strong> at the end of a "drop" event.<br/> * <br/> * Implementations should call {@link RecyclerView.Adapter#notifyItemMoved(int, int)} after * adjusting the underlying data to reflect this move. * * @param fromPosition The start position of the moved item. * @param toPosition Then resolved position of the moved item. * @return True if the item was moved to the new adapter position. * * @see RecyclerView#getAdapterPositionFor(RecyclerView.ViewHolder) * @see RecyclerView.ViewHolder#getAdapterPosition() */ /*item被拖拽时调用*/ boolean onItemMove(int fromPosition, int toPosition); /** * Called when an item has been dismissed by a swipe.<br/> * <br/> * Implementations should call {@link RecyclerView.Adapter#notifyItemRemoved(int)} after * adjusting the underlying data to reflect this removal. * * @param position The position of the item dismissed. * * @see RecyclerView#getAdapterPositionFor(RecyclerView.ViewHolder) * @see RecyclerView.ViewHolder#getAdapterPosition() */ /*item滑动删除时调用*/ void onItemDismiss(int position); }
5. 编写RecyclerView.Adapter实现类:
/** * Simple RecyclerView.Adapter that implements {@link ItemTouchHelperAdapter} to respond to move and * dismiss events from a {@link android.support.v7.widget.helper.ItemTouchHelper}. */ public class RecyclerListAdapter extends RecyclerView.Adapter<RecyclerListAdapter.ItemViewHolder> implements ItemTouchHelperAdapter { private final List<String> mItems = new ArrayList<>(); private final OnStartDragListener mDragStartListener; public RecyclerListAdapter(Context context, OnStartDragListener dragStartListener) { mDragStartListener = dragStartListener; mItems.addAll(Arrays.asList(context.getResources().getStringArray(R.array.dummy_items))); } @Override public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_main, parent, false); ItemViewHolder itemViewHolder = new ItemViewHolder(view); return itemViewHolder; } @Override public void onBindViewHolder(final ItemViewHolder holder, int position) { holder.textView.setText(mItems.get(position)); // Start a drag whenever the handle view it touched holder.handleView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) { mDragStartListener.onStartDrag(holder); } return false; } }); } @Override public void onItemDismiss(int position) { mItems.remove(position); notifyItemRemoved(position); } @Override public boolean onItemMove(int fromPosition, int toPosition) { Collections.swap(mItems, fromPosition, toPosition); notifyItemMoved(fromPosition, toPosition); return true; } @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 static class ItemViewHolder extends RecyclerView.ViewHolder implements ItemTouchHelperViewHolder { public final TextView textView; public final ImageView handleView; public ItemViewHolder(View itemView) { super(itemView); textView = (TextView) itemView.findViewById(R.id.text); handleView = (ImageView) itemView.findViewById(R.id.handle); } @Override public void onItemSelected() { itemView.setBackgroundColor(Color.LTGRAY); } @Override public void onItemClear() { itemView.setBackgroundColor(0); } } }
6. 编写Activity
public class RecylerViewListActivity extends AppCompatActivity implements OnStartDragListener{ private RecyclerView mRecyclerView; private ItemTouchHelper mItemTouchHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_swipe_recycler); mRecyclerView= (RecyclerView) findViewById(R.id.recycler); RecyclerListAdapter adapter = new RecyclerListAdapter(this, this); mRecyclerView.setHasFixedSize(true); mRecyclerView.setAdapter(adapter); mRecyclerView.setItemAnimator(new DefaultItemAnimator()); mRecyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL_LIST)); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(adapter); mItemTouchHelper = new ItemTouchHelper(callback); mItemTouchHelper.attachToRecyclerView(mRecyclerView); } @Override public void onStartDrag(RecyclerView.ViewHolder viewHolder) { //开启滑动或者拖拽 mItemTouchHelper.startDrag(viewHolder); } }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories