ShamylZakariya / StickyHeaders

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

No adapter attached; skipping layout issue inside fragment #86

Open hpmuthu opened 6 years ago

hpmuthu commented 6 years ago

No adapter attached; skipping layout issue inside fragment. My recyclerView is empty while using StickyHeaderLayoutManager. Working correctly with LinearLayoutManager.

adapter = new StockListAdapter(getActivity(),productStockList,width,height); stockRecyclerView.setAdapter(adapter); stockRecyclerView.setLayoutManager(new StickyHeaderLayoutManager());

public class StockListAdapter extends SectioningAdapter {

    private Context context;
    private ArrayList<ProductStockObj> dataList = new ArrayList<>();
    private int width, height;
    ArrayList<Section> sectionsList = new ArrayList<>();

    private class Section {
        String categoryName;
        ArrayList<ProductStockObj> productsList = new ArrayList<>();
    }

    public class HeaderViewHolder extends SectioningAdapter.HeaderViewHolder {
        TextView listHeaderTextView;

        public HeaderViewHolder(View itemView) {
            super(itemView);
            listHeaderTextView = (TextView) itemView.findViewById(R.id.listHeaderTextView);
        }
    }

    public class ItemViewHolder extends SectioningAdapter.ItemViewHolder {
        LinearLayout rowMainLayout;
        ImageView productImageView;
        TextView productTextView;
        LinearLayout stockSaleLayout;
        TextView stockTextView;
        TextView salesTextView;

        public ItemViewHolder(View itemView) {
            super(itemView);
            rowMainLayout = (LinearLayout)itemView.findViewById( R.id.rowMainLayout );
            productImageView = (ImageView)itemView.findViewById( R.id.productImageView );
            productTextView = (TextView)itemView.findViewById( R.id.productTextView );
            stockSaleLayout = (LinearLayout)itemView.findViewById( R.id.stockSaleLayout );
            stockTextView = (TextView)itemView.findViewById( R.id.stockTextView );
            salesTextView = (TextView)itemView.findViewById( R.id.salesTextView );

            int rowMainLayLeftRightPadding = (int) (width * 0.03225);//0.3
            int rowMainLayTopBottomPadding = (int) (width * 0.01290);//0.2
            int productImageRightMargin = (int) (width * 0.04301);//0.4
            int productTextRightMargin = (int) (width * 0.08602);//0.8

            rowMainLayout.setPadding(rowMainLayLeftRightPadding,rowMainLayTopBottomPadding,
                                    rowMainLayLeftRightPadding,rowMainLayTopBottomPadding);

            LinearLayout.LayoutParams productImage = (LinearLayout.LayoutParams) productImageView.getLayoutParams();
            productImage.setMargins(0,0,productImageRightMargin,0);
            productImageView.setLayoutParams(productImage);

            LinearLayout.LayoutParams productText = (LinearLayout.LayoutParams) productTextView.getLayoutParams();
            productText.setMargins(0,0,productTextRightMargin,0);
            productTextView.setLayoutParams(productText);
        }
    }

    public StockListAdapter(Context context, ArrayList<ProductStockObj> dataList, int width, int height) {
        this.context = context;
        this.dataList = dataList;
        this.width = width;
        this.height = height;
    }

    public ArrayList<ProductStockObj> getProductStockList() {
        return dataList;
    }

    public void setProductStockList(ArrayList<ProductStockObj> dataList) {
        this.dataList = dataList;
        sectionsList.clear();

        Section currentSection = null;
        if(dataList.size() > 0) {

            int sectionCategoryType = 0;
            for (int i = 0; i < dataList.size(); i++) {
                ProductStockObj productStockObj = dataList.get(i);
                int categoryType = productStockObj.getCategoryType();
                if(sectionCategoryType != categoryType) {
                    if (currentSection != null) {
                        sectionsList.add(currentSection);
                    }

                    currentSection = new Section();
                    sectionCategoryType = productStockObj.getCategoryType();
                    switch (sectionCategoryType) {
                        case 1:
                            currentSection.categoryName = context.getResources().getString(R.string.breads);
                            break;
                        case 2:
                            currentSection.categoryName = context.getResources().getString(R.string.buns);
                            break;
                        case 3:
                            currentSection.categoryName = context.getResources().getString(R.string.cookies);
                            break;
                    }

                    currentSection.productsList.add(productStockObj);

                } else if (currentSection != null) {
                    currentSection.productsList.add(productStockObj);
                }
            }
        }

        sectionsList.add(currentSection);
        notifyAllSectionsDataSetChanged();
    }

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

    @Override
    public int getNumberOfItemsInSection(int sectionIndex) {
        return sectionsList.get(sectionIndex).productsList.size();
    }

    @Override
    public boolean doesSectionHaveHeader(int sectionIndex) {
        return true;
    }

    @Override
    public boolean doesSectionHaveFooter(int sectionIndex) {
        return false;
    }

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

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

    @Override
    public void onBindHeaderViewHolder(SectioningAdapter.HeaderViewHolder viewHolder, int sectionIndex, int headerType) {
        Section currentSection = (Section) sectionsList.get(sectionIndex);
        HeaderViewHolder hvh = (HeaderViewHolder) viewHolder;
        hvh.listHeaderTextView.setText(currentSection.categoryName);
    }

    @Override
    public void onBindItemViewHolder(SectioningAdapter.ItemViewHolder viewHolder, int sectionIndex, int itemIndex, int itemType) {
        ItemViewHolder ivh = (ItemViewHolder) viewHolder;

        Section currentSection = sectionsList.get(sectionIndex);
        ArrayList<ProductStockObj> productsList = currentSection.productsList;
        ProductStockObj productStockObj = productsList.get(itemIndex);
        int categoryType = productStockObj.getCategoryType();
        switch (categoryType) {
            case 1:
                ivh.productImageView.setImageResource(R.drawable.ic_bread_circle);
                break;
            case 2:
                ivh.productImageView.setImageResource(R.drawable.ic_buns_circle);
                break;
            case 3:
                ivh.productImageView.setImageResource(R.drawable.ic_cookies_circle);
                break;
        }

        ivh.productTextView.setText("" + productStockObj.getProductName());
        ivh.stockTextView.setText("" + productStockObj.getTotalStockCount());
        ivh.salesTextView.setText("" + productStockObj.getTotalSoldCount());

    }

}