在 ListView 中滚动时的日期分隔符

Date separators while scrolling in ListView

在 ListView(使用自定义 CursorAdapter)中,我尝试仅根据两个 ListView 项之间的日期是否更改来显示分隔符,如下所示: .

不幸的是,我设置代码的方式是,当我向下滚动、暂停一秒钟,然后再次开始滚动时,分隔符出现在错误的位置(例如这里,看到消失ListView 中第二项中的分隔符):

或者,它可能会出现在第三项中。

我遇到了两个主要问题:

  1. 日期分隔符在不同时间出现和消失取决于滚动暂停,以及
    1. 日期分隔符的外观在错误的位置连续向上滚动。

我原则上理解这些问题的出现是因为 lastDate 在我的适配器中的行为实际上只考虑了单向向下滚动,此外是由 ListView 回收视图和容器的方式引起的。

我的问题本质上是:如何在 MyAdapter.java 中修改我的自定义 CursorAdapter 或如何在 MainFragment.java 中存储我的数据,以便我可以适当地显示分隔符,而不管滚动的方向,还是暂停然后重新滚动?

在此先感谢您!

下面是 MainFragment 的代码、适配器的代码,以及 ListView 项目布局的 XML。

MyAdapter.java

package com.cpsashta.seplistview;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cpsashta.seplistview.DbContract.DbEntry;

public class MyAdapter extends CursorAdapter {

    private LayoutInflater cursorInflater;
    private Context mContext;
    private Cursor mCursor;

    private String lastDate;

    TextView tvSep;
    TextView tvName;
    TextView tvTitle;
    TextView tvDate;
    TextView tvExp;

    public MyAdapter(Context context, Cursor c, int flags) {
        super(context,c,flags);
        this.mContext = context;
        this.mCursor = c;
        cursorInflater = LayoutInflater.from(context);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return cursorInflater.inflate(R.layout.list_layout_1, parent, false);

    }

    @Override
    public void bindView(View view, Context context, Cursor c) {
        tvTitle = (TextView) view.findViewById(R.id.tv_title);
        tvName = (TextView) view.findViewById(R.id.tv_name);
        tvSep = (TextView) view.findViewById(R.id.separator);
        tvDate = (TextView) view.findViewById(R.id.tv_date);
        tvExp = (TextView) view.findViewById(R.id.is_expanded_view);

        String title = c.getString(c.getColumnIndex(DbEntry.COLNAME_TITLE));
        String name = c.getString(c.getColumnIndex(DbEntry.COLNAME_NAME));
        String date = c.getString(c.getColumnIndex(DbEntry.COLNAME_DATE));

        tvTitle.setText(title);
        tvName.setText(name);
        tvDate.setText(date);
        if (!date.equals(lastDate) || c.getPosition() == 0) {
            tvSep.setVisibility(View.VISIBLE);
            tvSep.setText(date);
            tvExp.setText("true");
            lastDate = date;
        } else {
            tvSep.setVisibility(View.GONE);
            tvExp.setText("false");
        }
    }
}

list_layout_1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/separator"
        style="?android:attr/listSeparatorTextViewStyle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:background="#FF0000"
        android:visibility="gone"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:id="@+id/is_expanded_view"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/tv_title"
        android:layout_gravity="center_horizontal" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_date"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Small Text"
        android:id="@+id/tv_name"
        android:layout_gravity="right" />
</LinearLayout>

MainFragment.java

package com.cpsashta.seplistview;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.cpsashta.seplistview.DbContract.*;




public class MainFragment extends Fragment {

    String[] names = {"Dick", "Sammy", "Manuel", "Jenny", "Kirk", "Shimmy"};
    public static String titleSmile = "Damned crazy folks, Smile, you're on camera!";
    String[] titles = {"Here we go again!", titleSmile};
    String dateMonth = "Aug";

    DbHelper mDbHelper;
    SQLiteDatabase db;
    Button button;

    public MainFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        button = (Button) rootView.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LinearLayout ll = (LinearLayout) v.getParent();
                addOneItem(ll);
            }
        });

        return rootView;

    }


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        ListView lv = (ListView) view.findViewById(R.id.listview);
        Cursor c = getAll();
        MyAdapter adapter = new MyAdapter(getActivity(),c,0);
        lv.setAdapter(adapter);

    }

    //Occurs on button press, the view pressed is intended to be the rootView (a.k.a. the LinearLayout housing the ListView and button)
    public void addOneItem(View view) {
        newItemToDb();
        ListView lv = (ListView) view.findViewById(R.id.listview);
        MyAdapter adapter = (MyAdapter) lv.getAdapter();
        adapter.notifyDataSetChanged();
    }

    //Adds random name, title, and date to the database
    public void newItemToDb() {
        if (db == null || !db.isOpen()) {
            if (mDbHelper == null) {
                mDbHelper = new DbHelper(getActivity());
            }
            db = mDbHelper.getWritableDatabase();
        }

        ContentValues values = new ContentValues();
        int nameVal = (int)(Math.random() * names.length);
        values.put(DbEntry.COLNAME_NAME, names[nameVal]);

        int titleVal = (int) (Math.random() * titles.length);
        values.put(DbEntry.COLNAME_TITLE, titles[titleVal]);

        //Chooses a date from August 1 to August 10
        //int dateDay = (int)(Math.random() * 10 + 1);
        //FOR NOW choosing the date of Aug 1 or 2 only
        int dateDay = (int)(Math.random() * 2 + 1);
        String date = dateMonth + " " + dateDay;
        values.put(DbEntry.COLNAME_DATE, date);


        db.insert(DbEntry.TABLE_NAME, null, values);
        db.close();
    }

    public Cursor getAll() {
        if (db == null || !db.isOpen()) {
            if (mDbHelper == null) {
                mDbHelper = new DbHelper(getActivity());
            }
            db = mDbHelper.getWritableDatabase();
        }

       String[] projection = { DbEntry._ID,
               DbEntry.COLNAME_TITLE,
               DbEntry.COLNAME_NAME,
               DbEntry.COLNAME_DATE
       };

        String sortOrder = null;

        //TODO delete the sortOrder below
        //String sortOrder = RecordingEntry.COLNAME_TAG_FULL
        //        + " DESC";
        String[] selectionArgs = null;
        String selection = null; // Setting this to null

        Cursor c = db.query(DbEntry.TABLE_NAME,
                projection,
                selection,
                selectionArgs,
                null, // don't group the rows
                null, // don't filter by row groups
                sortOrder
        );

        return c;
    }

}

编辑:根据@N.T.的建议,我已经投入使用的解决方案将 MainFragment 的 bindView 部分的末尾替换为以下:

        String lastDate = "";
        int position = c.getPosition();
        if (position != 0 ) {
            c.moveToPosition(position - 1);
            lastDate = c.getString(c.getColumnIndex(DbEntry.COLNAME_DATE));
            c.moveToPosition(position);
        }
        if (position == 0 || !date.equals(lastDate)) {
            separator.setVisibility(View.VISIBLE);
            separator.setText(date);
        } else {
            separator.setVisibility(View.GONE);
        }

您无法控制调用 newView/bindView 的顺序。假设您在屏幕上显示项目 10-20,最后绘制的是表格位置 20,因此您的 lastDate 将指向它。现在你向上滚动,系统会要求你绘制第 9 项。此时,您在项目#9 和指向项目#20 的 lastDate 之间进行比较,这不是您想要的。

要修复问题,您需要将光标移动到上一个位置 (mCursor.moveToPosition(position - 1)) 并对照列表中的上一个项目检查当前项目,而不是对照之前请求绘制的项目。