onBindViewHolder вызывается еще раз
Я использую эту библиотеку для stickyHeader для recyclerview, и я хочу запустить Filter
в теме. проблема в onBindItemViewHolder
называется еще раз, и это делает throwIndexOutOfBoundsException
, где моя ошибка?
это мой адаптер:
public class RowsAdapter extends StickyHeaderGridAdapter implements Filterable {
private List<RowHeaderJavaModel> headers;
private LinkedHashMap<RowHeaderJavaModel , List<RowItemJavaModel>> mainList , filterList;
private ContentFragmentVM.CustomFilter filter;
public RowsAdapter(ContentFragmentVM viewmodel) {
filter = viewmodel.new CustomFilter(this);
this.mainList = new LinkedHashMap<>();
}
@Override
public HeaderViewHolder onCreateHeaderViewHolder(ViewGroup parent, int headerType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
RowHeaderItemBinding binding = DataBindingUtil.inflate(inflater, R.layout.row_header_item, parent, false);
return new MyHeaderViewHolder(binding);
}
@Override
public ItemViewHolder onCreateItemViewHolder(ViewGroup parent, int itemType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
RowItemBinding binding = DataBindingUtil.inflate(inflater, R.layout.row_item, parent, false);
return new MyItemViewHolder(binding);
}
@Override
public void onBindHeaderViewHolder(HeaderViewHolder viewHolder, int section) {
((MyHeaderViewHolder)viewHolder).setHeaderModel(this.headers.get(section));
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onBindItemViewHolder(ItemViewHolder viewHolder, int section, int offset) {
Log.e("faeze bind",getSectionCount()+" "+section+" offset "+getSectionItemCount(section)+" "+offset);
((MyItemViewHolder)viewHolder).setItemModel(Objects.requireNonNull(filterList.get(this.headers.get(section))).get(offset));
}
public void setLists(List<RowItemJavaModel> unSectionedItems , List<RowHeaderJavaModel> headers )
{
this.filterList = new LinkedHashMap<>();
List<RowItemJavaModel> sectionedItems = new ArrayList<>();
this.headers = new ArrayList<>();
this.headers.addAll(headers);
unSectionedItems.get(0).setChapterChanged(true);
unSectionedItems.get(0).setSessionChanged(true);
sectionedItems.add(unSectionedItems.get(0));
for(int i=1 ; i<unSectionedItems.size() ; i++)
{
unSectionedItems.get(i).setChapterChanged(!unSectionedItems.get(i).getChapter().equals(unSectionedItems.get(i - 1).getChapter()));
unSectionedItems.get(i).setSessionChanged(unSectionedItems.get(i).getSessionNo()!= unSectionedItems.get(i - 1).getSessionNo());
sectionedItems.add(unSectionedItems.get(i));
if(unSectionedItems.get(i).isSessionChanged())
{
RowItemJavaModel temp = unSectionedItems.get(i);
sectionedItems.remove(sectionedItems.size()-1);
mainList.put(headers.get(unSectionedItems.get(i-1).getSessionNo()-1),sectionedItems);
sectionedItems = new ArrayList<>();
sectionedItems.add(temp);
}
}
mainList.put(headers.get(unSectionedItems.get(unSectionedItems.size()-1).getSessionNo()-1),sectionedItems);
filterList.putAll(mainList);
this.notifyDataSetChanged();
}
public void setFilterList(List<RowHeaderJavaModel> headers)
{
this.headers = new ArrayList<>();
this.headers.addAll(headers);
filterList = new LinkedHashMap<>();
for(int i=0 ; i<this.headers.size() ; i++)
{
filterList.put(this.headers.get(i),mainList.get(this.headers.get(i)));
}
this.notifyDataSetChanged();
}
@Override
public int getSectionCount() {
return headers.size();
}
@Override
public int getSectionItemCount(int section) {
return filterList.get(this.headers.get(section)).size();
}
@Override
public Filter getFilter() {
return filter;
}
public static class MyHeaderViewHolder extends HeaderViewHolder {
RowHeaderItemBinding headerBinding;
MyHeaderViewHolder(RowHeaderItemBinding binding)
{
super(binding.getRoot());
headerBinding = binding;
}
void setHeaderModel(RowHeaderJavaModel model)
{
headerBinding.setModel(model);
}
}
public static class MyItemViewHolder extends ItemViewHolder {
RowItemBinding itemBinding;
MyItemViewHolder(RowItemBinding binding)
{
super(binding.getRoot());
itemBinding = binding;
}
void setItemModel(RowItemJavaModel model)
{
itemBinding.setModel(model);
}
}
}
и это мой фильтр:
public class CustomFilter extends Filter
{
RowsAdapter adapter;
final FilterResults results = new FilterResults();
boolean isPageFilter;
public CustomFilter(RowsAdapter adapter) {
this.adapter = adapter;
}
public void setPageFilter(boolean pageFilter) {
isPageFilter = pageFilter;
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
filterItems.clear();
filterHeader.clear();
if (constraint.length() == 0)
{
if(isPageFilter)
filterItems.addAll(items);
else
filterHeader.addAll(headers);
}
else
{
final String filterPattern = constraint.toString().toLowerCase().trim();
if(isPageFilter)
for (int i=0 ; i<items.size() ; i++) {
if (items.get(i).getPage().contains(filterPattern)) {
filterItems.add(items.get(i));
}
}
else
for (int i=0 ; i<headers.size() ; i++) {
if (headers.get(i).getSessionNumberString().contains(filterPattern)) {
filterHeader.add(headers.get(i));
}
}
}
if(isPageFilter)
{
results.values = filterItems;
results.count = filterItems.size();
}
else
{
results.values = filterHeader;
results.count = filterHeader.size();
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if(isPageFilter)
this.adapter.setLists(filterItems,headers);
else
this.adapter.setFilterList((List<RowHeaderJavaModel>) results.values);
}
}