【问题标题】:In listview,Viewstub cannot be found after the previous one is inflate在listview中,上一个inflate后找不到Viewstub
【发布时间】: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


【解决方案1】:

当您调用 contentstub.inflate(); 时,ViewStub 的内容将被替换包括其 ID。当视图在另一个对bindView 的调用中被回收时,搜索R.id.content_stub 将失败,因为ViewStub 已被R.layout.icon_image 替换。

如果搜索 R.id.content_stub 没有找到任何结果,则解决方案是查找您在 layout/icon_image.xml 中设置的 ID。

此外,您不必在newView 中手动调用bindView。这应该自动处理。

【讨论】:

  • 是的!现在我可以找到 iconImage 并且它不为空!
  • 但我不太明白为什么?总是影响下一个吗?为什么只有下一个不是全部?即使我确实在我的个人布局中找到了 iconImage,它是否仍然按照我的意愿排列?我的意思是,如果在这个列表视图中,我有六个项目和代码,那么图像是否仍然与项目相对应?
  • @T.J. CursorAdapter 文档似乎掩盖了它,但 ListViews 从不再可见的项目中重用列表项目视图以提高效率。 the docs on convertView in Adapter.getView() 对此进行了说明。因此,如果您得到一个存根已经膨胀的视图,那么它就是您已经将数据添加到该滚动屏幕的视图。如果您不对其进行任何操作,它仍会显示旧数据。
  • 感谢您的回答。而最重要的部分是关于bindView。由于是我自己调用的,所以bindView被调用了两次!这就是 null/not null 切换的方式!
猜你喜欢
  • 2017-01-16
  • 2015-07-25
  • 1970-01-01
  • 1970-01-01
  • 2016-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多