【问题标题】:ListView adapter, prevent updatesListView 适配器,防止更新
【发布时间】:2012-10-08 11:14:12
【问题描述】:

我已经扩展了 SimpleAdapter 的日期分组项目,这在创建时可以正常工作。列表项目包含分组视图,如果项目属于当前日期组,我的适配器将隐藏此视图:

TextView Title (2012-10-08) - visible
TextView Name  (Foo)        - visible

TextView Title (2012-10-08) - hidden
TextView Name  (Bar)        - visible

TextView Title (2012-10-07) - visible
TextView Name  (Baz)        - visible

TextView Title (2012-10-07) - hidden
TextView Name  (Etc)        - visible

现在,如果我在完全填充的列表中向下滚动到第二项下方的位置,隐藏视图将在再次向上滚动时变得可见。为什么会发生这种情况,我该如何预防?

我在onCreate 之后没有更改适配器,所以我不知道为什么我的ListViewAdapter 会更改视图状态。

日期适配器:

public class DateAdapter extends SimpleAdapter
{
    protected   ArrayList<HashMap<String, String>>  items;
    private     String                  prevDate = "";
    private     SimpleDateFormat            dateFormat = new SimpleDateFormat("EEEE, dd/MM yyyy", Locale.getDefault());

    public DateAdapter (Context context, ArrayList<HashMap<String, String>> items, int resource, String[] from, int[] to)
    {
        super(context, items, resource, from, to);
        this.items = items;
    }

    @Override
    public View getView (int position, View convertView, ViewGroup parent)
    {
        View view = super.getView(position, convertView, parent);

        try
        {
            HashMap<String, String> item = this.items.get(position);
            Long itemTime = Long.parseLong(item.get("timedate")) * 1000;

            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(itemTime);
            String dateString = dateFormat.format(calendar.getTime());

            TextView section = (TextView) view.findViewById(R.id.sectionTitle);

            if (!dateString.equals(this.prevDate))
            {
                this.prevDate = dateString;
                section.setText(dateFormat.format(calendar.getTime()));
                section.setVisibility(View.VISIBLE);
            }
        }
        catch (Exception e) { Debug.e(e); }

        return view;
    }
}

【问题讨论】:

  • if (!dateString.equals(this.prevDate)) else 声明添加到此if 并在此处隐藏部分...。我仍然认为您应该使用SectionIndexer developer.android.com/reference/android/widget/…
  • @Selvin 我会看看SectionIndexer。似乎更符合逻辑。我还发现我的DateAdapter 不是绝对的,会看到最后获取的项目的prevDate。因此,像我一样扩展 SimpleAdapter 不是在不检查相邻项目的情况下就可以工作的。

标签: android listview simpleadapter


【解决方案1】:

View view = super.getView(position, convertView, parent);

此行返回新创建的视图,仅当提供的convertView为null时,如果convertView不为null,则返回相同的视图。因此,当它返回相同的 convertView 实例时,具有可见性 VISIBLE 的视图可能会保持可见。所以,你要做的就是填充这个条件的 else

    if (!dateString.equals(this.prevDate)){} 

您将在此处将视图可见性设置为 INVISIBLE。告诉我有用吗?

【讨论】:

  • Selvin 建议使用 SectionIndexer 对象,我认为这将比我当前的 DateAdapter 更有效。 prevDate 也是相对于适配器最后获取的项目,因此如果不检查相邻项目将无法工作。
  • 我将您的答案标记为解决方案。为此,关键是仅对照前一项检查日期,而不是定义前一个日期属性 (prevDate)。 if (position &gt; 0) { ...getItem(position - 1) }.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多