学习笔记-ListView中控件的刷新-setTag的两种用法

来源:互联网 发布:python cookie 登录 编辑:程序博客网 时间:2024/06/05 03:42

在ListView中,有时候需要动态更新其中某些控件的内容。更新的方法有以下几个:

方法一:更新所有item的信息

在更新后,直接调用adapter.notifyDataSetChanged(); 

但是这是最耗资源的做法。通常我们只想更新一个item中的其中某个view,这样,效率很低。


方法二:只更新需要更新的view。关键是拿到相应view的引用。

建立一个list保存需要更新的view

List<View> needToUpdate;needToUpdate = new ArrayList<View>();

然后再adapter的getView方法中,将需要更新的view添加进去:

holder.summary = (TextView) convertView.findViewById(R.id.ii_hall_lottery_summary);needToUpdate.add(holder.summary);    //这里holder.summary是一个TextView

然后就可以在外面找到该控件:

TextView view = (TextView)categoryList.findViewWithTag(0);if(view != null) {<span style="white-space:pre"></span>view.setText(text);}

方法三:如果不想多维护一个ListView,可以使用convertView.setTag(Object)方法:

方法说明:

Sets the tag associated with this view. A tag can be used to mark a view in its hierarchy and does not have to be unique within the hierarchy. Tags can also be used to store data within a view without resorting to another data structure.

我们原先使用的都是第二个功能,即convertView.setTag(holder)来记录holder

这里使用它的另一项功能:

holder.summary.setTag(position); //记录下这个view

然后可以在外面找到该控件:

TextView view = (TextView)categoryList.findViewWithTag(0);if(view != null) {view.setText(text);}
这种方法比较方便。


0 0
原创粉丝点击