Scrolling past header view in a ListView?
I have a typical listview
, it has one headerview. I want to set the y scroll value so the headerview is initially hidden to the user. My attempts have brought odd results. I know the exact height of the headerview (40dip), so I figured I should be able to just immediately set the scroll-y pos to that height and be good to go:
ListView lv = getListView();
lv.addHeaderView(mHeaderView, null, false);
lv.setAdapter(myAdapter);
// 1) Works, but the first time I touch the screen,
// the listview pops its scroll position to zero
// immediately, pretty odd.
lv.scrollTo(40 * density);
// 2) Does not work in onCreate(), does not work
// if wrapped in a post.
lv.setSelectionAfterHeaderView();
// 3) Does not work in onCreate(), works if
// wrapped in a post.
lv.listView.setSelectionFromTop(1, 0);
My clicks on th开发者_StackOverflow社区e listview
seem to be off by one now, looks like the headerview is now being interpreted as the 0th item in the adapter - is that possible? That's why I have the setSelectionFromTop()
call using 1 instead of 0. Maybe this is why setSelectionAfterHeaderView()
is failing.
Thanks
You can wrap it on a post or execute it after the adapter has populated the ListView
Handler handler = new Handler()
handler.post(new Runnable() {
@Override
public void run() {
lv.setSelectionAfterHeaderView();
}
});
精彩评论