Android 滑动监听RecyclerView线性流+左右划删除+上下移动
程序员文章站
2024-03-04 23:59:24
废话不多说了,直接给大家贴代码了。具体代码如下所示:
...
废话不多说了,直接给大家贴代码了。具体代码如下所示:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="c.example.jreduch10.recycler1activity"> <android.support.v7.widget.recyclerview android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/rv" ></android.support.v7.widget.recyclerview> </relativelayout> <span style="font-size:18px;">package c.example.jreduch10; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.linearlayoutmanager; import android.support.v7.widget.recyclerview; import android.support.v7.widget.helper.itemtouchhelper; import android.util.log; import android.view.view; import android.view.viewgroup; import android.widget.imageview; import android.widget.textview; import android.widget.toast; import java.util.arraylist; import java.util.list; import c.example.jreduch10.entity.video; import jp.wasabeef.recyclerview.adapters.alphainanimationadapter; import jp.wasabeef.recyclerview.adapters.scaleinanimationadapter; public class recycler1activity extends appcompatactivity { private recyclerview rv; private list<video> list; private adapter adapter; private itemtouchhelper itemtouchhelper; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_recycler1); rv=(recyclerview)findviewbyid(r.id.rv); list=new arraylist<>(); initdata(); adapter=new adapter(list); linearlayoutmanager llm=new linearlayoutmanager(this); llm.setorientation(linearlayoutmanager.vertical); rv.setlayoutmanager(llm); // rv.setadapter(adapter); //动画效果 alphainanimationadapter alphainanimationadapter=new alphainanimationadapter(adapter); rv.setadapter(new scaleinanimationadapter(alphainanimationadapter)); //默认动画效果 // rv.setitemanimator(new defaultitemanimator()); itemtouchhelper=createitemtouchhelper(); itemtouchhelper.attachtorecyclerview(rv); ////监听事件某一行 // recyclerviewitemlistener listener=new recyclerviewitemlistener(this, new recyclerviewitemlistener.onitemclicklistener() { // @override // public void onitemclick(view item, int adapterposition) { // toast.maketext(getbasecontext(),adapterposition+"",toast.length_short).show(); // } // }); // rv.addonitemtouchlistener(listener); } public itemtouchhelper createitemtouchhelper(){ itemtouchhelper=new itemtouchhelper(new itemtouchhelper.callback() { @override public int getmovementflags(recyclerview recyclerview, recyclerview.viewholder viewholder) { //拖拽时支持的方向向上向下 int dragflags=itemtouchhelper.up|itemtouchhelper.down; //滑动的时候支持的方向为左右 int swipeflags=itemtouchhelper.left|itemtouchhelper.right; //必须调用makemovementflags()方法通知 touchhelper支持的种类 return makemovementflags(dragflags,swipeflags); } //上下拖动回调次方法。 @override public boolean onmove(recyclerview recyclerview, recyclerview.viewholder viewholder, recyclerview.viewholder target) { adapter.move(viewholder.getadapterposition(),target.getadapterposition()); return true; } //左右滑动回调此方法。 @override public void onswiped(recyclerview.viewholder viewholder, int direction) { adapter.delete(viewholder.getadapterposition()); } @override public boolean isitemviewswipeenabled() { return super.isitemviewswipeenabled(); } @override public boolean islongpressdragenabled() { return super.islongpressdragenabled(); } }); return itemtouchhelper; } public void initdata(){ video video=new video(r.mipmap.zyf,"国产","........"); list.add(video); video=new video(r.mipmap.zyfzyf,"国产","........"); list.add(video); video=new video(r.mipmap.zyfzyfzyf,"国产","........"); list.add(video); video=new video(r.mipmap.zyf,"国产","........"); list.add(video); video=new video(r.mipmap.g,"国产","........"); list.add(video); video=new video(r.mipmap.ic_launcher,"国产","........"); list.add(video); video=new video(r.mipmap.zyf,"国产","........"); list.add(video); video=new video(r.mipmap.zyf,"国产","........"); list.add(video); video=new video(r.mipmap.zyfzyf,"国产","........"); list.add(video); video=new video(r.mipmap.zyfzyfzyf,"国产","........"); list.add(video); video=new video(r.mipmap.zyf,"国产","........"); list.add(video); video=new video(r.mipmap.g,"国产","........"); list.add(video); video=new video(r.mipmap.ic_launcher,"国产","........"); list.add(video); video=new video(r.mipmap.zyf,"国产","........"); list.add(video); } public class adapter extends recyclerview.adapter<viewholder>{ private list<video> mdata; public adapter(list<video> mdata){ this.mdata=mdata; } //左右滑动时调用此方法 public void delete(int position){ mdata.remove(position); notifyitemremoved(position); } //上下拖动时调用此方法 public void move(int from,int to){ video video=mdata.remove(from); mdata.add(to>from?to-1:to,video); notifyitemmoved(from,to); } @override public int getitemcount() { return mdata.size(); } @override public viewholder oncreateviewholder(viewgroup parent, int viewtype) { view view=getlayoutinflater().inflate(r.layout.recycle1_linear_layout,parent,false); viewholder vh=new viewholder(view); return vh; } @override public void onbindviewholder(viewholder holder, int position) { video video=mdata.get(position); holder.iv.setimageresource(video.getimg()); holder.title.settext(video.gettitle()); holder.content.settext(video.getcontent()); } } public class viewholder extends recyclerview.viewholder{ imageview iv; textview title; textview content; public viewholder(view itemview) { super(itemview); iv= (imageview) itemview.findviewbyid(r.id.iv); title=(textview)itemview.findviewbyid(r.id.title); content=(textview)itemview.findviewbyid(r.id.content); //监听事件某一项 itemview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { int position=getadapterposition(); log.d("9999999999","uuuuu"); toast.maketext(getbasecontext(),title.gettext()+":"+position,toast.length_short).show(); } }); } } } </span>
以上所述是小编给大家介绍的android 滑动监听recyclerview线性流+左右划删除+上下移动,希望对大家有所帮助