列表视图返回getChildAt不对劲儿 [英] listview getChildAt return wrong child
问题描述
我碰到一个ListView /的getChildAt方法一些奇怪的行为。
I'm getting some strange behavior from a listview/the getChildAt method.
我有它ListView和设置适配器有300行。我尝试设置onScrollListener我的列表和onScroll方法得到firstVisibleItem查看和更改它的背景色系列表视图返回错误观点的getChildAt方法(它到底在返回查看 firstVisibleItem双)。
I have listView and set adapter for it that have 300 rows. I try to set onScrollListener for my list and in onScroll method get firstVisibleItem view and change it's background color.but getChildAt method of list view return wrong view(exactly it return view at Double of firstVisibleItem).
这是我的code:
adapter = new ListAdapterShowContent(this, arrayList);
list.setAdapter(adapter);
list.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
View v = list.getChildAt(firstVisibleItem);
if(v != null) {
v.setBackgroundColor(Color.BLUE);
}
}
});
例如,如果在firstVisibleItem是onScroll 3,名单变化的背景第六行
for example if firstVisibleItem in onScroll was 3,the Sixth row of list change background
更新:我只是改变了BG简化问题,其实我想要得到的view.so使用适配器不帮我回到榜首位置
UPDATE: I just change bg for simplify issue, In fact I want to get top position of returned view.so using adapter don't help me.
推荐答案
您需要简单地调用 getChildAt()与指数0
(或1如果列表视图有一些固定报头)。这将永远是第一个可见项目,不管其背后的数据支持数组中的位置。
You need to simply call getChildAt() with index 0
(or 1 if you list view has some fixed header). This will always be the first visible item, regardless of the position of the data behind it in the backing array.
这篇关于列表视图返回getChildAt不对劲儿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!