Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all full visible objects on lListView

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?

like image 552
Kooki Avatar asked Nov 01 '12 15:11

Kooki


3 Answers

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

like image 167
Sam Avatar answered Nov 20 '22 07:11

Sam


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.

like image 40
Emil Davtyan Avatar answered Nov 20 '22 09:11

Emil Davtyan


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

like image 1
MKB Avatar answered Nov 20 '22 07:11

MKB