Get ListView children that are not in view

后端 未结 2 670
孤城傲影
孤城傲影 2020-12-05 18:54

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


        
2条回答
  •  既然无缘
    2020-12-05 19:49

    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);
        }
    }
    

提交回复
热议问题