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

时间:2015-08-14 14:39:58

标签: android android-listview android-cursoradapter

在ListView(使用自定义CursorAdapter)中,我试图仅根据两个ListView项之间的日期是否发生变化来显示分隔符,如下所示:as so http://i61.tinypic.com/vmqjkj.png

不幸的是,我设置代码的方式,当我向下滚动,暂停一秒钟,然后再次开始滚动,我让分隔符在错误的位置出现(例如在这里,看到消失了ListView中第二项中的分隔符): see the disappearance of the second item http://i61.tinypic.com/14lk7j7.jpg

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

我遇到了两个主要问题:

  1. 日期分隔符的出现和消失在不同时间取决于滚动的暂停,和
    1. 日期分隔符的外观,因为它在错误的点上连续向上滚动。
  2. 我原则上理解这些问题的出现是因为我的适配器中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);
            }
    

1 个答案:

答案 0 :(得分:2)

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

要解决问题,您需要将光标移动到上一个位置(mCursor.moveToPosition(position - 1))并检查当前项目与列表中的上一个项目,而不是先前请求的项目。