问题是你有一个列表Bean
并且您想在新的月份时添加标题。因此,当月份发生变化时,beanArrayListToPopulate
你需要添加Bean
and标题。所以你需要创建一个额外的Bean
添加标题。
所以 for 循环应该是这样的(在MainActivity
)
String prevMonth = null;
String newMonth;
String createdDate;
for (Bean bean : beanArrayList) {
createdDate = bean.getCreatedDate();
// Create header Bean if necessary
if (createdDate != null) {
newMonth = createdDate.split(" ")[1].trim();
if (!newMonth.equalsIgnoreCase(prevMonth)) {
Bean headerBean = new Bean();
headerBean.setViewType(Adapter.VIEW_TYPE_HEADER);
headerBean.setHeaderString(newMonth);
beanArrayListToPopulate.add(headerBean);
prevMonth = newMonth;
}
}
// Update data Bean
bean.setViewType(Adapter.VIEW_TYPE_ITEM);
beanArrayListToPopulate.add(bean);
}
编辑:你也可以尝试这种方法
但我认为我最好使用专用对象来不污染您检索的数据(Bean
),信息仅用于显示它们。
所以你能做的就是创建一个ListItem
对象并在您的适配器中使用它而不是Bean
itself.
活动中
private ArrayList<Bean> beanArrayList = new ArrayList<>();
private ArrayList<ListItem> mListItems = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addDataToBean();
rv = findViewById(R.id.rv);
rv.setLayoutManager(new LinearLayoutManager(this));
String prevMonth = null;
String newMonth;
String createdDate;
for (Bean bean : beanArrayList) {
createdDate = bean.getCreatedDate();
// Create a header if necessary
if (createdDate != null) {
newMonth = createdDate.split(" ")[1].trim();
if (!newMonth.equalsIgnoreCase(prevMonth)) {
ListItem headerItem = new ListItem(newMonth, Adapter.VIEW_TYPE_HEADER);
mListItems.add(headerItem);
prevMonth = newMonth;
}
}
// Create a new item with the Bean data
ListItem item = new ListItem(bean.getFirstName(), Adapter.VIEW_TYPE_ITEM);
mListItems.add(item);
}
rv.setAdapter(new Adapter(mListItems));
}
在适配器中(我只放置了我更改过的内容)
private final ArrayList<ListItem> mItems;
public Adapter(ArrayList<ListItem> items) {
this.mItems = items;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
if (viewType == VIEW_TYPE_HEADER) {
view = inflater.inflate(R.layout.header, parent, false);
return new VHHeader(view);
} else if (viewType == VIEW_TYPE_ITEM) {
view = inflater.inflate(R.layout.item, parent, false);
return new VHItem(view);
}
throw new RuntimeException("There is no type that matches the type " + viewType + ". Make sure you are using view types correctly!");
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
ListItem item = mItems.get(position);
// Note: You have the type information directly inside the ViewHolder so you don't need
// to check the ViewHolder's instance type
switch (holder.getItemViewType()) {
case VIEW_TYPE_HEADER:
((VHHeader) holder).tvHeader.setText(item.getData());
break;
case VIEW_TYPE_ITEM:
((VHItem) holder).tvItem.setText(item.getData());
break;
}
}
列表项
public class ListItem {
private String mData;
private int mType;
public ListItem(String data, int type) {
mData = data;
mType = type;
}
public String getData() {
return mData;
}
public void setData(String data) {
mData = data;
}
public int getType() {
return mType;
}
}
Note:也许代码需要一些调整,因为我还没有测试过。