我使用luizgrp/SectionedRecyclerViewAdapter中的SectionedRecyclerViewAdapter
作为我的RecyclerView的适配器。
使用RecyclerViewAdapter,如果我想唯一地标识每一行,我将覆盖此方法:
@Override
public long getItemId( int position ) {
return this.dataList.get(position).getId();
}
但是如何使用SectionedRecyclerViewAdapter
执行此操作?我有以下部分代码,我添加了getId()方法:
public class Section1 extends Section {
.....
public long getItemId(int position) {
if (position == 0) {
return 0;
}
return this.openPosList.get(position - 1).getId();
}
}
我想我会扩展SectionedRecyclerViewAdapter
&覆盖getItemId()
。但是我在这里将position
转换为Section的行位置时遇到了问题。
public class PositionRecylerViewAdapter extends SectionedRecyclerViewAdapter {
......
@Override
public long getItemId(int position) {
// ????
// transform position here into Section's item position, considering multiple Sections with Header & Footer
// ????
}
}
之前有人使用任何示例代码实现类似的代码吗?谢谢!
答案 0 :(得分:1)
我弄清楚&在我的PositionRecylerViewAdapter
。
public class PositionRecyclerViewAdapter extends SectionedRecyclerViewAdapter {
public PositionRecyclerViewAdapter() {
super();
......
this.setHasStableIds(true);
}
.......
@Override
public long getItemId (int index) {
int viewType = this.getSectionItemViewType(index);
Section1 section1 = (Section1) this.getSection(POSITION_SECTION);
if (viewType == SectionedRecyclerViewAdapter.VIEW_TYPE_HEADER) {
return section1.getHeaderId();
} else if (viewType == SectionedRecyclerViewAdapter.VIEW_TYPE_ITEM_LOADED) {
int sectionItemIndex = this.getPositionInSection(index);
return section1.getItemId(sectionItemIndex);
}
return -1;
}
}
在Section1
课程中:
public class Section1 extends Section {
.....
public long getHeaderId() {
return headerId;
}
public long getItemId(int index) {
return this.openPosList.get(index).getId();
}
}
发表我自己的回答&希望下次有人发现它有用,谢谢!