自定义listView中的持有者是如何创建的?

时间:2014-02-18 09:15:04

标签: android android-listview

我在以下链接中看到了自定义列表视图的程序 http://www.ezzylearning.com/tutorial.aspx?tid=1763429&q=customizing-android-listview-items-with-custom-arrayadapter

这是定制适配器:

public class WeatherAdapter extends ArrayAdapter<Weather>{

    Context context; 
    int layoutResourceId;    
    Weather data[] = null;

    public WeatherAdapter(Context context, int layoutResourceId, Weather[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        WeatherHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new WeatherHolder();
            holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
            holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);

            row.setTag(holder);
        }
        else
        {
            holder = (WeatherHolder)row.getTag();
        }

        Weather weather = data[position];
        holder.txtTitle.setText(weather.title);
        holder.imgIcon.setImageResource(weather.icon);

        return row;
    }

    static class WeatherHolder
    {
        ImageView imgIcon;
        TextView txtTitle;
    }
}

getView() method中,他为WeatherHolder 创建了此WeatherHolder类的对象?

  1. 它是如何创建的?
  2. 是我们手动创建的吗?
  3.   

    因为我无法找到“WeatherHolder”类的主体   否则在程序中。

    我希望我的问题很明确。什么是程序中的WeatherHolder,谁创建了WatchHolder类。

1 个答案:

答案 0 :(得分:0)

during the scrolling of ListView 经常调用findViewById() (which layout's children is inflated for a row of listview),这会降低性能。即使适配器返回recycling的虚增视图,您仍需要look up the elements and update them。绕过重复使用findViewById()的方法是使用view holder design pattern

ViewHolder对象存储component views inside the tag field of the Layout中的每一个,因此您可以immediately access them without the need to look them up repeatedly。首先,您需要创建一个类来保存您的确切视图集。

以下是代码中的类

static class WeatherHolder {
        ImageView imgIcon;
        TextView txtTitle;
}
  

是的,它是由我们手动创建的   在getView()中,您将创建该类的Object并访问它

 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        WeatherHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new WeatherHolder();
            holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
            holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);

            row.setTag(holder);
        }
        else
        {
            holder = (WeatherHolder)row.getTag();
        }
//do ur staff
return row;
}

了解更多信息Visit here

http://developer.android.com/training/improving-layouts/smooth-scrolling.html