18

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 only partially visible). At this moment I use getFirstVisiblePosition() & getLastVisiblePosition() into an for-loop to iterate them, but these method is not accurate as I want to.

Is there any better solution?

ProjectDelta
  • 351
  • 4
  • 12
Kooki
  • 1,157
  • 9
  • 30

4 Answers4

30

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).)

Sam
  • 86,580
  • 20
  • 181
  • 179
1

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.

Emil Davtyan
  • 13,808
  • 5
  • 44
  • 66
1

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..,.

Community
  • 1
  • 1
MKB
  • 7,587
  • 9
  • 45
  • 71
  • Thanks for the link, but this problem is exactly what I had, it gives me not the index of ListEntry what is fully visible, it returns all Items on Screen... – Kooki Nov 01 '12 at 19:37
-1

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.
}
Makyen
  • 31,849
  • 12
  • 86
  • 121
logan
  • 72
  • 4