How to get the first visible View from an Android ListView

前端 未结 6 1231
情歌与酒
情歌与酒 2021-01-04 15:22

Is there a way to get the first visible View out of the ListView in Android?

I can get the data that backs the first View in the Adapter but it seems I can\'t get t

相关标签:
6条回答
  • 2021-01-04 16:13

    You can use the following code:

    for (int i = 0; i <= conversationListView.getLastVisiblePosition() - conversationListView.getFirstVisiblePosition(); i++) {
            View listItem = conversationListView.getChildAt(i);
    }
    
    0 讨论(0)
  • 2021-01-04 16:20

    Indeed listView.getChildAt(listView.getFirstVisiblePosition()) gives the first visible item,
    BUT it could be half visible list item.

    To get first completely visible list item,

    if (listView.getChildAt(0).getTop() < 0) {
         int firstCompletelyVisiblePos = listView.getFirstVisiblePosition() + 1;
    }
    
    0 讨论(0)
  • 2021-01-04 16:21

    ListView has a function getFirstVisiblePosition so to get the first visible view, the code would be:

    listView.getChildAt(listView.getFirstVisiblePosition());

    0 讨论(0)
  • 2021-01-04 16:21
    Object item = listView.getItemAtPosition(listView.getFirstVisiblePosition());
    

    For first completely visible list item:

    int pos = listView.getFirstVisiblePosition();
    if (listView.getChildCount() > 1 && listView.getChildAt(0).getTop() < 0) pos++;
    Object item = listView.getItemAtPosition(pos);
    
    0 讨论(0)
  • Actually ListView items are just children of ListView. So first visible ListView item is:

    listView.getChildAt(0)
    
    0 讨论(0)
  • 2021-01-04 16:28

    listView.scrollBy(0, -40);

    This works very well

    0 讨论(0)
提交回复
热议问题