我正在做一个RecyclerView
有两个不同项目的项目,但我不知道我该怎么做。
首先:我有ArrayList
一个对象,但我不知道如何将与日期相关的时间放在RecyclerView
.
日期 - 组项目。
时间 - 子项。
Object { Date date; ArrayList times; }
第二:当项目的位置不是预期时,如何根据适配器中的项目更改 spanSize?( GridLayoutManager
)
我正在做一个RecyclerView
有两个不同项目的项目,但我不知道我该怎么做。
首先:我有ArrayList
一个对象,但我不知道如何将与日期相关的时间放在RecyclerView
.
日期 - 组项目。
时间 - 子项。
Object {
Date date;
ArrayList times;
}
第二:当项目的位置不是预期时,如何根据适配器中的项目更改 spanSize?( GridLayoutManager
)
要回答您的第二部分,要根据项目类型(组或子项)更改跨度大小,请在GridLayoutManager
. 在您的适配器中,您getItemViewType()
根据位置实现并返回视图类型。例如,0 表示组,1 表示孩子,在这种情况下MyAdapter.VIEW_TYPE_GROUP
为 0。
RecyclerView list = (RecyclerView) view.findViewById(
R.id.my_fragment_my_recycler_view);
GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 5);
layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
if (list.getAdapter().getItemViewType(position) == MyAdapter.VIEW_TYPE_GROUP) {
// return the number of columns so the group header takes a whole row
return 5
}
// normal child item takes up 1 cell
return 1;
}
});
您可以使用库SectionedRecyclerViewAdapter将您的项目分组并为每个部分添加标题:
class MySection extends StatelessSection {
String date;
List<String> list;
public MySection(String date, List<String> list) {
// call constructor with layout resources for this Section header, footer and items
super(R.layout.section_header, R.layout.section_item);
this.date = date;
this.list = list;
}
@Override
public int getContentItemsTotal() {
return list.size(); // number of items of this section
}
@Override
public RecyclerView.ViewHolder getItemViewHolder(View view) {
// return a custom instance of ViewHolder for the items of this section
return new MyItemViewHolder(view);
}
@Override
public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
MyItemViewHolder itemHolder = (MyItemViewHolder) holder;
// bind your view here
itemHolder.tvItem.setText(list.get(position));
}
@Override
public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
return new SimpleHeaderViewHolder(view);
}
@Override
public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
MyHeaderViewHolder headerHolder = (MyHeaderViewHolder) holder;
// bind your header view here
headerHolder.tvItem.setText(date);
}
}
然后你用你的部分设置 RecyclerView 并使用 GridLayoutManager 更改标题的 spanSize:
// Create an instance of SectionedRecyclerViewAdapter
SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();
// Create your sections with the list of data
MySection date1Section = new MySection("Date 1", date1List);
MySection date2Section = new MySection("Date 2", date2List);
// Add your Sections to the adapter
sectionAdapter.addSection(date1Section);
sectionAdapter.addSection(date2Section);
// set up a gridlayoutmanager to change the spansize of the header
GridLayoutManager glm = new GridLayoutManager(getContext(), 2);
glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
switch(sectionAdapter.getSectionItemViewType(position)) {
case SectionedRecyclerViewAdapter.VIEW_TYPE_HEADER:
return 2;
default:
return 1;
}
}
});
// Set up your RecyclerView with the SectionedRecyclerViewAdapter
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(glm);
recyclerView.setAdapter(sectionAdapter);