在Android的ListView,我怎么可以遍历/ manipulte所有孩子的意见,而不仅仅是可见的呢? [英] In an android ListView, how can I iterate/manipulte all the child views, not just the visible ones?
问题描述
下面的code不改变所有的 ListView的
行的文本,因为 getChildCount()
没有得到全部的 ListView的
行,但只是可见的行。
The code below does NOT change the text of all of a ListView's
rows because getChildCount()
does not get all of a ListView's
rows, but just the rows that are visible.
for (int i = 0; i < listView.getChildCount(); i++)
{
View v = listView.getChildAt(i);
TextView tx = (TextView) v.findViewById(R.id.mytext);
tx.setTextSize(newTextSize);
}
那么,我该怎么办?
So, what SHOULD I do?
时有$ C $下得到一个通知,当 ListView的
行变为可见,这样我就可以设置其文本大小呢?
Is there code for getting a notification when a ListView's
row becomes visible, so I can set its text size then?
推荐答案
<一个href="http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List13.html">List13从API演示做类似的东西使用OnScrollStateChanged。有可能是一个更好的办法,虽然:
List13 from the API Demos does something similar using OnScrollStateChanged. There may be a better way, though:
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState) {
case OnScrollListener.SCROLL_STATE_IDLE:
mBusy = false;
int first = view.getFirstVisiblePosition();
int count = view.getChildCount();
for (int i=0; i<count; i++) {
TextView t = (TextView)view.getChildAt(i);
if (t.getTag() != null) {
t.setText(mStrings[first + i]);
t.setTag(null);
}
}
mStatus.setText("Idle");
break;
。 。 。
. . .
编辑由科瑞Trager:
EDIT BY Corey Trager:
上面明确指出我朝着正确的方向发展。我发现处理OnScrollListener.onScroll比onScrollStateChanged更好地工作。即使我删除在onScrollSgtaetChanged的情况下发言,并处理每一个状态变化,一些文本没有得到调整。但随着onScroll,事情似乎工作。
The above definitely pointed me in the right direction. I found handling OnScrollListener.onScroll worked better than onScrollStateChanged. Even if I removed the case statement in onScrollSgtaetChanged and handled every state change, some text wasn't getting resized. But with onScroll, things seem to work.
所以,我貌似工作code是这样的:
So, my seemingly working code looks like this:
public void onScroll(AbsListView v, int firstVisibleItem, int visibleCount, int totalItemCount)
{
ListView lv = this.getListView();
int childCount = lv.getChildCount();
for (int i = 0; i < childCount; i++)
{
View v = lv.getChildAt(i);
TextView tx = (TextView) v.findViewById(R.id.mytext);
tx.setTextSize(textSize);
}
}
这篇关于在Android的ListView,我怎么可以遍历/ manipulte所有孩子的意见,而不仅仅是可见的呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!