A
Data management such as to dissolve JSON in model facilities, each model will contain a field of date and a field of recording. The number shall be quashed at the height of the date.We'll need two kinds of markings first. The first will display the date and the record.layout.headerSecond - only recordlayout.record) Marking layout.header includes the marking layout.record ♪ include♪layout.record: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id = "@+id/record" />
</LinearLayout>
layout.header: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id = "@+id/date" />
<include
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
The logic of the definition of which type of View is based on the comparison of the date of the current recording and the past - if they are not equal - remove the title with the date and the record, otherwise only the record. The zero element is treated separately and always contains a date and a record.Also, please draw attention to the organization of the block switch - case in method. onBindViewHolder()♪ When View draws up the date of the case, turns the date on the mark and passes on the next case. breakwhere the tape is marked. If the heading is not required, the date shall not be marked (second case)class SomeAdapter extends RecyclerView.Adapter <SomeAdapter.ItemHolder> {
private ArrayList <Data> mData;
private final int TYPE_HEADER = 0;
private final int TYPE_ITEM = 1;
public SomeAdapter (ArrayList <Data> data) {
mData = data;
}
@Override
public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v;
switch (viewType) {
case TYPE_HEADER:
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.header, parent, false);
break;
default:
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.record, parent, false);
}
return new ItemHolder(v);
}
@Override
public void onBindViewHolder( ItemHolder holder, int position) {
int type = getItemViewType(position);
switch (type) {
case TYPE_HEADER:
holder.mHeaderDate.setText(mData.get(position).getDate()));
case TYPE_ITEM:
holder.mItemRecord.setText(mData.get(position).getRecord());
break;
}
}
@Override
public int getItemCount() {
return mData.size();
}
@Override
public int getItemViewType(int position) {
if (isIdentType(position)) return TYPE_ITEM;
return TYPE_HEADER;
}
private boolean isIdentType (int position ){
if (!(position == 0)&&(mData.get(position).getDate()).equals(mData.get(position-1).getDate())) return true;
return false;
}
public static class ItemHolder extends RecyclerView.ViewHolder{
TextView mHeaderDate;
TextView mItemRecord;
public ItemHolder(View v) {
super(v);
mHeaderDate = (TextView) v.findViewById(R.id.date);
mItemRecord = (TextView) v.findViewById(R.id.record);
}
}
Of course, the markings need to be painted in a way that the date can be set by another background or another. This example is just an idea.