【发布时间】:2013-11-05 21:52:28
【问题描述】:
我正在使用一些列表项布局,在项目布局中,有一个 Viewstub 我想在其中放置一些图像。我没有列表项布局的来源,只知道其中有一些 TextViews 和 ViewStubs它。
我的目的是先找到 ViewStub 并设置我的个人布局并使用它。但是,无法找到某些 ViewStub。
public class TJAdapter extends CursorAdapter {
....
public void bindView(View view, Context context, Cursor cursor) {
View item = view;
ViewStub contentstub = (ViewStub)item.findViewById(R.id.content_stub);
if (contentstub == null){
LOG.error("TJ,contentstub is null");
} else {
LOG.error("TJ,contentstub is not null");
contentstub.setLayoutResource(R.layout.icon_image);
View iconImage = contentstub.inflate();
}
....
}
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View view = mInflater.inflate(R.layout.list_item, parent, false);
bindView(view, context, cursor);
}
}
而日志输出是这样的:
TJ,bindView is called
TJ,contentstub is not null
TJ,bindView is called
TJ,contentstub is null
TJ,bindView is called
TJ,contentstub is not null
TJ,bindView is called
TJ,contentstub is not null
TJ,bindView is called
TJ,contentstub is null
TJ,bindView is called
TJ,contentstub is not null
我花了很多时间,但不知道为什么会发生这种情况。
身体可以帮忙吗?
================================================ ===================
正如 blahdiblah 建议的那样,现在我可以找到 iconImage。但是我不太明白为什么下一项会受到影响,我不能再按列表项了。它没有响应。
ImageButton iconImage = null;
ViewStub contentstub = (ViewStub)item.findViewById(R.id.content_stub);
if (contentstub == null){
LOG.error("TJ,contentstub is null");
iconImage = (ImageButton)item.findViewById(R.id.icon_image);
} else {
LOG.error("TJ,contentstub is not null");
contentstub.setLayoutResource(R.layout.list_item_icon_image);
View image = contentstub.inflate();
iconImage = (ImageButton)image.findViewById(R.id.icon_image);
}
if (iconImage == null) {
LOG.error("TJ,iconImage is null");
} else {
LOG.error("TJ, iconImage is not null");
}
【问题讨论】:
-
是的,View item = view;我简化了代码
-
ViewStub上是否设置了android:inflatedId? -
我没有列表项的 xml 文件。但据我所知,没有。
-
对,你提到了。只是出于好奇,您怎么可以夸大您没有来源的资源?
-
其他人在这部分工作,他们建立了一个我可以使用的库。没有其他人跟我有同样的问题。这对我来说太棘手了..
标签: android listview layout-inflater viewstub