18

对于我正在开发的应用程序,我正在尝试获取 ListView 的子项。为此,我有以下代码:

View listItem = mListView.getChildAt(i);

但是,这仅适用于视野中的孩子。我还需要接触到不在视线范围内的孩子。我该怎么做?

编辑:

将建议的方法与我已经使用的方法进行比较,我发现以下内容:

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 指向 ListView 中的实际视图,这是我想要的,只是它仅适用于可见的视图。从日志文件中可以看出listItem1 和listItem2 不对应。

编辑2:

我想出了以下内容:

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

这将正确返回列表中所有可见项目的视图。但是,它会为看不见的视图返回不同的视图。

编辑 3:

感谢 nfirex,我有一个可行的答案。这将返回视图,即使它们不在视线范围内:

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

2 回答 2

35

您需要方法Adapter.getView()

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

更新:

你需要创建你的方法。像这样的东西:

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);
    }
}
于 2013-08-27T10:49:21.723 回答
2

您可以使用此代码

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

和 ListView getChildViews()。

于 2015-10-15T03:08:17.180 回答