欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android连载3-定制ListView的界面、性能优化以及绑定点击事件

程序员文章站 2022-05-01 21:08:44
一、订制ListView的界面 1.先建立一个Fruit水果类 package com.example.listviewtest; ​ ​ public class Fruit { private String name; private int imageId; public Fruit(Stri ......

一、订制listview的界面

1.先建立一个fruit水果类

 

package com.example.listviewtest;

​

​

public class fruit {

  private string name;

 

  private int imageid;

 

  public fruit(string name,int imageid) {

    this.name=name;

    this.imageid = imageid;

  }

  public string getname() {

    return name;

  }

  public int getimageid() {

    return imageid;

   

  }

}

 

2.然后建立一个展示水果的layout文件

 

<?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:orientation="vertical" >

   

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

    android:layout_marginleft="10dip" />

   

</linearlayout>

 

然后新建一个fruitadapter适配器,来继承arrayadapter

 

package com.example.listviewtest;

import android.widget.arrayadapter;

import android.widget.imageview;

import android.widget.textview;

import android.content.context;

import java.util.list;

​

import android.view.layoutinflater;

import android.view.view;

import android.view.viewgroup;

​

​

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 = layoutinflater.from(getcontext()).inflate(resourceid,null);

    imageview fruitimage = (imageview) view.findviewbyid(r.id.fruit_image);

    textview fruitname = (textview) view.findviewbyid(r.id.fruit_name);

    fruitimage.setimageresource(fruit.getimageid());

    fruitname.settext(fruit.getname());

    return view;

   

  }

}

 

最后我们完善我们的主程序

 

package com.example.listviewtest;

​

import java.util.list;

​

import android.app.activity;

import android.os.bundle;

import android.view.menu;

import android.view.menuitem;

import android.widget.arrayadapter;

import android.widget.listview;

import java.util.*;

//import java.lang.arrayadapter;

​

public class mainactivity extends activity {

 

  private list<fruit> fruitlist = new arraylist<fruit>();

 

//  private string[] data = {"apple","banana","orange","watermelon","pear","grape","pineapple","strawberry","cherry"};

 

  @override

  protected void oncreate(bundle savedinstancestate) {

    super.oncreate(savedinstancestate);

    setcontentview(r.layout.activity_main);

   

    initfruits();//初始化水果数据

    fruitadapter adapter = new fruitadapter(mainactivity.this,r.layout.fruit_item,fruitlist);

   

//    arrayadapter<string> adapter = new arrayadapter<string>(

//        mainactivity.this,android.r.layout.simple_list_item_1,data);

    listview listview = (listview) findviewbyid(r.id.list_view);

    listview.setadapter(adapter);

   

  }

  private void initfruits() {

    fruit apple = new fruit("apple",r.drawable.apple_pic);

    fruitlist.add(apple);

    fruit banana = new fruit("banana",r.drawable.banana_pic);

    fruitlist.add(banana);

    fruit orange =new fruit("orange",r.drawable.orange_pic);

    fruitlist.add(orange);

       

  }

}

 

这样运行我们的代码,得到如下的结果

 Android连载3-定制ListView的界面、性能优化以及绑定点击事件

 

二、提升listview的运行效率

1.我们通过下面的注释内容可以看出,这一段代码的重构,可以将画面的流畅程度提升,以及缓存一个view​holder对象就可以大幅度提升运行速度。

 

package com.example.listviewtest;

import android.widget.arrayadapter;

import android.widget.imageview;

import android.widget.textview;

import android.content.context;

import java.util.list;

​

import android.view.layoutinflater;

import android.view.view;

import android.view.viewgroup;

​

​

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 = layoutinflater.from(getcontext()).inflate(resourceid,null);

    //上面我们注释掉一个view实例,我们下一行先初始化一个view实例

    view view;

    viewholder viewholder;

    if(convertview == null) {//如果这个view不存在的话,我们就直接建一个view

      view = layoutinflater.from(getcontext()).inflate(resourceid,null);

      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;//如果view已经存在了,那么我们就复用已经存在了的view,这样就可以避免再次重新生成view,节省了运行时间

      viewholder = (viewholder) view.gettag();//调用gettag方法,把viewholder重新取出。

    }

    //这里我们建立了一个内部类,用来存储已经生成了的fruitimage和fruiename的实例,就省的每次都需要通过findviewbyid的方法,来去内存中查找目标实例

    viewholder.fruitimage.setimageresource(fruit.getimageid());

    viewholder.fruitname.settext(fruit.getname());

​

//    imageview fruitimage = (imageview) view.findviewbyid(r.id.fruit_image);

//    textview fruitname = (textview) view.findviewbyid(r.id.fruit_name);

//    fruitimage.setimageresource(fruit.getimageid());

//    fruitname.settext(fruit.getname());

    return view;

   

  }

  class viewholder{

    imageview fruitimage;

   

    textview fruitname;

  }

}

 

三、listview的点击事件

如果只列出了几条内容,我们不能通过点击​list来来获取我们想要知道的信息,那将是毫无意义的。

接下来就在mainactivity​中来注册我们的点击事件。

 

package com.example.listviewtest;

​

import java.util.list;

​

import android.app.activity;

import android.os.bundle;

import android.view.menu;

import android.view.menuitem;

import android.view.view;

import android.widget.adapterview;

import android.widget.adapterview.onitemclicklistener;

import android.widget.arrayadapter;

import android.widget.listview;

import android.widget.toast;

​

import java.util.*;

//import java.lang.arrayadapter;

​

public class mainactivity extends activity {

 

  private list<fruit> fruitlist = new arraylist<fruit>();

 

//  private string[] data = {"apple","banana","orange","watermelon","pear","grape","pineapple","strawberry","cherry"};

 

  @override

  protected void oncreate(bundle savedinstancestate) {

    super.oncreate(savedinstancestate);

    setcontentview(r.layout.activity_main);

   

    initfruits();//初始化水果数据

    fruitadapter adapter = new fruitadapter(mainactivity.this,r.layout.fruit_item,fruitlist);

   

//    arrayadapter<string> adapter = new arrayadapter<string>(

//        mainactivity.this,android.r.layout.simple_list_item_1,data);

    listview listview = (listview) findviewbyid(r.id.list_view);

    listview.setadapter(adapter);

   

    //接下来就来注册点击事件

    listview.setonitemclicklistener(new onitemclicklistener() {

      @override

      public void onitemclick(adapterview<?> parent,view view,int position,long id) {

        fruit fruit = fruitlist.get(position);

        toast.maketext(mainactivity.this, fruit.getname(), toast.length_short).show();

      }

    });

   

  }

  private void initfruits() {

    fruit apple = new fruit("apple",r.drawable.apple_pic);

    fruitlist.add(apple);

    fruit banana = new fruit("banana",r.drawable.banana_pic);

    fruitlist.add(banana);

    fruit orange =new fruit("orange",r.drawable.orange_pic);

    fruitlist.add(orange);

       

  }

}

 

Android连载3-定制ListView的界面、性能优化以及绑定点击事件

 

四、源码:

1.项目地址

https://github.com/ruigege66/android/tree/master/listviewtest

2.csdn:https://blog.csdn.net/weixin_44630050

3.博客园:https://www.cnblogs.com/ruigege0000/

4.欢迎关注微信公众号:傅里叶变换,个人公众号,仅用于学习交流,后台回复”礼包“,获取大数据学习资料

 Android连载3-定制ListView的界面、性能优化以及绑定点击事件