简体   繁体   中英

Adding views to the layout programatically (and inflating) is very slow

I have RecyclerView which contains multiple Views (around 30) where each of them is page/height long.

Each page inside RecyclerView contains views that can be also scrolled horizontally (around 20 views in 1st horizontal scroll view) and (around 10 views in 2nd horizontal scroll view).

Which means that each RecyclerView creates around 30 views (and inflates them) every time view is created. This is very, very slow.

     for(int i = 0; i < 10; i++) {
            View details = LayoutInflater.from(parent.getContext()).inflate(R.layout.details, parent, false);
            viewHolder.container.addView(details);
        }

I have tried multiple things such as nested RecyclerViews but it's equally slow. Is there a good solution to create multiple views for each page without getting hit by performance (mostly seen when scrolling vertically and only for a second but still very annoying).

So far my best solution is RecyclerView + nested RecyclerView for each page and Horizontall Scroll View which creates less layouts (just 10 instead of 20 and they are simple).

Thanks

Try only getting the LayoutInflater once outside the loop and letting it add to your container view. (this assumes parent and viewHolder.container are the same)

    LayoutInflater inflator = LayoutInflater.from(parent.getContext())
    for(int i = 0; i < 10; i++) {
        View details = inflator.inflate(R.layout.details, viewHolder.container, true);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM