【问题标题】:Strange behaviour of ImageViews of the last item in ListViewListView 中最后一项的 ImageViews 的奇怪行为
【发布时间】:2013-01-19 20:31:02
【问题描述】:

我有一个带有自定义适配器的 ListView。在每一行中都有一个 ImageView,它只在某些约束下可见。问题是如果第一行有这个 ImageView 可见,那么最后一行也是如此,反之亦然。

这是我的适配器的getView() 代码。

public View getView(int position, View view, ViewGroup parent) {
    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.row_idea, null);
    }

    Idea idea = mIdeas.get(position);

    if (idea != null) {
        ImageView imgAlarm = (ImageView) view
                .findViewById(R.id.imgAlarm_rowIdea);
        if (idea.getTimeReminder() != null)
            imgAlarm.setVisibility(ImageView.VISIBLE);

        TextView lblTitle = (TextView) view
                .findViewById(R.id.lblTitle_rowIdea);
        lblTitle.setText(idea.getTitle());

        TextView lblDescription = (TextView) view
                .findViewById(R.id.lblDescription_rowIdea);
        lblDescription.setText(idea.getDescription());
    }
    return view;
}

mIdeas 是一个ArrayList,所有数据都显示在ListView 中。 imgAlarm 就是我上面说的ImageView

【问题讨论】:

    标签: android android-listview android-imageview


    【解决方案1】:

    改变

    if (idea.getTimeReminder() != null)
                imgAlarm.setVisibility(ImageView.VISIBLE);
    

    if (idea.getTimeReminder() != null)
                imgAlarm.setVisibility(ImageView.VISIBLE);
    else
                imgAlarm.setVisibility(ImageView.GONE);
    

    这里发生的是适配器正在“回收”视图。因此,在您在测试中看到的情况下,最后一个视图和第一个视图实际上是同一个实例。

    【讨论】:

      【解决方案2】:

      如果条件不满足,您需要恢复 ImageView 的可见性状态,这样您就不会遇到潜在的回收视图问题(这可能有 ImageView 已经可见并在它应该出现的时候出现't):

      if (idea.getTimeReminder() != null) {
           imgAlarm.setVisibility(ImageView.VISIBLE);
      } else {
          imgAlarm.setVisibility(ImageView.INVISIBLE); // or GONE
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-10-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-08
        • 2023-02-06
        • 2014-03-21
        相关资源
        最近更新 更多