在ListView行布局中获取一个subView

时间:2015-03-12 13:43:32

标签: android android-listview

我需要更改ListView行内ImageView中使用的可绘制资源。 我正在使用这个:

int wantedPosition = 10; // Whatever position you're looking for
int firstPosition = listView.getFirstVisiblePosition() - listView.getHeaderViewsCount(); // This is the same as child #0
int wantedChild = wantedPosition - firstPosition;
// Say, first visible position is 8, you want position 10, wantedChild will now be 2
// So that means your view is child #2 in the ViewGroup:
if (wantedChild < 0 || wantedChild >= listView.getChildCount()) {
Log.w(TAG, "Unable to get view for desired position, because it's not being displayed on screen.");
return;
}
View wantedView = listView.getChildAt(wantedChild);

如果我在findViewById内搜索wantedView,我找不到我正在寻找的ImageView。 如何引用此ImageView来设置另一个可绘制资源?

1 个答案:

答案 0 :(得分:0)

快捷方式:使用setTag(position)(或getItem或任何您想要设置的标记作为适配器View方法或相对内容的getView和进一步使用listView.findViewByTag