开发者

Scrolling past header view in a ListView?

开发者 https://www.devze.com 2023-04-02 05:50 出处:网络
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 t

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

            }
        });
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号