luizgrp / SectionedRecyclerViewAdapter

An Adapter that allows a RecyclerView to be split into Sections with headers and/or footers. Each Section can have its state controlled individually.
MIT License
1.68k stars 372 forks source link

Scrolling is jerky. #10

Closed momersaleem-sdsol closed 8 years ago

momersaleem-sdsol commented 8 years ago

I have added 4 sections and each section has around 4 to 5 items. When I scroll (up and down) list, it jerks. How can I make it scroll smoothly.

luizgrp commented 8 years ago

Could you please paste your code and the xml layouts?

momersaleem-sdsol commented 8 years ago

I have pasted java code here but xml files were not pasting correctly so i have added their content in txt file and attached it.

// section class public class CategorySection extends StatelessSection {

private OnCategoryItemSelectionListener onCategoryItemSelectionListener = null;

Context context = null;
private SectionedRecyclerViewAdapter sectionAdapter = null;
String title;
String description;
List<Item> itemList;

public CategorySection(Context context, SectionedRecyclerViewAdapter adapter,
                       String title, String description, List<Item> list) {
    super(R.layout.item_section_menu_header, R.layout.item_section_menu_item);

    this.context = context;
    sectionAdapter = adapter;
    this.title = title;
    this.description = description;
    this.itemList = list;
}

@Override
public int getContentItemsTotal() {
    return itemList.size();
}

@Override
public RecyclerView.ViewHolder getItemViewHolder(View view) {
    return new ItemViewHolder(view);
}

@Override
public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
    final ItemViewHolder itemHolder = (ItemViewHolder) holder;

    String price = itemList.get(position).getPrice();
    String imageUrl = itemList.get(position).getImage();
    String name = itemList.get(position).getName();

    if (!Validation.isEmptyOrNull(price))
        itemHolder.tvPackagePrice.setText(price);

    if (!Validation.isEmptyOrNull(imageUrl)) {
        Glide.with(context)
                .load(imageUrl)
                .placeholder(R.drawable.placeholder)
                .into(itemHolder.ivPackageImage);
    }

    if (!Validation.isEmptyOrNull(name))
        itemHolder.tvPackageName.setText(name);

    itemHolder.rootView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (onCategoryItemSelectionListener != null)
                onCategoryItemSelectionListener.onCategoryItemSelected(v,
                        sectionAdapter.getSectionPosition(itemHolder.getAdapterPosition()),
                        title);
        }
    });
}

@Override
public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
    return new HeaderViewHolder(view);
}

@Override
public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
    HeaderViewHolder headerHolder = (HeaderViewHolder) holder;

    headerHolder.tvMenuName.setText(title);

    if (!Validation.isEmptyOrNull(description)) {
        headerHolder.tvMenuDescription.setVisibility(View.VISIBLE);
        headerHolder.tvMenuDescription.setText(description);
    } else {
        headerHolder.tvMenuDescription.setVisibility(View.GONE);
    }
}

public interface OnCategoryItemSelectionListener {
    void onCategoryItemSelected(View view, int position, String categoryTitle);
}

public OnCategoryItemSelectionListener getOnCategoryItemSelectionListener() {
    return onCategoryItemSelectionListener;
}

public void setOnCategoryItemSelectionListener(OnCategoryItemSelectionListener onCategoryItemSelectionListener) {
    this.onCategoryItemSelectionListener = onCategoryItemSelectionListener;
}

public void addItems(List<Item> itemList) {
    if (this.itemList != null)
        this.itemList.addAll(itemList);
}

}

// function for adding sections private void initializeAdapterView() {

    SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();

    for (final MenuItem menuItem : menuItemList) {

        CategorySection categorySection = new CategorySection(getLocalContext(),
                sectionAdapter,
                menuItem.getMenuName(),
                menuItem.getMenuDescription(),
                menuItem.getItems());

        sectionAdapter.addSection(categorySection);
    }

    final 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;
            }
        }
    });

    rvMenuItems.setLayoutManager(glm);
    rvMenuItems.setAdapter(sectionAdapter);

}

XML files content.txt

luizgrp commented 8 years ago

Could you please replace all your com.sdsol.corporatecaterers.views.LatoRegularTextView components by a normal TextView and test the scroll?

momersaleem-sdsol commented 8 years ago

It worked. Thanks. But custom textview "LatoRegularTextView" was created to add font style text views. Now how could I add font style on normal text views? Your help will be greatly appreciated.

luizgrp commented 8 years ago

Glad to hear that it worked. Please ask your question in stackoverflow.com and more people will be able to help you.