我在以下链接中看到了自定义列表视图的程序
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()方法中,他为WeatherHolder创建了这个WeatherHolder类的对象是什么?
>它是如何创建的?
>它是由我们手动创建的吗?
Because I cant find the body of the “WeatherHolder” class any where
else in the program.
我希望我的问题很明确.程序中的WeatherHolder是什么,谁创建了WatchHolder类.
解决方法:
在ListVIEw的滚动期间,findVIEwByID()(布局的子项为列表视图的行膨胀)经常被调用,这会降低性能.即使适配器返回一个膨胀的视图以进行回收,您仍然需要查找元素并更新它们.重复使用findVIEwByID()的方法是使用视图持有者设计模式.
VIEwHolder对象将每个组件视图存储在Layout的标记字段中,因此您可以立即访问它们而无需重复查找它们.首先,您需要创建一个类来保存您的确切视图集.
这是代码中的类
static class WeatherHolder { ImageVIEw imgIcon; TextVIEw txtTitle;}
Yes it is manually created by us
ingetVIEw()
u will createObject
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 staffreturn row;}
欲了解更多信息Visit here
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
总结以上是内存溢出为你收集整理的android – 自定义listView中的持有者是如何创建的?全部内容,希望文章能够帮你解决android – 自定义listView中的持有者是如何创建的?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)