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

Is this an optimized way to host a custom draw View in Section? #96

Closed yccheok closed 3 years ago

yccheok commented 6 years ago

Quite some time, I saw some "Slow rendering" warning from my Android Developer Console's Android Vital. I'm not exactly sure whether it is related to this issues. But, I would like to get some additional information.

I have a page, which is holding a single RecyclerView. The RecyclerView is hosting around 10 different Section.

Each Section, is holding a single custom draw chart (inherited from View) . Every Section is having getContentItemsTotal equal to 1.

Every Section, the code template looks as following

@Override
public int getContentItemsTotal() {
    return 1;
}

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

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

private class ItemViewHolder extends RecyclerView.ViewHolder {
    // Deferred creation later for optimization.
    private CombinedChart chart;

    public void show() {
        buildChart();

        // Is this View.invalidate call necessary?
        chart.invalidate();
    }

    private void buildChart() {
        if (this.chart != null) {
            // Early return for optimization. Need not perform same 
            // expensive graphic canvas drawing again and again.
            //
            // This is based on assumption that, getContentItemsTotal is 
            // always equal to 1. No view will be reused?!
            return;
        }

        this.chart = (CombinedChart) this.view.findViewById(R.id.chart);

        // ...
        // ...
        // Code to perform expensive graphic canvas drawing.
        // ...
        // ...

1) I was wondering, is my optimization correct? I would like to avoid expensive same canvas drawing again-and-again. Hence, I try to only perform drawing one time, and return early during rest of the time. Is that OK?

2) Is calling chart.invalidate() each time necessary?

Thanks.