Get all full visible objects on lListView

后端 未结 4 439
粉色の甜心
粉色の甜心 2020-12-31 03:21

I have a ListView, which contains more elements then I can display at one time.Now I want to get Index off all Elements, which are full visible ( -> excluding those that are

相关标签:
4条回答
  • 2020-12-31 03:34

    Try onScrollListner and you can able to use getFirstVisiblePosition and getLastVisiblePosition.

    This this link, it contain similar type of problem. I suppose you got your answer there..,.

    0 讨论(0)
  • 2020-12-31 03:41

    A ListView keeps its rows organized in a top-down list, which you can access with getChildAt(). So what you want is quite simple. Let's get the first and last Views, then check if they are completely visible or not:

    // getTop() and getBottom() are relative to the ListView, 
    //   so if getTop() is negative, it is not fully visible
    int first = 0;
    if(listView.getChildAt(first).getTop() < 0)
        first++;
    
    int last = listView.getChildCount() - 1;
    if(listView.getChildAt(last).getBottom() > listView.getHeight())
        last--;
    
    // Now loop through your rows
    for( ; first <= last; first++) {
        // Do something
        View row = listView.getChildAt(first);
    }
    

    Addition

    Now I want to get Index off all Elements, which are full visible

    I'm not certain what that sentence means. If the code above isn't the index you wanted you can use:

    int first = listView.getFirstVisiblePosition();
    if(listView.getChildAt(0).getTop() < 0)
        first++;
    

    To have an index that is relative to your adapter (i.e. adapter.getItem(first).)

    0 讨论(0)
  • 2020-12-31 03:49

    The way I would do this is I would extend whatever view your are passing in getView of the ListView adapter and override the methods onAttachedToWindow and onDetachedToWindow to keep track of the indexes that are visible.

    0 讨论(0)
  • 2020-12-31 03:56

    The above code is somewhat correct. If you need to find the completely visible location of view use below code

    public void onScrollStateChanged(AbsListView view, int scrollState)  {
        // TODO Auto-generated method stub
        View v = null;
    
        if (scrollState == 0) {
            int first =0;
            if (view.getChildAt(first).getTop() < 0)
                first++;
            int last = list.getChildCount() - 1;
            if (list.getChildAt(last).getBottom() > list
                    .getHeight())
                last--;
            // Now loop through your rows
            for ( ; first <= last; first++) {
                // Do something
    
                View row = view.getChildAt(first);
                // postion for your row............ 
                int i=list.getPositionForView(row);
            }
        }
            // set the margin.
    }
    
    0 讨论(0)
提交回复
热议问题