如何在自定义的ListView持有人创造? [英] How are holders in custom listView created?

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

问题描述

我在下面的链接看到一个程序自定义列表视图 <一href="http://www.ezzylearning.com/tutorial.aspx?tid=1763429&q=customizing-android-listview-items-with-custom-arrayadapter" rel="nofollow">http://www.ezzylearning.com/tutorial.aspx?tid=1763429&q=customizing-android-listview-items-with-custom-arrayadapter

这是自定义适配器:

 公共类WeatherAdapter扩展ArrayAdapter&LT;天气及GT; {

    上下文语境;
    INT layoutResourceId;
    气象数据[] = NULL;

    公共WeatherAdapter(上下文的背景下,INT layoutResourceId,天气[]数据){
        超级(上下文,layoutResourceId,数据);
        this.layoutResourceId = layoutResourceId;
        this.context =背景;
        this.data =数据;
    }

    @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        查看排= convertView;
        WeatherHolder支架=无效;

        如果(行== NULL)
        {
            LayoutInflater充气=((活动)上下文).getLayoutInflater();
            行= inflater.inflate(layoutResourceId,父母,假);

            持有人=新WeatherHolder();
            holder.imgIcon =(ImageView的)row.findViewById(R.id.imgIcon);
            holder.txtTitle =(TextView中)row.findViewById(R.id.txtTitle);

            row.setTag(保持器);
        }
        其他
        {
            支架=(WeatherHolder)row.getTag();
        }

        天气天气预报=数据[位置]
        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() (其中布局的儿童充气一排列表视图)被频繁调用,这会降低性能。即使当适配器返回回收膨胀的观点,你仍然需要查找内容并更新它们。各地重复使用findViewById()的一种方法是使用视图座的设计模式

一个ViewHolder对象,每个布局的标签领域内的部分观点店,这样你就可以立即访问他们无需看它们一再。首先,你需要创建一个类来保存您的具体意见集。

下面是类在code

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

  

是的,它是手动由我们创造   在 getView() U将创建对象这个类和访问它

  @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        查看排= convertView;
        WeatherHolder支架=无效;

        如果(行== NULL)
        {
            LayoutInflater充气=((活动)上下文).getLayoutInflater();
            行= inflater.inflate(layoutResourceId,父母,假);

            持有人=新WeatherHolder();
            holder.imgIcon =(ImageView的)row.findViewById(R.id.imgIcon);
            holder.txtTitle =(TextView中)row.findViewById(R.id.txtTitle);

            row.setTag(保持器);
        }
        其他
        {
            支架=(WeatherHolder)row.getTag();
        }
//做UR工作人员
返回行;
}
 

有关详细信息访问这里

http://developer.android.com/training/improving-布局/平滑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天全站免登陆