在Android ListView中,如何迭代/操纵所有子视图,而不仅仅是可见的视图? [英] In an android ListView, how can I iterate/manipulate all the child views, not just the visible ones?

查看:137
本文介绍了在Android ListView中,如何迭代/操纵所有子视图,而不仅仅是可见的视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码不会更改所有ListView's行的文本,因为getChildCount()不会获取所有ListView's行,而只会显示可见的行.

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);
}

那我该怎么办?

是否存在用于在ListView's行变为可见时获取通知的代码,因此我可以设置其文本大小?

Is there code for getting a notification when a ListView's row becomes visible, so I can set its text size then?

推荐答案

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;

. . .

由Corey Trager

EDIT BY Corey Trager:

以上内容无疑为我指明了正确的方向.我发现处理OnScrollListener.onScroll比onScrollStateChanged更好.即使我删除了onScrollSgtaetChanged中的case语句并处理了每个状态更改,某些文本也没有得到调整大小.但是使用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.

因此,我看似正常的代码如下:

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中,如何迭代/操纵所有子视图,而不仅仅是可见的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆