简体   繁体   中英

Does ListView expect the selected row from setSelection() to be on the top when a user scrolls?

I have a ListView which has overlayed on top of it another view which I call a Header. This header can be of various heights.

On ListView's dataset, I insert in a blank row, and programmatically set the height of this row to be the height of my Header view. This way when the ListView loads, the row at position 1 (the actual first row of data) is lined up to the bottom of the header. I do this so I can set the Header view as partially transparent and when the user scrolls they see the rows of data mix with the transparency of the Header view.

Now I have one use case where I can navigate to this list and a given row will be selected. What I've done is call setSelection with position X. This will automatically scroll my ListView so that the top of row X is at the top of the list. However, my Header view still obscures this. To compensate I call scrollTo. These two pieces are something as such:

listView.setSelection(selectedRowPosition);

if(hasHeadersEnabled()) { listView.scrollTo(-headerView.getMeasuredHeight). }

When I call these lines of code, my ListView looks as I would expect. The selected row's top is to the bottom of my HeaderView. The problem I run into is that as soon as a user touches the ListView, the scroll position jumps so that the selected row's top is at the true top of the ListView (aka the Header view's top).

My question is, after you setSelectedRow on a ListView, does the ListView always expect that row's top to be the ListView's top when it starts consuming the onTouch events for scrolling? Is there another way to accomplish what I'm hoping to do with this code? I've tried scrollTo and scrollBy and both have the same effect.

So I should have looked at the API harder. Found the answer to my question.

I need to use setSelectionFromTop instead of the combination of setSelection and scrollTo. Here is the sample code:

listView.setSelectionFromTop(rowPosition, headerView.getMeasuredHeight());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM