ListView——HolderView方式

来源:互联网 发布:java定时器 任务超时 编辑:程序博客网 时间:2024/04/30 11:02

HolderView方式

在HolderView方式中,目的同样是为了避免反复的调用findViewById,但是我们将这个findViewByIdde 任务交给了一个HolderView对象

@Overridepublic View getView(int i, View convertView, ViewGroup viewGroup) {  HolderView holderView;  // Important to not just null check, but rather to a instanceof  // since we might get any subclass of view here.  if (convertView instanceof HolderView) {    holderView = (HolderView) convertView;  } else {    holderView = new HolderView(mContext);  }  holderView.bind("标题", R.drawable.ic_launcher, "sajsa");  return holderView;}public class HolderView extends GridLayout {  private ImageView img;  private TextView title;  private TextView info;  public HolderView(Context context, AttributeSet attrs) {    super(context, attrs);    View v = LayoutInflater.from(context).inflate(R.layout.list_item, this);    title = (TextView) v.findViewById(R.id.title);    img = (ImageView)item.findViewById(R.id.img)    info = (TextView)item.findViewById(R.id.info);  }  public void bind(String stringtitle,int imgrsc, String stringinfo) {    title.setText(stringtitle);    img.setImageResource(imgrsc);    info.setText(stringinfo);  }}

HolderView自己维护一个子元素的集合,同时对外提供绑定数据的公共方法bind。HolderView方式的思想是:对于ListView来讲,每一个Item本身是同一个类只是数据不同。 但是需要注意的是使用HolderView方式在Adapter中getView返回的是HolderView对象。

总结:ViewHolder方式使用简单,HolderView方式更符合面向对象规范。

0 0