0

我试图在listView基于日期的部分标题中加载自定义呼叫日志。ListAdapter我将每个日期与前一个日期进行比较并设置SectionHeaderLayout可见/不可见。加载后,部分标题显示正确,但是当ListView我滚动部分标题时,部分标题设置为错误ListItems

请帮我找出解决方案。

这就是我试图SectionHeader通过adapter.

    if (position == 0) {

        checkDate = mDateStr;
        holder.sectionHeaderDate.setVisibility(View.VISIBLE);
        holder.sectionHeaderText.setText(mDateStr);
         }
        } else if (checkDate == null || !checkDate.equals(mDateStr)) {

            checkDate = mDateStr;
            holder.sectionHeaderDate.setVisibility(View.VISIBLE);
            holder.sectionHeaderText.setText(mDateStr);

        } else {
            holder.sectionHeaderDate.setVisibility(View.GONE);
        }

提前致谢

4

1 回答 1

0

我看到这是一个老问题,你可能已经解决了你的问题,但我会为其他有同样问题的人回答。

如果您想根据上一个日期显示标题,则无法通过记住传递给 getView 函数的最后一项来做到这一点。原因是滚动,即上下方向不同。例如,如果您有项目 1、2、3、4、5

当你下楼时,当前项目是 3,前一个项目是 2,一切都会正常工作。但是如果你要上去,你之前的 3 项目实际上是 4,这就是你的问题发生的地方。

因此,您应该使用职位,而不是保留项目。

这将是您可以在 getView 函数内部调用的解决方案草图:

private void showHeader(ViewHolder holder, Call item, int position) {

    boolean shouldShowHeader = false;
    if (position == 0
            || !DateHelper.isSameDay(item.getDateTime(),
                    items.get(position - 1).getDateTime()))
        shouldShowHeader = true;

    if (shouldShowHeader) {
        holder.header.setVisibility(View.VISIBLE);
        holder.date.setText(DateHelper.getSimpleDate(item.getDateTime()));
    } else {
        holder.header.setVisibility(View.GONE);
    }

}
于 2014-11-26T03:49:58.173 回答