获取不在视图中的 ListView 子项 [英] Get ListView children that are not in view
问题描述
对于我正在开发的应用程序,我正在尝试获取 ListView 的子项.为此,我有以下代码:
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?
将建议的方法与我已经使用的方法进行比较,我发现以下内容:
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 指向 ListView 中的实际 View,这正是我想要的,只是它仅适用于可见的 View.从日志文件中可以看出listItem1和listItem2并不对应.
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.
编辑 2:
我想出了以下几点:
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.
编辑 3:
感谢 nfirex,我有了一个有效的答案.即使它们不在视线范围内,这也会返回视图:
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);
}
}
推荐答案
你需要方法 Adapter.getView():
final View view = mListView.getAdapter().getView(position, null, mListView);
更新:
您需要创建您的方法.像这样:
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);
}
}
这篇关于获取不在视图中的 ListView 子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!