基于下一项的 CursorAdapter 中的多行布局
Multiple row layouts in CursorAdapter based on next item
我需要在 RecyclerView
中显示一种访问者日志,要显示的数据来自 SQLiteDatabase
DESC
- 按日期时间排序(第 enter_time
列, UNIX 值)。我已经实现了一个简单的 CursorAdapter
并且我能够在 RecyclerView
.
中显示数据
下一步是制作不同的视图,以便每天的第一个条目使用带有附加 header 的布局,日期为:
在 CursorAdapter
中,我实现了 getItemViewType(int)
方法,我需要在其中添加我的逻辑 select 正确的项目类型。
逻辑很简单:
- 如果
Cursor
中的下一项与当前的日期相同,则 select 简单布局
- 如果
Cursor
中的下一项与当前的日期不同,则 select 布局 header
问题来了: 我需要检查CursorAdapter
的Cursor
中的下一项,但是显示的结果是错误的,我不能'不要说为什么。代码在我看来是正确的,但布局是随机分配给项目的。
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
class 并将它们切换到 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 以获取更多参考,这将使您的任务更加轻松。
发生了两个不同的问题。
物品回收
适配器正在回收针对不同项目类型扩充的视图,因此绑定时发生异常,因为 view.getTag()
包含错误的 ViewHolder
类型。为了修复它,我将 StableIds
设置为 false。
@Override
public void setHasStableIds(boolean hasStableIds) {
super.setHasStableIds(false);
}
类型分配逻辑
逻辑上是正确的,但我调整了光标的位置,但在完成后没有将其设置为以前的值。
@Override
public int getItemViewType(int pos) {
Integer position = pos;
// with -1 position you can return whatever you want
if(position.equals(-1)) { return HEADER_LAYOUT; }
Cursor cursor = mCursorAdapter.getCursor();
if(!position.equals(0)) {
cursor.moveToPosition(position);
Date date0 = new Date(Long.parseLong(cursor.getString(cursor.getColumnIndex(DatabaseConst.VisitorEntry.COLUMN_NAME_ENTER))) * 1000);
cursor.moveToPosition(position - 1);
Date date1 = new Date(Long.parseLong(cursor.getString(cursor.getColumnIndex(DatabaseConst.VisitorEntry.COLUMN_NAME_ENTER))) * 1000);
SimpleDateFormat format = new SimpleDateFormat("ddMMyyyy", Locale.ENGLISH);
cursor.moveToPosition(position);
return ((format.format(date0).equals(format.format(date1))) ? NO_HEADER_LAYOUT : HEADER_LAYOUT);
} else {
// position = 0 -> first item in list always need header
cursor.moveToPosition(position); // important!
return HEADER_LAYOUT;
}
}
如您所见,在从方法返回之前,我再次设置了光标位置。
我需要在 RecyclerView
中显示一种访问者日志,要显示的数据来自 SQLiteDatabase
DESC
- 按日期时间排序(第 enter_time
列, UNIX 值)。我已经实现了一个简单的 CursorAdapter
并且我能够在 RecyclerView
.
下一步是制作不同的视图,以便每天的第一个条目使用带有附加 header 的布局,日期为:
在 CursorAdapter
中,我实现了 getItemViewType(int)
方法,我需要在其中添加我的逻辑 select 正确的项目类型。
逻辑很简单:
- 如果
Cursor
中的下一项与当前的日期相同,则 select 简单布局 - 如果
Cursor
中的下一项与当前的日期不同,则 select 布局 header
问题来了: 我需要检查CursorAdapter
的Cursor
中的下一项,但是显示的结果是错误的,我不能'不要说为什么。代码在我看来是正确的,但布局是随机分配给项目的。
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
class 并将它们切换到 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 以获取更多参考,这将使您的任务更加轻松。
发生了两个不同的问题。
物品回收
适配器正在回收针对不同项目类型扩充的视图,因此绑定时发生异常,因为 view.getTag()
包含错误的 ViewHolder
类型。为了修复它,我将 StableIds
设置为 false。
@Override
public void setHasStableIds(boolean hasStableIds) {
super.setHasStableIds(false);
}
类型分配逻辑
逻辑上是正确的,但我调整了光标的位置,但在完成后没有将其设置为以前的值。
@Override
public int getItemViewType(int pos) {
Integer position = pos;
// with -1 position you can return whatever you want
if(position.equals(-1)) { return HEADER_LAYOUT; }
Cursor cursor = mCursorAdapter.getCursor();
if(!position.equals(0)) {
cursor.moveToPosition(position);
Date date0 = new Date(Long.parseLong(cursor.getString(cursor.getColumnIndex(DatabaseConst.VisitorEntry.COLUMN_NAME_ENTER))) * 1000);
cursor.moveToPosition(position - 1);
Date date1 = new Date(Long.parseLong(cursor.getString(cursor.getColumnIndex(DatabaseConst.VisitorEntry.COLUMN_NAME_ENTER))) * 1000);
SimpleDateFormat format = new SimpleDateFormat("ddMMyyyy", Locale.ENGLISH);
cursor.moveToPosition(position);
return ((format.format(date0).equals(format.format(date1))) ? NO_HEADER_LAYOUT : HEADER_LAYOUT);
} else {
// position = 0 -> first item in list always need header
cursor.moveToPosition(position); // important!
return HEADER_LAYOUT;
}
}
如您所见,在从方法返回之前,我再次设置了光标位置。