ListView的优化

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

}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Chen_Ace/article/details/49948881
个人分类: Android
上一篇listView的简单用法
下一篇广播机制
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭