Search code examples
androidlistviewchildren

Get ListView children that are not in view


For an App I am working on, I'm trying to get the children of a ListView. To do this I have the following piece of code:

View listItem = mListView.getChildAt(i);

However, this only works for children that are in view. I need to also reach the children that are not in view. How would I do this?

EDIT:

Comparing the suggested methods to the one I was already using I find the following:

RelativeLayout listItem1 = (RelativeLayout) mListView.getAdapter().getView(i, null, mListView);
RelativeLayout listItem2 = (RelativeLayout) mListView.getChildAt(i);
Log.d("Checks", "listItem1: " + listItem1);
Log.d("Checks", "listItem2: " + listItem2);

08-27 14:16:56.628: D/Checks(15025): listItem1: android.widget.RelativeLayout@2c5f2920
08-27 14:16:56.628: D/Checks(15025): listItem2: android.widget.RelativeLayout@2c06d938
08-27 14:16:56.628: D/Checks(15025): listItem1: android.widget.RelativeLayout@2c72dfb0
08-27 14:16:56.628: D/Checks(15025): listItem2: android.widget.RelativeLayout@2bfabe50
08-27 14:16:56.638: D/Checks(15025): listItem1: android.widget.RelativeLayout@2c730c18
08-27 14:16:56.638: D/Checks(15025): listItem2: android.widget.RelativeLayout@2c0d3e38
08-27 14:16:56.638: D/Checks(15025): listItem1: android.widget.RelativeLayout@2c12ebc0
08-27 14:16:56.638: D/Checks(15025): listItem2: android.widget.RelativeLayout@2bddbf70
08-27 14:16:56.648: D/Checks(15025): listItem1: android.widget.RelativeLayout@2c131828
08-27 14:16:56.648: D/Checks(15025): listItem2: android.widget.RelativeLayout@2bdf3270
08-27 14:16:56.648: D/Checks(15025): listItem1: android.widget.RelativeLayout@2c140de0
08-27 14:16:56.648: D/Checks(15025): listItem2: android.widget.RelativeLayout@2c0c8d30

listItem2 points to the actual View in the ListView, which is what I want except that it only works for Views that are in sight. You can see from the log file that listItem1 and listItem2 don't correspond.

EDIT 2:

I came up with the following:

for (int i = 0; i < mListView.getCount(); i++) {
    RelativeLayout listItem = (RelativeLayout) mListView.getAdapter().getView(i, mListView.getChildAt(i), mListView);
}

This returns the Views correctly for all visible items in the list. However, it returns a different View for the ones that aren't in sight.

EDIT 3:

Thanks to nfirex I have a working answer. This will return Views even if they're not directly in sight:

public View getViewByPosition(int position, ListView listView) {
    final int firstListItemPosition = listView.getFirstVisiblePosition();
    final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;

    if (position < firstListItemPosition || position > lastListItemPosition ) {
        return listView.getAdapter().getView(position, listView.getChildAt(position), listView);
    } else {
        final int childIndex = position - firstListItemPosition;
        return listView.getChildAt(childIndex);
    }
}

Solution

  • You need method Adapter.getView():

    final View view = mListView.getAdapter().getView(position, null, mListView);
    

    UPDATE:

    You need to create your method. Something like this:

    public View getViewByPosition(int position, ListView listView) {
        final int firstListItemPosition = listView.getFirstVisiblePosition();
        final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;
    
        if (position < firstListItemPosition || position > lastListItemPosition ) {
            return listView.getAdapter().getView(position, null, listView);
        } else {
            final int childIndex = position - firstListItemPosition;
            return listView.getChildAt(childIndex);
        }
    }