有些项目在列表视图onScroll方法无效 [英] Some items are null in listview onScroll method

查看:144
本文介绍了有些项目在列表视图onScroll方法无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListView和我重写它的onScroll事件,以便我可以在列表视图中第一个可见项目的文本的第一个字符。我的code是如下:

I have a listview and I override the onScroll event for it so that I can get the first character of the text on the first visible item of the listview. My code is as follows:

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
    //
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
           int visibleItemCount, int totalItemCount) {
    ListView caller = (ListView) view;
    View v = caller.getChildAt(firstVisibleItem);
    if(v instanceof TextView){
        TextView tv = (TextView) v;
        if(tv != null){
            String sInitial = tv.getText().toString(); 
            sInitial = Character.toString(sInitial.charAt(0));

            TextView tvPager = (TextView) findViewById(R.id.tvPager);
            tvPager.setText(sInitial);
        }
    }
}

在该FirstVisibleItem变量是从0到12(以precise),我的视图V不为空,我可以得到它的文本。但是,当它超过12,我的v是已空。我的项目是不是12路多,因此不能为空。

When the FirstVisibleItem variable is from 0 to 12(to be precise), my View v is not null and I can get the text of it. But when it goes beyond 12, my v is already null. My items are way more than 12 so it shouldn't be null.

是不是有什么毛病我code?还是有更好的方法做我想要什么?
在此先感谢!

Is there something wrong with my code? Or are there better way doing what I want? Thanks in advance!

推荐答案

在Android的ListView回收列表中的项目的时候都没有显示在屏幕上。因此,任何不可见为null。

An android listview recycles "items" in the list when they are not visible on the screen. So anything that is not visible will be null.

http://commonsware.com/Android/excerpt.pdf

这篇关于有些项目在列表视图onScroll方法无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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