Android使用RecyclerView实现今日头条频道管理功能
使用过今日头条的伙计们对这个效果肯定很熟悉。拖拽可排序,点击标签后可以删除。今天我们采用recyclerview来实现。
实现思路:
通过itemtouchhelper来绑定recyclerview的子控件触摸事件。
当滑动拖拽的时候,通知适配器来交换两个子控件的显示位置。
更改数据源,使数据源与子空间显示内容一致。
这就是实现的基本思路,是不是很简单?当然,首先要了解一下itemtouchhelper这哥们儿是干啥的,有什么作用。
this is a utility class to add swipe to dismiss and drag & drop support to recyclerview.
it works with a recyclerview and a callback class, which configures what type of interactions are enabled and also receives events when user performs these actions.
depending on which functionality you support, you should override onmove(recyclerview, viewholder, viewholder) and / or onswiped(viewholder, int).
this class is designed to work with any layoutmanager but for certain situations, it can be optimized for your custom layoutmanager by extending methods in the itemtouchhelper.callback class or implementing itemtouchhelper.viewdrophandler interface in your layoutmanager.
by default, itemtouchhelper moves the items' translatex/y properties to reposition them. you can customize these behaviors by overriding onchilddraw(canvas, recyclerview, viewholder, float, float, int, boolean) or onchilddrawover(canvas, recyclerview, viewholder, float, float, int, boolean).
most of the time you only need to override onchilddraw.
通过api文档的介绍,这个哥们儿是为recyclerview工作的,他需要一个callback,可以回调recyclerview的子控件滑动和拖拽事件,而且也可以通过这个callback重绘我们的子view。这就一目了然了嘛。通过使用itemtouchhelper,可以很轻松的就实现了recyclerview触摸事件的回调。换句话说,只要我们为recyclerview 绑定了itemtouchhelper之后,recyclerview子控件的拖动和滑动事件已经帮我们实现了。我们所要做的就是在触摸事件之后,如何去改变去更新adapter和改变我们的数据。
开始撸码:
public class channelactivity extends activity { public recyclerview rv; public list<databean> list = new arraylist<>(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_channel); initdata(); initview(); } private void initview() { rv = (recyclerview) findviewbyid(r.id.rl_view); rv.setlayoutmanager(new gridlayoutmanager(this, 4)); myadapter adapter = new myadapter(this, list); rv.setadapter(adapter); //关联itemtouchhelper itemtouchhelper touchhelper = new itemtouchhelper(new myitemtouchcallback(adapter)); touchhelper.attachtorecyclerview(rv); } private void initdata() { databean bean1 = new databean("体育", 0, "url"); databean bean2 = new databean("新闻", 1, "url"); databean bean3 = new databean("影视", 2, "url"); databean bean4 = new databean("电视剧", 3, "url"); databean bean5 = new databean("热点", 4, "url"); databean bean6 = new databean("推荐", 5, "url"); databean bean7 = new databean("屌丝男士", 6, "url"); databean bean8 = new databean("音乐", 7, "url"); databean bean9 = new databean("电影", 8, "url"); list.add(bean1); list.add(bean2); list.add(bean3); list.add(bean4); list.add(bean5); list.add(bean6); list.add(bean7); list.add(bean8); list.add(bean9); } }
自定义myitemtouchcallback
public class myitemtouchcallback extends itemtouchhelper.callback { private touchinterface touchinterface; public myitemtouchcallback(touchinterface touchinterface) { this.touchinterface = touchinterface; } @override public int getmovementflags(recyclerview recyclerview, recyclerview.viewholder viewholder) { //拖拽 int dragflags = itemtouchhelper.up | itemtouchhelper.down | itemtouchhelper.right | itemtouchhelper.left; //滑出屏幕 int swipeflags = itemtouchhelper.right | itemtouchhelper.left | itemtouchhelper.up | itemtouchhelper.down; return makemovementflags(dragflags, 0); } @override public boolean onmove(recyclerview recyclerview, recyclerview.viewholder viewholder, recyclerview.viewholder target) { int position_target = target.getlayoutposition(); int position = viewholder.getlayoutposition(); //滑动事件回调到了adapter,用来处理数据 touchinterface.onmove(position, position_target); return true; } //标签动画持续时间,默认是250 @override public long getanimationduration(recyclerview recyclerview, int animationtype, float animatedx, float animatedy) { return super.getanimationduration(recyclerview, animationtype, animatedx, animatedy); } /** * 是否可以长按拖拽,默认是true * * @return */ @override public boolean islongpressdragenabled() { return super.islongpressdragenabled(); } /** * 标签划出去的回调,direction是滑动的方向 * * @return */ @override public void onswiped(recyclerview.viewholder viewholder, int direction) { } }
adapter类中处理数据
public class myadapter extends recyclerview.adapter<myviewholder> implements touchinterface { private context context; //是否显示delete public boolean isshow; public list<databean> getlist() { return list; } public void setlist(list<databean> list) { this.list = list; } private list<databean> list; public myadapter(context context, list<databean> list) { this.context = context; this.list = list; } @override public myviewholder oncreateviewholder(viewgroup parent, int viewtype) { myviewholder viewholder = new myviewholder(layoutinflater.from(context).inflate(r.layout.item_layout, parent, false)); return viewholder; } @override public void onbindviewholder(myviewholder holder, final int position) { holder.tv_des.settext(list.get(position).name); holder.tv_des.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { databean bean = list.remove(position); notifydatasetchanged(); toast.maketext(context,"删除了"+bean.name+"频道",toast.length_short).show(); } }); holder.tv_des.setonlongclicklistener(new view.onlongclicklistener() { @override public boolean onlongclick(view v) { isshow = true; notifydatasetchanged(); return true; } }); if (isshow) { holder.iv_icon.setvisibility(view.visible); } else { holder.iv_icon.setvisibility(view.gone); } } @override public int getitemcount() { return list.size(); } @override public void onmove(int currentposition, int targetposition) { collections.swap(list, currentposition, targetposition); if (targetposition < currentposition) { list<databean> sublist = list.sublist(targetposition + 1, currentposition + 1); //向右移一位 rightsteplist(0, sublist); } else { list<databean> sublist = list.sublist(currentposition, targetposition); //向左移一位 leftsteplist(0, sublist); } notifyitemmoved(currentposition, targetposition); } } class myviewholder extends recyclerview.viewholder { public imageview iv_icon; public textview tv_des; public myviewholder(view itemview) { super(itemview); iv_icon = (imageview) itemview.findviewbyid(r.id.iv_icon); tv_des = (textview) itemview.findviewbyid(r.id.tv_des); } }
解释一下onmove方法,例如:我们的数据是[1,2,3,4,5,6],当6移动到3的位置时,那么数据源最后变化为[1,2,6,3,4,5]。但是在显示的时候我们先是将当前position和targetposition对调[1,2,6,4,5,3],然后取出[4,5,3]进行右移一位,这样数据源就对上了。如果是从3移动到6进行左移就可以了,数据排序的算法,采用反转的思想。
public class datautils { /** * 利用反转的思想对数据进行排序 * 例如:list{0,1,2,3,4,5,6,7} 左移一位 * 第一步:第一位先反转{0,1,2,3,4,5,6,7} * 第二部:剩下的在反转{0,7,6,5,4,3,2,1} * 第三步:全部反转{1,2,3,4,5,6,7,0} * * 例如:list{0,1,2,3,4,5,6,7} 右移一位 * 第一步:最右边一位先反转{1,2,3,4,5,6,7} * 第二部:剩下的在反转{6,5,4,3,2,1,0,7} * 第三步:全部反转{7,6,5,4,3,2,1,0} * * 因为list的index是从0开始的,step要相应的-1 * 优点:少创建对象,优化内存 * * @param start * @param end * @param list */ public static void reverselist(int start,int end,list list){ int count = (end+1-start)/2 ; for(int i = 0;i< count;i++){ object temp = list.get(start+i); list.set(start+i,list.get(end-i)); list.set(end-i,temp); } } public static void leftsteplist(int step,list list){ int size = list.size() -1; //左移 reverselist(0,step,list); reverselist(step+1,size,list); reverselist(0,size,list); } public static void rightsteplist(int step,list list){ int size = list.size() -1; //右移 reverselist(size-step,size,list); reverselist(0,size-step-1,list); reverselist(0,size,list); } }
activity布局文件:
<?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:id="@+id/activity_channel" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.iwintrue.channe.channelactivity"> <android.support.v7.widget.recyclerview android:id="@+id/rl_view" android:background="@color/white" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v7.widget.recyclerview> </relativelayout>
子控件布局文件:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" > <relativelayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp"> <textview android:id="@+id/tv_des" android:layout_width="80dp" android:layout_height="30dp" android:text="屌丝男士" android:gravity="center" android:background="@drawable/rl_shape" android:textcolor="@color/textcolor" android:layout_margintop="5dp" android:layout_marginright="5dp" /> <imageview android:layout_alignright="@+id/tv_des" android:layout_marginright="-5dp" android:id="@+id/iv_icon" android:layout_width="20dp" android:layout_height="20dp" android:src="@mipmap/delete" android:scaletype="fitxy" android:visibility="gone" /> </relativelayout> </linearlayout>
实现效果:
github地址:https://github.com/zhoukai1526/channel
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。