ListView 与 RecyclerView的比较
listview与recyclerview在在app应用非常广泛,相对于其他的view(button textview)来说比较复杂,接下来我将讲一下创建的流程以及两者的不同。
代码来自《第一行代码》
秋天到了,果园大丰收了,现在着急的事情,就是把水果收集好放进仓库里。
listview
-
首先肯定要先把仓库准备好,腾一块地方出来,在布局中添加listview。
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <listview android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="match_parent" > </listview> </linearlayout>
-
把装水果的框子准备好,创建fruit_item布局。
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <imageview android:id="@+id/fruit_image" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <textview android:id="@+id/fruit_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginleft="10dp" /> </linearlayout>
主角登场啦,咱们的搬运工,创建类fruitadapter。
他需要干什么呢?
先贴出源码,下面解释
package com.example.listviewtest; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.imageview; import android.widget.textview; import java.util.list; public class fruitadapter extends arrayadapter<fruit> { private int resourceid; public fruitadapter(context context, int textviewresourceid, list<fruit> objects) { super(context, textviewresourceid, objects); resourceid = textviewresourceid; } @override public view getview(int position, view convertview, viewgroup parent) { fruit fruit = getitem(position); // 获取当前项的fruit实例 view view; viewholder viewholder; if (convertview == null) { view = layoutinflater.from(getcontext()).inflate(resourceid, parent, false); viewholder = new viewholder(); viewholder.fruitimage = (imageview) view.findviewbyid (r.id.fruit_image); viewholder.fruitname = (textview) view.findviewbyid (r.id.fruit_name); view.settag(viewholder); // 将viewholder存储在view中 } else { view = convertview; viewholder = (viewholder) view.gettag(); // 重新获取viewholder } viewholder.fruitimage.setimageresource(fruit.getimageid()); viewholder.fruitname.settext(fruit.getname()); return view; } class viewholder { imageview fruitimage; textview fruitname; } }
搬运工的工作就是,返回一个装满水果的框框
- 拿到一个水果
- 找到一个框框
- 把水果放到框框里
- 把框框返回(结局自然回到里仓库)
-
拿到一个水果
fruit fruit = getitem(position);
-
找到一个框框
view view; view = layoutinflater.from(getcontext()).inflate(resourceid, parent, false); imageview fruitimage = (imageview) view.findviewbyid(r.id.fruit_image) textview fruitname = (textview) view.findviewbyid(r.id.fruit_name)
-
把水果放到框框里
viewholder.fruitimage.setimageresource(fruit.getimageid()); viewholder.fruitname.settext(fruit.getname());
源码里面有两布优化,自己想想哦。
4.把框框返回
return view;
-
recylerview
还是老步骤,首先准备好仓库,在准备好框框
-
准备好仓库,腾一块地方出来
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.recyclerview android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" /> </linearlayout>
-
准备好框框,和上面一样
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" > <imageview android:id="@+id/fruit_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" /> <textview android:id="@+id/fruit_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="left" android:layout_margintop="10dp" /> </linearlayout>
-
请出我们更加聪明的搬运工了
先贴出源码,下面解释
package com.example.recyclerviewtest;
import android.support.v7.widget.recyclerview;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import android.widget.imageview;
import android.widget.textview;
import android.widget.toast;import java.util.list;
public class fruitadapter extends recyclerview.adapter<fruitadapter.viewholder>{
private list<fruit> mfruitlist; static class viewholder extends recyclerview.viewholder { view fruitview; imageview fruitimage; textview fruitname; public viewholder(view view) { super(view); fruitview = view; fruitimage = (imageview) view.findviewbyid(r.id.fruit_image); fruitname = (textview) view.findviewbyid(r.id.fruit_name); } } public fruitadapter(list<fruit> fruitlist) { mfruitlist = fruitlist; } @override public viewholder oncreateviewholder(viewgroup parent, int viewtype) { view view = layoutinflater.from(parent.getcontext()).inflate(r.layout.fruit_item, parent, false); final viewholder holder = new viewholder(view); holder.fruitview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { int position = holder.getadapterposition(); fruit fruit = mfruitlist.get(position); toast.maketext(v.getcontext(), "you clicked view " + fruit.getname(), toast.length_short).show(); } }); holder.fruitimage.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { int position = holder.getadapterposition(); fruit fruit = mfruitlist.get(position); toast.maketext(v.getcontext(), "you clicked image " + fruit.getname(), toast.length_short).show(); } }); return holder; } @override public void onbindviewholder(viewholder holder, int position) { fruit fruit = mfruitlist.get(position); holder.fruitimage.setimageresource(fruit.getimageid()); holder.fruitname.settext(fruit.getname()); } @override public int getitemcount() { return mfruitlist.size(); }
}
多了好多诶,其实,咱们聪明的搬运工制造了更多的工具,真是方便了好多。
现在搬运工只需要干两件事情了
- 找到一个水果
-
把水果放到框子里
public void onbindviewholder(viewholder holder, int position) { fruit fruit = mfruitlist.get(position); holder.fruitimage.setimageresource(fruit.getimageid()); holder.fruitname.settext(fruit.getname()); }
没错,只需要两部,其他的事情都靠聪明的搬运工制造出来的自动小车,送回到仓库了,是不是很酷。
-
现在创造出我们的工具
static class viewholder extends recyclerview.viewholder { view fruitview; imageview fruitimage; textview fruitname; public viewholder(view view) { super(view); fruitview = view; fruitimage = (imageview) view.findviewbyid(r.id.fruit_image); fruitname = (textview) view.findviewbyid(r.id.fruit_name); } }
这个工具记得他应该去仓库中腾出来的位置
-
再看看工具是怎么运作的
@override public viewholder oncreateviewholder(viewgroup parent, int viewtype) { view view = layoutinflater.from(parent.getcontext()).inflate(r.layout.fruit_item, parent, false); viewholder holder = new viewholder(view); return holder; }
首先他会和仓库联系一下,然后就会自动运回仓库啦。
推荐阅读
-
Kotlin 基础教程之注解与java中的注解比较
-
Oracle与SQL Server在企业应用的比较
-
android ListView内数据的动态添加与删除实例代码
-
ListView用法中与滚动相关的需求实现
-
linux shell中的比较符号与特殊符号介绍
-
Android ListView与ScrollView冲突的解决方法总结
-
HTML5中Canvas与SVG的画图原理比较
-
Linux中curl命令和wget命令的使用介绍与比较
-
java操作(DOM、SAX、JDOM、DOM4J)xml方式的四种比较与详解
-
ScrollView与ListView合用(正确计算Listview的高度)的问题解决