我需要在RecyclerView
中显示某种访客日志,并且要显示的数据来自SQLiteDatabase
DESC
-按DateTime排序(列enter_time
,UNIX值)。我已经实现了一个简单的CursorAdapter
,并且能够在RecyclerView
中显示数据。
下一步是制作不同的视图,以便每天的第一项使用带附加日期标题的布局:
在CursorAdapter
内部,我实现了getItemViewType(int)
方法,需要在其中添加选择正确项目类型的逻辑。
逻辑很简单:
如果Cursor
中的下一个项目与当前日期相同,则选择简单的布局
如果Cursor
中的下一项与当前日期不同,则选择带标题的布局
问题来了:我需要检查Cursor
的CursorAdapter
中的下一个项目,但是显示的结果是错误的,我不知道为什么。代码对我来说似乎正确,但是布局是随机分配给项目的。
public static class VisitorsRecyclerAdapter extends RecyclerView.Adapter<VisitorsRecyclerAdapter.VisitorViewHolder> {
CustomCursorAdapter mCursorAdapter;
Context mContext;
private static final int NO_HEADER_LAYOUT = 1;
private static final int HEADER_LAYOUT = 0;
public VisitorsRecyclerAdapter(Context context, Cursor c) {
mContext = context;
mCursorAdapter = new CustomCursorAdapter(mContext, c, 0);
}
public static class VisitorViewHolder extends RecyclerView.ViewHolder {
public TextView textName;
public TextView textCats;
public VisitorViewHolder(View v) {
super(v);
textName = (TextView) v.findViewById(R.id.item_visitor_name);
textCats = (TextView) v.findViewById(R.id.item_visitor_category);
}
}
public static class HeaderViewHolder extends RecyclerView.ViewHolder {
public TextView textName;
public TextView textCats;
public HeaderViewHolder(View v) {
super(v);
textName = (TextView) v.findViewById(R.id.item_visitor_name);
textCats = (TextView) v.findViewById(R.id.item_visitor_category);
}
}
private class CustomCursorAdapter extends CursorAdapter {
public CustomCursorAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
Cursor cursor = (Cursor) mCursorAdapter.getItem(position);
cursor.moveToNext();
// date of current item
Date date0 = new Date(Long.parseLong(cursor.getString(cursor.getColumnIndex(DatabaseConst.VisitorEntry.COLUMN_NAME_ENTER))) * 1000);
if(position == -1) return HEADER_LAYOUT;
cursor = (Cursor) mCursorAdapter.getItem(position - 1);
cursor.moveToNext();
// date of item that temporary comes after
Date date1 = new Date(Long.parseLong(cursor.getString(cursor.getColumnIndex(DatabaseConst.VisitorEntry.COLUMN_NAME_ENTER))) * 1000);
SimpleDateFormat format = new SimpleDateFormat("ddMMyyyy", Locale.ENGLISH);
return format.format(date0).equals(format.format(date1)) ? NO_HEADER_LAYOUT : HEADER_LAYOUT;
}
@Override
public View newView(final Context context, Cursor cursor, ViewGroup parent) {
View v = null;
int position = cursor.getPosition();
int type = getItemViewType(position);
RecyclerView.ViewHolder viewHolder = null;
switch (type) {
case HEADER_LAYOUT:
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_day, parent, false);
viewHolder = new HeaderViewHolder(v);
break;
case NO_HEADER_LAYOUT:
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_visitor, parent, false);
viewHolder = new VisitorViewHolder(v);
break;
}
assert v != null;
v.setTag(viewHolder);
return v;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
final int viewType = getItemViewType(cursor.getPosition());
switch (viewType) {
case HEADER_LAYOUT:
HeaderViewHolder holder = (HeaderViewHolder) view.getTag();
holder.textName.setText(DateFormat.getDateTimeInstance().format(new Date(Long.parseLong(cursor.getString(cursor.getColumnIndex(DatabaseConst.VisitorEntry.COLUMN_NAME_ENTER))) * 1000)));
holder.textCats.setText(cursor.getString(cursor.getColumnIndex(DatabaseConst.VisitorEntry.COLUMN_NAME_FIRST_NAME)));
break;
case NO_HEADER_LAYOUT:
VisitorViewHolder holder0 = (VisitorViewHolder) view.getTag();
holder0.textName.setText(DateFormat.getDateTimeInstance().format(new Date(Long.parseLong(cursor.getString(cursor.getColumnIndex(DatabaseConst.VisitorEntry.COLUMN_NAME_ENTER))) * 1000)));
holder0.textCats.setText(cursor.getString(cursor.getColumnIndex(DatabaseConst.VisitorEntry.COLUMN_NAME_FIRST_NAME)));
break;
}
}
}
@Override
public int getItemCount() {
if(mCursorAdapter == null) return 0;
return mCursorAdapter.getCount();
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
mCursorAdapter.getCursor().moveToPosition(position);
mCursorAdapter.bindView(holder.itemView, mContext, mCursorAdapter.getCursor());
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = mCursorAdapter.newView(mContext, mCursorAdapter.getCursor(), parent);
RecyclerView.ViewHolder viewHolder = null;
switch (viewType){
case HEADER_LAYOUT:
viewHolder = new HeaderViewHolder(v);
break;
case NO_HEADER_LAYOUT:
viewHolder = new VisitorViewHolder(v);
break;
}
return viewHolder;
}
}
这是实际的错误结果:
我查看了SO,但找不到需要从相邻项中恢复数据的类似情况(我不能肯定这是问题所在)。您将提出什么解决此问题的建议?
最佳答案
在您从getItemViewType
返回值之前,这似乎很正确。您仅创建了一个ViewHolder
,即VisitorViewHolder
。
但是还有另一种更好的方法来实现此目的,创建多个ViewHolder
类,然后在onCreateViewHolder
中切换它们。
像这样修改
@Override
public VisitorViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType){
case HEADER_LAYOUT:
View v = mCursorAdapter.newView(mContext, mCursorAdapter.getCursor(), parent);
final VisitorViewHolder holder = new VisitorViewHolder(v);
return holder;
case NO_HEADER_LAYOUT:
//Return you another viewholder which contains view with no header
}
}
请参阅here以获取更多参考,这将使您的任务更加轻松。