AndroID ListVIEw之EfficIEntAdapte的使用详解
在做AndroID手机应用开发时, ListVIEw是一个非常常用的控件。如何更新的使用它呢?其实SDK中的例子已经非常的完整了,并且能满足大多数的需要。
如果大家刚开始学习ListVIEw,我建议大家还是直接先看官方的例子好了,这样大家会学到更好的写法以及养成更好的习惯。
下面就以EfficIEntAdapter为例,看看官网例子是如何使用ListVIEw的:
请大家格外注意getVIEw的书写方法,大家可能从网上也能找到过一些其它的例子,但是网上的写法和官网不同,建议大家采用官网例子的写法。
简要说明:要实现高效的Adapter,需要做两件事:
1. 重用getVIEw()中的convertVIEw,避免在不必要的时候inflating VIEw。
2. 使用VIEwHolder模式,避免在不必要的时候调用findVIEwByID()。
顺便再提一句:若继承的是ListActivity,如果在layout xml里定义了ListVIEw,那么该ListVIEw的ID必须是"@ID/androID:List",最好再包含一个ID是"@ID/androID:empty"的TextVIEw,供ListVIEw中没有数据时,显示提示文字用。如下所示:
Xml代码
<?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" androID:paddingleft="8dp" androID:paddingRight="8dp"> <ListVIEw androID:ID="@ID/androID:List" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:background="#00FF00" androID:layout_weight="1" androID:drawSelectorOntop="false"/> <TextVIEw androID:ID="@ID/androID:empty" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:background="#FF0000" androID:text="No data"/> </linearLayout>
官网EfficIEntAdapter例子如下:
Java代码
/** * Demonstrates how to write an efficIEnt List adapter. The adapter used in this example binds * to an ImageVIEw and to a TextVIEw for each row in the List. * * To work efficIEntly the adapter implemented here uses two techniques: * - It reuses the convertVIEw passed to getVIEw() to avoID inflating VIEw when it is not necessary * - It uses the VIEwHolder pattern to avoID calling findVIEwByID() when it is not necessary * * The VIEwHolder pattern consists in storing a data structure in the tag of the vIEw returned by * getVIEw(). This data structures contains references to the vIEws we want to bind data to,thus * avoIDing calls to findVIEwByID() every time getVIEw() is invoked. */ public class List14 extends ListActivity { private static class EfficIEntAdapter extends BaseAdapter { private LayoutInflater mInflater; private Bitmap mIcon1; private Bitmap mIcon2; public EfficIEntAdapter(Context context) { // Cache the LayoutInflate to avoID asking for a new one each time. mInflater = LayoutInflater.from(context); // Icons bound to the rows. mIcon1 = BitmapFactory.decodeResource(context.getResources(),R.drawable.icon48x48_1); mIcon2 = BitmapFactory.decodeResource(context.getResources(),R.drawable.icon48x48_2); } /** * The number of items in the List is determined by the number of speeches * in our array. * * @see androID.Widget.listadapter#getCount() */ public int getCount() { return DATA.length; } /** * Since the data comes from an array,just returning the index is * sufficent to get at the data. If we were using a more complex data * structure,we would return whatever object represents one row in the * List. * * @see androID.Widget.listadapter#getItem(int) */ public Object getItem(int position) { return position; } /** * Use the array index as a unique ID. * * @see androID.Widget.listadapter#getItemID(int) */ public long getItemID(int position) { return position; } /** * Make a vIEw to hold each row. * * @see androID.Widget.listadapter#getVIEw(int,androID.vIEw.VIEw,* androID.vIEw.VIEwGroup) */ public VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) { // A VIEwHolder keeps references to children vIEws to avoID unneccessary calls // to findVIEwByID() on each row. VIEwHolder holder; // When convertVIEw is not null,we can reuse it directly,there is no need // to reinflate it. We only inflate a new VIEw when the convertVIEw supplIEd // by ListVIEw is null. if (convertVIEw == null) { convertVIEw = mInflater.inflate(R.layout.List_item_icon_text,null); // Creates a VIEwHolder and store references to the two children vIEws // we want to bind data to. holder = new VIEwHolder(); holder.text = (TextVIEw) convertVIEw.findVIEwByID(R.ID.text); holder.icon = (ImageVIEw) convertVIEw.findVIEwByID(R.ID.icon); convertVIEw.setTag(holder); } else { // Get the VIEwHolder back to get fast access to the TextVIEw // and the ImageVIEw. holder = (VIEwHolder) convertVIEw.getTag(); } // Bind the data efficIEntly with the holder. holder.text.setText(DATA[position]); holder.icon.setimageBitmap((position & 1) == 1 ? mIcon1 : mIcon2); return convertVIEw; } static class VIEwHolder { TextVIEw text; ImageVIEw icon; } } @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setlistadapter(new EfficIEntAdapter(this)); } private static final String[] DATA = { "Abbaye de Belloc","Abbaye du Mont des Cats","Abertam"}; }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持,如有疑问请留言或者到本站社区交流讨论,大家共同进步!
总结以上是内存溢出为你收集整理的Android ListView之EfficientAdapte的使用详解全部内容,希望文章能够帮你解决Android ListView之EfficientAdapte的使用详解所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)