Skip to content Skip to sidebar Skip to footer

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 onl

Solution 1:

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 visibleintfirst=0;
if(listView.getChildAt(first).getTop() < 0)
    first++;

intlast= listView.getChildCount() - 1;
if(listView.getChildAt(last).getBottom() > listView.getHeight())
    last--;

// Now loop through your rowsfor( ; first <= last; first++) {
    // Do somethingViewrow= 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:

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

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

Solution 2:

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.

Solution 3:

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

Solution 4:

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

publicvoidonScrollStateChanged(AbsListView view, int scrollState)  {
    // TODO Auto-generated method stubViewv=null;

    if (scrollState == 0) {
        intfirst=0;
        if (view.getChildAt(first).getTop() < 0)
            first++;
        intlast= list.getChildCount() - 1;
        if (list.getChildAt(last).getBottom() > list
                .getHeight())
            last--;
        // Now loop through your rowsfor ( ; first <= last; first++) {
            // Do somethingViewrow= view.getChildAt(first);
            // postion for your row............ int i=list.getPositionForView(row);
        }
    }
        // set the margin.
}

Post a Comment for "Get All Full Visible Objects On Llistview"