Get ListView children that are not in view

后端 未结 2 669
孤城傲影
孤城傲影 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);
        }
    }
    
    0 讨论(0)
  • 2020-12-05 19:49

    You can use this code

    private static void initRecyclerBin(AbsListView view) {
            try {
                Field localField = AbsListView.class.getDeclaredField("mRecycler");
                localField.setAccessible(true);
                Object recyclerBin = localField.get(view);
                Class<?> clazz = Class.forName("android.widget.AbsListView$RecycleBin");
                Field scrapField = clazz.getDeclaredField("mScrapViews");
                scrapField.setAccessible(true);
                ArrayList<View>[] scrapViews;
                scrapViews = (ArrayList<View>[]) scrapField.get(recyclerBin);
                if (null != scrapViews) {
                    int length = scrapViews.length;
                    for (int i = 0, count = 0; i < length; i++) {
                        if (null != scrapViews[i] && !scrapViews[i].isEmpty()) {
                            for (int j = 0; j < scrapViews[i].size(); j++) {
                                scrapViews[i].get(j);//this is the scrapViews
                            }
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    

    and ListView getChildViews().

    0 讨论(0)
提交回复
热议问题