自定义 listView 中的持有者是如何创建的? [英] How are holders in custom listView created?

查看:14
本文介绍了自定义 listView 中的持有者是如何创建的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是自定义适配器:

public class WeatherAdapter extends ArrayAdapter{上下文上下文;int layoutResourceId;天气数据[] = null;公共天气适配器(上下文上下文,int layoutResourceId,天气 [] 数据){超级(上下文,layoutResourceId,数据);this.layoutResourceId = layoutResourceId;this.context = 上下文;this.data = 数据;}@覆盖public View getView(int position, View convertView, ViewGroup parent) {查看行 = convertView;WeatherHolder 持有人 = null;如果(行==空){LayoutInflater inflater = ((Activity)context).getLayoutInflater();row = inflater.inflate(layoutResourceId, parent, false);持有人=新天气持有人();holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);row.setTag(holder);}别的{持有人 = (WeatherHolder)row.getTag();}天气 weather = data[位置];holder.txtTitle.setText(weather.title);holder.imgIcon.setImageResource(weather.icon);返回行;}静态类 WeatherHolder{ImageView imgIcon;TextView txtTitle;}}

getView()方法中,他为WeatherHolder创建对象这个WeatherHolder类是什么?

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

<块引用>

因为我无法在任何地方找到WeatherHolder"类的主体其他在程序中.

我希望我的问题很清楚.程序中的 WeatherHolder 是什么,谁创建了那个 WatchHolder 类.

解决方案

在 ListView 滚动期间 findViewById() (哪个布局的子级被膨胀了行列表视图) 被频繁调用,这会降低性能.即使当 Adapter 为 recycling 返回一个膨胀的视图时,你仍然需要查找元素并更新它们.避免重复使用 findViewById() 的一种方法是使用 view holder design pattern.

一个ViewHolder 对象将每个组件视图存储在Layout 的标签字段中,因此您可以立即访问它们,而无需反复查找它们.首先,您需要创建一个类来保存您的确切视图集.

这是您代码中的类

静态类 WeatherHolder {ImageView imgIcon;TextView txtTitle;}

<块引用>

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

 @Overridepublic View getView(int position, View convertView, ViewGroup parent) {查看行 = convertView;WeatherHolder 持有人 = null;如果(行==空){LayoutInflater inflater = ((Activity)context).getLayoutInflater();row = inflater.inflate(layoutResourceId, parent, false);持有人=新天气持有人();holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);row.setTag(holder);}别的{持有人 = (WeatherHolder)row.getTag();}//做你的员工返回行;}

欲了解更多信息访问此处

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

I saw a program for the custom listview in the following link http://www.ezzylearning.com/tutorial.aspx?tid=1763429&q=customizing-android-listview-items-with-custom-arrayadapter

THIS IS THE CUSTOM ADAPTER:

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

in getView() method he creates object for WeatherHolder what is this WeatherHolder class?

  1. How is it created?
  2. Is it manually created by us?

Because I cant find the body of the "WeatherHolder" class any where else in the program.

I hope my question is clear. What is WeatherHolder in the program, who created that WatchHolder class.

解决方案

during the scrolling of ListView findViewById() (which layout's children is inflated for a row of listview) is called frequently, which can slow down performance. Even when the Adapter returns an inflated view for recycling, you still need to look up the elements and update them. A way around repeated use of findViewById() is to use the view holder design pattern.

A ViewHolder object stores each of the component views inside the tag field of the Layout, so you can immediately access them without the need to look them up repeatedly. First, you need to create a class to hold your exact set of views.

Here is the class in your code

static class WeatherHolder {
        ImageView imgIcon;
        TextView txtTitle;
}

Yes it is manually created by us in getView() u will create Object of that class and access it

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

For more info Visit here

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

这篇关于自定义 listView 中的持有者是如何创建的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆