文章目录
一、概述 这篇文章记录:
recyclerView的基本用法
为recyclerview添加点击事件
添加上拉加载更多
拖拽排序&滑动删除(初稿)
二、recyclerview基本用法:
写item和recyclerview布局
写Adapter
绑定数据、适配器、recyclerview
1、基本操作 1 2 3 4 5 6 7 8 9 10 11 12 mRecyclerView = findView(R.id.id_recyclerview); mRecyclerView.setLayoutManager(layout); mRecyclerView.setAdapter(adapter) mRecyclerView.setItemAnimator(new DefaultItemAnimator()); mRecyclerView.addItemDecoration(new DividerItemDecoration( getActivity(), DividerItemDecoration.HORIZONTAL_LIST)); mRecyclerView.setHasFixedSize(true );
2、适配器&ViewHolder
继承RecyclerView.Adapter
复写下面三个方法:
1 2 3 4 5 public RecyclerView.ViewHolder (ViewGroup viewGroup, int i) {}public void onBindViewHolder (RecyclerView.ViewHolder viewHolder, int i) {}public int getItemCount () {}
写ViewHolder继承RecyclerView.ViewHolder
3、Click和LongClick事件
1 2 3 public static interface OnRecyclerViewItemClickListener { void onItemClick (View view , String data) ; }
2、在onCreateViewHolder()中为每个item添加点击事件
1 2 3 4 5 6 7 8 @Override public ViewHolder (ViewGroup viewGroup, int viewType) { View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false ); ViewHolder vh = new ViewHolder(view); view.setOnClickListener(this ); return vh; }
3、将点击事件转移给外面的调用者:1 2 3 4 5 6 7 @Override public void onClick (View v) { if (mOnItemClickListener != null ) { mOnItemClickListener.onItemClick(v,(String)v.getTag()); } }
注意: onBindViewHolder()方法中设置和item相关的数据1 2 3 4 5 6 7 @Override public void onBindViewHolder (ViewHolder viewHolder, int position) { viewHolder.mTextView.setText(datas[position]); viewHolder.itemView.setTag(datas[position]); }
4、最后暴露给外面的调用者,定义一个设置Listener的方法()
1 2 3 public void setOnItemClickListener (OnRecyclerViewItemClickListener listener) { this .mOnItemClickListener = listener; }
4、上拉加载更多 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled (RecyclerView recyclerView, int dx, int dy) { super .onScrolled(recyclerView, dx, dy); int lastVisibleItem = ((LinearLayoutManager) mLayoutManager).findLastVisibleItemPosition(); int totalItemCount = mLayoutManager.getItemCount(); if (lastVisibleItem >= totalItemCount - 4 && dy > 0 ) { if (isLoadingMore){ Log.d(TAG,"ignore manually update!" ); } else { loadPage(); isLoadingMore = false ; } } } });
5、ItemTouchHelper 大专栏 recyclerview lity_class_to_add_swipe_to_dismiss_and_drag__26amp_3B_drop_support_to_RecyclerView_28from_google_docs_29" class="headerlink" title="This is a utility class to add swipe to dismiss and drag & drop support to RecyclerView(from google docs)">This is a utility class to add swipe to dismiss and drag & drop support to RecyclerView(from google docs)
类的大概声明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class ItemTouchHelper extends RecyclerView .ItemDecoration implements RecyclerView .OnChildAttachStateChangeListener { public ItemTouchHelper (Callback callback) { mCallback = callback; } public void attachToRecyclerView (RecyclerView recyclerView) {} public abstract static class Callback {} public abstract static class SimpleCallback extends Callback {} }
SimpleCallback
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 *dragDirs- 表示拖拽的方向,有六个类型的值:LEFT、RIGHT、START、END、UP、DOWN *swipeDirs- 表示滑动的方向,有六个类型的值:LEFT、RIGHT、START、END、UP、DOWN */ ItemTouchHelper.Callback mCallback = new ItemTouchHelper.SimpleCallback(int dragDirs, int swipeDirs) { * @param recyclerView * @param viewHolder 拖动的ViewHolder * @param target 目标位置的ViewHolder * @return */ @Override public boolean onMove (RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { return false ; } * @param viewHolder 滑动的ViewHolder * @param direction 滑动的方向 */ @Override public void onSwiped (RecyclerView.ViewHolder viewHolder, int direction) { } };
Examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 ItemTouchHelper.Callback mCallback = new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP|ItemTouchHelper.DOWN,ItemTouchHelper.RIGHT) { @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(datas, i, i + 1 ); } } else { for (int i = fromPosition; i > toPosition; i--) { Collections.swap(datas, i, i - 1 ); } } mAdapter.notifyItemMoved(fromPosition, toPosition); return true ; } @Override public void onSwiped (RecyclerView.ViewHolder viewHolder, int direction) { int position = viewHolder.getAdapterPosition(); datas.remove(position); mAdapter.notifyItemRemoved(position); } @Override public void onChildDraw (Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { super .onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) { final float alpha = 1 - Math.abs(dX) / (float )viewHolder.itemView.getWidth(); viewHolder.itemView.setAlpha(alpha); viewHolder.itemView.setTranslationX(dX); } } }; ItemTouchHelper itemTouchHelper = new ItemTouchHelper(mCallback); itemTouchHelper.attachToRecyclerView(mRecyclerView);
参考链接: https://github.com/devunwired/recyclerview-playground
https://github.com/AleBarreto/DragRecyclerView.git