废话不多说,下面展示一下效果。
这是GrIDVIEw主文件实现。
public class GrIDVIEwActivity extends AppCompatActivity { RecyclerVIEw mRecyclerVIEw; List<String> mStringList; RecyclerAdapter mRecyAdapter; @OverrIDe protected voID onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_recyclervIEw); initVIEw(); initRecy(); } private voID initVIEw() { getSupportActionbar().setdisplayHomeAsUpEnabled(true); mRecyclerVIEw = (RecyclerVIEw) findVIEwByID(R.ID.vIEw_recycler); } private voID initRecy() { if (mStringList == null) { mStringList = new ArrayList<>(); } mStringList.addAll(DataManager.getData(20 - mStringList.size())); mRecyAdapter = new RecyclerAdapter(R.layout.item_grIDvIEw,mStringList,true); mRecyclerVIEw.setLayoutManager(new GrIDLayoutManager(this,4)); mRecyclerVIEw.addItemdecoration(new divIDerGriItemdecoration(this)); mRecyclerVIEw.setHasFixedSize(true); RecyitemtouchhelperCallback itemtouchhelperCallback = new RecyitemtouchhelperCallback(mRecyAdapter,false,true); final itemtouchhelper itemtouchhelper = new itemtouchhelper(itemtouchhelperCallback); itemtouchhelper.attachToRecyclerVIEw(mRecyclerVIEw); mRecyclerVIEw.addOnItemtouchListener(new OnRecyclerItemClickListener(mRecyclerVIEw) { @OverrIDe public voID onItemClick(RecyclerVIEw.VIEwHolder vIEwHolder) { RecyclerAdapter.VIEwHolder vIEwHolder1 = (RecyclerAdapter.VIEwHolder) vIEwHolder; String tvString = vIEwHolder1.mTextVIEw.getText().toString(); Toast.makeText(GrIDVIEwActivity.this,"碰了一下 " + tvString,Toast.LENGTH_SHORT).show(); } @OverrIDe public voID onLongClick(RecyclerVIEw.VIEwHolder vIEwHolder) { RecyclerAdapter.VIEwHolder vIEwHolder1 = (RecyclerAdapter.VIEwHolder) vIEwHolder; String tvString = vIEwHolder1.mTextVIEw.getText().toString(); Toast.makeText(GrIDVIEwActivity.this,"长按不放可以拖动!",Toast.LENGTH_SHORT).show(); if (vIEwHolder.getLayoutposition() != 0) { itemtouchhelper.startDrag(vIEwHolder); } } }); mRecyclerVIEw.setAdapter(mRecyAdapter); } @OverrIDe public boolean onoptionsItemSelected(MenuItem item) { if (item.getItemID() == androID.R.ID.home) { finish(); } return super.onoptionsItemSelected(item); }}
代码不难理解,就是activity里面设置一大堆初始化的东西。RecyclerVIEw 初始化,setLayoutManager、addItemdecoration、setHasFixedSize、itemtouchhelper、addOnItemtouchListener、setAdapter。拖拽效果关键代码是itemtouchhelper.startDrag(vIEwHolder)。其他都围绕着它来转。
然后具体看看各个设置。
DataManager
public class DataManager { private static List<String> sstringList = Arrays.asList("语文","数学","英语","政治","历史","化学","生物","地理","体育","音乐"); public static final List<String> getData(int number) { List<String> stringList = new ArrayList<>(); for (int i = 0; i < number; i++) { stringList.add(sstringList.get(i % sstringList.size())); } return stringList; }}
divIDerGriItemdecoration 这个绘画了item的边线
public class divIDerGriItemdecoration extends RecyclerVIEw.Itemdecoration { private static final int[] ATTRS = new int[]{androID.R.attr.ListdivIDer}; private Drawable mdivIDer; private int linewidth = 1; public divIDerGriItemdecoration(GrIDVIEwActivity grIDVIEwActivity) { final TypedArray array = grIDVIEwActivity.obtainStyledAttributes(ATTRS); mdivIDer = array.getDrawable(0); array.recycle(); } public divIDerGriItemdecoration(int color) { mdivIDer = new colorDrawable(color); } @OverrIDe public voID onDraw(Canvas c,RecyclerVIEw parent,RecyclerVIEw.State state) { drawHorizontal(c,parent); drawVertical(c,parent); } private voID drawHorizontal(Canvas c,RecyclerVIEw parent) { int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { final VIEw child = parent.getChildAt(i); final RecyclerVIEw.LayoutParams params = (RecyclerVIEw.LayoutParams) child.getLayoutParams(); final int left = child.getleft() - params.leftmargin; final int right = child.getRight() + params.rightmargin + linewidth; final int top = child.getBottom() + params.bottommargin; final int bottom = top + linewidth; mdivIDer.setBounds(left,top,right,bottom); mdivIDer.draw(c); } } private voID drawVertical(Canvas c,RecyclerVIEw parent) { final int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { final VIEw child = parent.getChildAt(i); final RecyclerVIEw.LayoutParams params = (RecyclerVIEw.LayoutParams) child.getLayoutParams(); final int top = child.gettop() - params.topmargin; final int bottom = child.getBottom() + params.bottommargin; final int left = child.getRight() + params.rightmargin; final int right = left + linewidth; mdivIDer.setBounds(left,bottom); mdivIDer.draw(c); } } @OverrIDe public voID getItemOffsets(Rect outRect,VIEw vIEw,RecyclerVIEw.State state) { outRect.set(0,linewidth,linewidth); }}
RecyclerAdapter 适配器,和ui建立连接
public class RecyclerAdapter extends RecyclerVIEw.Adapter<RecyclerAdapter.VIEwHolder> { private int item_layout; private List<String> mDataList; private List<Integer> mInts; private boolean isFirstSpecial; public RecyclerAdapter(int item_grIDvIEw,List<String> dataList,boolean isFisrtSpecial) { this(item_grIDvIEw,dataList); this.isFirstSpecial = isFisrtSpecial; } public RecyclerAdapter(int item_grIDvIEw,List<String> dataList) { this.item_layout = item_grIDvIEw; this.mDataList = dataList; mInts = Arrays.asList(R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher); } @OverrIDe public VIEwHolder onCreateVIEwHolder(VIEwGroup parent,int vIEwType) { VIEw vIEw = LayoutInflater.from(parent.getContext()).inflate(item_layout,parent,false); return new VIEwHolder(vIEw); } @OverrIDe public voID onBindVIEwHolder(VIEwHolder holder,int position) { String str = mDataList.get(position); if (isFirstSpecial && position == 0) { holder.itemVIEw.setBackgroundcolor(color.LTGRAY); holder.mTextVIEw.setText("不许动"); holder.mImageVIEw.setimageResource(R.mipmap.ic_launcher); } else { holder.itemVIEw.setBackgroundcolor(color.WHITE); holder.mTextVIEw.setText(str); holder.mImageVIEw.setimageResource(mInts.get(position % mInts.size())); } } @OverrIDe public int getItemCount() { return mDataList == null ? 0 : mDataList.size(); } public List<String> getDataList() { return mDataList; } class VIEwHolder extends RecyclerVIEw.VIEwHolder { TextVIEw mTextVIEw; ImageVIEw mImageVIEw; public VIEwHolder(VIEw itemVIEw) { super(itemVIEw); mTextVIEw = itemVIEw.findVIEwByID(R.ID.tv_item); mImageVIEw = itemVIEw.findVIEwByID(R.ID.img_item); } }}
OnRecyclerItemClickListener,点击事件 。我们借用手势工具类GestureDetectorCompat 来 *** 作
public abstract class OnRecyclerItemClickListener implements RecyclerVIEw.OnItemtouchListener{ private GestureDetectorCompat mGestureDetectorCompat; private RecyclerVIEw mRecyclerVIEw; public OnRecyclerItemClickListener(RecyclerVIEw mRecyclerVIEw) { this.mRecyclerVIEw = mRecyclerVIEw; mGestureDetectorCompat = new GestureDetectorCompat(mRecyclerVIEw.getContext(),new itemtouchhelperGestureListener()); } @OverrIDe public boolean onIntercepttouchEvent(RecyclerVIEw rv,MotionEvent e) { mGestureDetectorCompat.ontouchEvent(e); return false; } @OverrIDe public voID ontouchEvent(RecyclerVIEw rv,MotionEvent e) { mGestureDetectorCompat.ontouchEvent(e); } @OverrIDe public voID onRequestdisallowIntercepttouchEvent(boolean disallowIntercept) { } //回调事件 public abstract voID onItemClick(RecyclerVIEw.VIEwHolder vIEwHolder); public abstract voID onLongClick(RecyclerVIEw.VIEwHolder vIEwHolder); private class itemtouchhelperGestureListener extends GestureDetector.SimpleOnGestureListener { @OverrIDe public boolean onSingleTapUp(MotionEvent e) { VIEw childVIEwUnder = mRecyclerVIEw.findChildVIEwUnder(e.getX(),e.getY()); if (childVIEwUnder != null) { RecyclerVIEw.VIEwHolder childVIEwHolder = mRecyclerVIEw.getChildVIEwHolder(childVIEwUnder); onItemClick(childVIEwHolder); } return true; } @OverrIDe public voID onLongPress(MotionEvent e) { VIEw childVIEwUnder = mRecyclerVIEw.findChildVIEwUnder(e.getX(),e.getY()); if (childVIEwUnder != null) { RecyclerVIEw.VIEwHolder childVIEwHolder = mRecyclerVIEw.getChildVIEwHolder(childVIEwUnder); onLongClick(childVIEwHolder); } } }}
RecyitemtouchhelperCallback
public class RecyitemtouchhelperCallback extends itemtouchhelper.Callback { RecyclerVIEw.Adapter mAdapter; boolean isSwipeEnable; boolean isFirstDragUnable; public RecyitemtouchhelperCallback(RecyclerVIEw.Adapter mAdapter) { this.mAdapter = mAdapter; isSwipeEnable = true; isFirstDragUnable = false; } public RecyitemtouchhelperCallback(RecyclerVIEw.Adapter mAdapter,boolean isSwipeEnable,boolean isFirstDragUnable) { this.mAdapter = mAdapter; this.isSwipeEnable = isSwipeEnable; this.isFirstDragUnable = isFirstDragUnable; }// 获取touch的响应方向 @OverrIDe public int getMovementFlags(RecyclerVIEw recyclerVIEw,RecyclerVIEw.VIEwHolder vIEwHolder) { if (recyclerVIEw.getLayoutManager() instanceof GrIDLayoutManager) {//网格布局时候 int dragFlags = itemtouchhelper.UP | itemtouchhelper.DOWN | itemtouchhelper.left | itemtouchhelper.RIGHT; int swipeFlags = 0; return makeMovementFlags(dragFlags,swipeFlags); } else {//List布局时候 int dragFlags = itemtouchhelper.UP | itemtouchhelper.DOWN; int swipeFlags = itemtouchhelper.START | itemtouchhelper.END; return makeMovementFlags(dragFlags,swipeFlags); } } @OverrIDe public boolean onMove(RecyclerVIEw recyclerVIEw,RecyclerVIEw.VIEwHolder vIEwHolder,RecyclerVIEw.VIEwHolder target) { int fromposition = vIEwHolder.getAdapterposition(); int toposition = target.getAdapterposition(); if (isFirstDragUnable && toposition == 0) { return false; } //重新更新排序 if (fromposition < toposition) { for (int i = fromposition; i < toposition; i++) { Collections.swap(((RecyclerAdapter) mAdapter).getDataList(),i,i + 1); } } else { for (int i = fromposition; i > toposition; i--) { Collections.swap(((RecyclerAdapter) mAdapter).getDataList(),i - 1); } } //刷新 mAdapter.notifyItemmoved(fromposition,toposition); return true; } /** * 侧滑删除后会回调的方法 */ @OverrIDe public voID onSwiped(RecyclerVIEw.VIEwHolder vIEwHolder,int direction) { int adapterposition = vIEwHolder.getAdapterposition(); mAdapter.notifyItemRemoved(adapterposition); ((RecyclerAdapter)mAdapter).getDataList().remove(adapterposition); } @OverrIDe public voID onSelectedChanged(RecyclerVIEw.VIEwHolder vIEwHolder,int actionState) { if (actionState != itemtouchhelper.ACTION_STATE_IDLE) { vIEwHolder.itemVIEw.setBackgroundcolor(color.LTGRAY); } super.onSelectedChanged(vIEwHolder,actionState); } @OverrIDe public voID clearVIEw(RecyclerVIEw recyclerVIEw,RecyclerVIEw.VIEwHolder vIEwHolder) { super.clearVIEw(recyclerVIEw,vIEwHolder); vIEwHolder.itemVIEw.setBackgroundcolor(color.WHITE); } @OverrIDe public boolean isLongPressDragEnabled() { return !isFirstDragUnable; } @OverrIDe public boolean isItemVIEwSwipeEnabled() { return isSwipeEnable; }}
下面是List的相关设置
同样,ListvIEw也是一顿初始化设置。
public class ListVIEwActivity extends AppCompatActivity { RecyclerVIEw mRecyclerVIEw; List<String> mStringList; RecyclerAdapter mRecyAdapter; @OverrIDe protected voID onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_recyclervIEw); initVIEw(); initRecy(); } private voID initRecy() { if (mStringList == null) { mStringList = new ArrayList<>(); } mStringList.addAll(DataManager.getData(15 - mStringList.size())); mRecyAdapter = new RecyclerAdapter(R.layout.item_ListvIEw,mStringList); mRecyclerVIEw.setLayoutManager(new linearlayoutmanager(this)); mRecyclerVIEw.addItemdecoration(new divIDerListItemdecoration(this,linearlayoutmanager.VERTICAL)); mRecyclerVIEw.setHasFixedSize(true); RecyitemtouchhelperCallback itemtouchhelperCallback = new RecyitemtouchhelperCallback(mRecyAdapter); final itemtouchhelper itemtouchhelper = new itemtouchhelper(itemtouchhelperCallback); itemtouchhelper.attachToRecyclerVIEw(mRecyclerVIEw); mRecyclerVIEw.addOnItemtouchListener(new OnRecyclerItemClickListener(mRecyclerVIEw) { @OverrIDe public voID onItemClick(RecyclerVIEw.VIEwHolder vIEwHolder) { RecyclerAdapter.VIEwHolder vIEwHolder1 = (RecyclerAdapter.VIEwHolder) vIEwHolder; String tvString = vIEwHolder1.mTextVIEw.getText().toString(); Toast.makeText(ListVIEwActivity.this,Toast.LENGTH_SHORT).show(); } @OverrIDe public voID onLongClick(RecyclerVIEw.VIEwHolder vIEwHolder) { Toast.makeText(ListVIEwActivity.this,Toast.LENGTH_SHORT).show(); } }); mRecyclerVIEw.setAdapter(mRecyAdapter); } private voID initVIEw() { getSupportActionbar().setdisplayHomeAsUpEnabled(true); mRecyclerVIEw = (RecyclerVIEw) findVIEwByID(R.ID.vIEw_recycler); } @OverrIDe public boolean onoptionsItemSelected(MenuItem item) { if (item.getItemID() == androID.R.ID.home) { finish(); } return super.onoptionsItemSelected(item); }}
divIDerListItemdecoration
public class divIDerListItemdecoration extends RecyclerVIEw.Itemdecoration { private static final int[] ATTRS = new int[] {androID.R.attr.ListdivIDer}; public static final int HORIZONTAL_List = linearlayoutmanager.HORIZONTAL; public static final int VERTICAL_List = linearlayoutmanager.VERTICAL; private Drawable mdivIDer; private int mOrIEntation; public divIDerListItemdecoration() { super(); } public divIDerListItemdecoration(Context context,int orIEntation) { final TypedArray array = context.obtainStyledAttributes(ATTRS); mdivIDer = array.getDrawable(0); array.recycle(); this.mOrIEntation = orIEntation; } public divIDerListItemdecoration(Context context,int orIEntation,int drawableID) { mdivIDer = ContextCompat.getDrawable(context,drawableID); setorIEntation(orIEntation); } @OverrIDe public voID onDraw(Canvas c,RecyclerVIEw.State state) { super.onDraw(c,state); } @OverrIDe public voID onDrawOver(Canvas c,RecyclerVIEw.State state) { super.onDrawOver(c,state); } @OverrIDe public voID getItemOffsets(Rect outRect,RecyclerVIEw.State state) { if (mOrIEntation == VERTICAL_List) { outRect.set(0,mdivIDer.getIntrinsicHeight()); } else { outRect.set(0,mdivIDer.getIntrinsicWIDth(),0); } super.getItemOffsets(outRect,vIEw,state); } public voID setorIEntation(int orIEntation) { if (orIEntation != HORIZONTAL_List && orIEntation != VERTICAL_List) { throw new IllegalArgumentException("invalID orIEntaion"); } mOrIEntation = orIEntation; }}
源码下载:http://download.csdn.net/download/loongago/9972876
总结
以上所述是小编给大家介绍的AndroID中RecyclerVIEw拖拽、侧删功能的实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!
总结以上是内存溢出为你收集整理的Android中RecyclerView拖拽、侧删功能的实现代码全部内容,希望文章能够帮你解决Android中RecyclerView拖拽、侧删功能的实现代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)