ShamylZakariya / StickyHeaders

Adapter and LayoutManager for Android RecyclerView which enables sticky header positioning.
MIT License
1.4k stars 185 forks source link

onCreateHeaderViewHolder is not called #85

Open ahmedalaa92 opened 6 years ago

ahmedalaa92 commented 6 years ago

the onCreateHeaderViewHolder override method is not being called however onCreateItemViewHolder is called and working fine Kindly check my adapter class below `

public class MenuRecyclerAdapter extends SectioningAdapter {

private Context context;
private ArrayList<Menu> data;
private ArrayList<Section> sections = new ArrayList<>();

public MenuRecyclerAdapter(Context context, ArrayList<Menu> menuList) {
    this.context = context;
    this.data = menuList;
    populateSections();
}

private void populateSections() {
    for (int i = 0; i < data.size(); i++) {
        appendSection(i, data.get(i).getMenuListArrayList().size());
    }
}

private void appendSection(int index, int itemCount) {
    Section section = new Section();
    section.index = index;
    section.copyCount = 0;
    section.header = data.get(index).getCategoryName();

    for (int j = 0; j < itemCount; j++) {
        section.items.add(data.get(index).getMenuListArrayList().get(j).getItemNameEn());
    }

    sections.add(section);
}

@Override
public ItemViewHolder onCreateItemViewHolder(ViewGroup parent, int itemType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View v = inflater.inflate(R.layout.menu_item, parent, false);
    return new ItemViewHolder(v);
}

@Override
public HeaderViewHolder onCreateHeaderViewHolder(ViewGroup parent, int headerType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View v = inflater.inflate(R.layout.header_view_sticky_header, parent, false);
    return new HeaderViewHolder(v);
}

@Override
public void onBindItemViewHolder(SectioningAdapter.ItemViewHolder viewHolder, int sectionIndex, int itemIndex, int itemType) {
    Section s = sections.get(sectionIndex);
    ItemViewHolder ivh = (ItemViewHolder) viewHolder;
    ivh.txtMenuItemName.setText(s.items.get(itemIndex));

// ivh.adapterPositionTextView.setText(Integer.toString(getAdapterPositionForSectionItem(sectionIndex, itemIndex))); }

@Override
public void onBindHeaderViewHolder(SectioningAdapter.HeaderViewHolder viewHolder, int sectionIndex, int headerType) {
    Section s = sections.get(sectionIndex);
    HeaderViewHolder hvh = (HeaderViewHolder) viewHolder;

// hvh.adapterPositionTextView.setText(Integer.toString(getAdapterPositionForSectionHeader(sectionIndex)));

// if (USE_DEBUG_APPEARANCE) { // hvh.textView.setText(pad(sectionIndex * 2) + s.header); // viewHolder.itemView.setBackgroundColor(0x55FF9999); // } else { hvh.txtHeader.setText(s.header); // }

// hvh.updateSectionCollapseToggle(isSectionCollapsed(sectionIndex)); }

private class Section {
    int index;
    int copyCount;
    String header;
    String footer;
    ArrayList<String> items = new ArrayList<>();

    public Section duplicate() {
        Section c = new Section();
        c.index = this.index;
        c.copyCount = this.copyCount + 1;
        c.header = c.index + " copy " + c.copyCount;
        c.footer = this.footer;
        for (String i : this.items) {
            c.items.add(i + " copy " + c.copyCount);
        }

        return c;
    }

    public void duplicateItem(int item) {
        String itemCopy = items.get(item) + " copy";
        items.add(item + 1, itemCopy);
    }

}

public class ItemViewHolder extends SectioningAdapter.ItemViewHolder {
    TextView txtMenuItemName;

    ItemViewHolder(View itemView) {
        super(itemView);
        txtMenuItemName = itemView.findViewById(R.id.txtMenuItemName);
    }
}

public class HeaderViewHolder extends SectioningAdapter.HeaderViewHolder {
    TextView txtHeader;

    HeaderViewHolder(View itemView) {
        super(itemView);
        txtHeader = itemView.findViewById(R.id.txtHeader);
    }
}

@Override
public int getNumberOfSections() {
    return sections.size();
}

@Override
public int getNumberOfItemsInSection(int sectionIndex) {
    return sections.get(sectionIndex).items.size();
}

} `

baiguangyuan commented 6 years ago

Did you override doesSectionHaveHeader function of SectioningAdapter.


    /**
     * @param sectionIndex index of the section in question
     * @return true if this section has a header
     */
    public boolean doesSectionHaveHeader(int sectionIndex) {
        return false;
    }