Android监听左右滑删除通知,Android 滑动监听RecyclerView线性流+左右划删除+上下移动...

废话不多说了,直接给大家贴代码了。具体代码如下所示:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context="c.example.jreduch10.recycler1activity">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:id="@+id/rv"

>

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 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{

private list mdata;

public adapter(list 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();

}

});

}

}

}

aa67edcb352134ba023c71f91a03a8cc.png

567091ad15bd34fca5181242e982d29a.png

8b7c92a6050300173d05bb2d85037884.png

以上所述是小编给大家介绍的android 滑动监听recyclerview线性流+左右划删除+上下移动,希望对大家有所帮助

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值