关闭

ListView的优化

225人阅读 评论(0) 收藏 举报
分类:
package com.example.chenac.listviewtest;

import android.content.Context;
import android.media.Image;
import android.util.Log;
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;

/**
* Created by CHENAC on 10/8/2015.
*/
public class FruitAdapter extends ArrayAdapter<Fruit> {

   
private int resourceId;

   
public FruitAdapter(Context context, int textViewResourceId, List<Fruit> objects) {
       
super(context, textViewResourceId, objects);
       
resourceId=textViewResourceId;
        Log.
d(
"FruitAdapter","11111111111111" );
    }


   
//在每个子项被滚动到屏幕内的时候会被调用
  
@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, null);  //先取出一个单元行的View的布局

          
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;
    }

}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3037次
    • 积分:124
    • 等级:
    • 排名:千里之外
    • 原创:9篇
    • 转载:2篇
    • 译文:0篇
    • 评论:4条
    文章分类
    文章存档
    最新评论