Setting a background resource for many linearlayouts in scrollview causes lag - android

I am dynamically adding around 150 linearlayouts to a scrollview in a grid-like layout. If I set the background resource to a drawable for each of them using setBackgroundResource(R.drawable.x), the scrollview shows extremely noticeable lag and choppiness, even though the drawable is a simple colour and border.
If I remove the call to setBackgroundResource, the scrollview is smooth again.
Is this expected to happen with so many views containing backgrounds? If so, how would I go about making a grid with custom backgrounds for each cell?

You're going to want to use a list view in your scroll, and you're going to want to use a ListAdapater:
http://developer.android.com/guide/topics/ui/layout/listview.html
http://developer.android.com/reference/android/widget/Adapter.html
Basically what's going on is you're loading a large number of images into memory, and the scroll view by default doesn't do a very good job of managing releasing and inflating these resources.
Using methods similar to the above, with some custom image management, I've managed to get thousands of views running smoothly on a scroll.

It seems like you're trying to create your own list view implementation so that you can set your own layouts for each row. I don't recommend doing this. Instead, use the default list view implementation provided by Android and instead of setting a default ArrayAdapter instance on the list view, subclass ArrayAdapter, override the getView method, and return your custom layout.
I highly recommend you check out this tutorial for a more thorough explanation:
http://www.ezzylearning.com/tutorial.aspx?tid=1763429

Related

How to make evenly spaced Grid Layout with dynamically added square items

I would like to create a grid layout in which each item is taking as much space as possible (minus padding), but only as long as there are columns available (after that the next item would be inserted in the next row while keeping the size). Additionally, each item must be a square and is added dynamically.
Example layout with 10 items would be as follows:
I have tried to achieve this by setting weights, ratio constraints, overriding onMeasure - but I just can't get it to work. I would be happy with either a programmatic or an XML-based solution (as long as each item can be added programmatically). I would prefer the solution to be in Kotlin, but I would be happy with a Java-based one as well.
It's probably worth saying that each item in the grid layout is a layout (RelativeLayout as of now) to make inflating it and setting a layered background drawable programmatically easy.
I think you might be able achieve what you want with a different Layout
Have a look at https://github.com/google/flexbox-layout it has lots of methods to control how the cells grown or shrink and includes automatic or manual wrapping of cells.
Take a look at RecyclerView. You would need to pass GridLayoutManager. This tutorial may or may not help you. For square items, I suggest using CardView but it's not necessary. If you are targeting tablets as well as smartphones, check this out. And for dynamically adding new items, you should notify recyclerView's adapter. See this link. You can also extend RecyclerView or GridLayoutManager for more control over items.

A recycler view vs the linear layout inside a scroll view?

Am trying to create a page with a scrollable list. Features would be a normal list to remove item by clicking on it. Number of items in that list are limited and added dynamically by user. You can consider a to do list as example. Now which would be a better approach to implement it? Recycler view with data bound to its adapter? Or the normal linear layout with items added as children at run time?
My current implementation is recycler view. But,I found it lagging and animations are not performing well. So a linear layout is auto animated by specifying it xml -- by setting animate layout changes to true.
FYI data is local and syncs in background.
Never use a LinearLayout for anything longer than a single screen. The whole point of ListView and RecyclerView is to efficiently reuse views instead of needing to hold things in memory when they're not visible. Maybe you can refine or reask your question so people can help you with whatever difficulty you're having with animations, rather than avoiding the issue.

Merging multiple views into a custom view

I could use some help with merging multiple views into one, like Pinterest has at
I know how to make a reusable custom view, been searching around custom components, compound controls, checked sdk samples and threads like Custom view made of multiple views , but it was never the proper way. I want it to also show 1 view (where i can set the image/texts dynamically) at the hierarchy viewer instead of 5+ child subviews, as that way increases performance aswell. I checked the github acc of Pinterest too without success :)
Can I somehow achieve it, or Im way too off? Thanks!
Try creating a separate layout.xml file containing all of the views you want to have in your custom view. Then in java all you would have to do is make reference to each of those custom views and fill them with your data.

Android listview updating too many times

In my main-activity layout (RelativeLayout), I display three listviews of vertical orientation among other Buttons and Textviews. My problem is that when one of the listviews (the one on the left side of the screen) updates, the other two update too, causing poor UI performance.
I understand that RelativeLayout can be tricky in cases when the size of one view affects the positioning of the other (causing the other to redraw), so I have made sure that the positioning of the two listviews is not associated with the left listview's size.
I have also checked out tips on how to make listview redrawing more efficient, using ViewHolders etc, but I'd rather resolve this problem to it's core.
More details:
For the left listview's adapter I subclass the ArrayAdapter class.
For the other two listviews' adapters I use subclass the CursorAdapter class.
You are right RelativeLayout in the culprit, use some other view in plase of it, like LinearLayout with android:weightSum etc...
If you set android:layout_height="wrap_content" to your ListView, then system try to find the best size of each elements and call getView many times. There is no way to around that other than using android:layout_height="fill_parent"
http://www.androiddevelopersolutions.com/2013/07/android-listview-adapter-getview-called.html
custom listview adapter getView method being called multiple times, and in no coherent order
Try to use same adapter class for the better performance because the execution time differs on what sort of data your are going to display.

ListView or recycle views myself?

I have an app which loads a boatload of images and displays them in a TableLayout which is inside a ScrollView. At run time I get the width of the layout parent and use that to determine how many images can go in each TableRow (all of the images are of a set size).
I'm concerned about memory issues when loading more and more images. I know ListView recycles its views but I don't know how to dynamically change number of views in each item. I am only aware of inflating XML which isn't going to change the number of views per item at run time.
So my question is what is easier - figuring out how to recycle views in my table by myself, or making a list's items change based on screen size? Just a link to a tutorial on how to do whichever is easier is good enough an answer for me.
I suggest you to use ListView with the ViewHolder approach (you can see it here: How to load the Listview "smoothly" in android).
The ListView, when scrolled, removes the views that are no more visible and gets the views that are about to become visible. This way, it's better than using a ScrollView and a TableLayout.

Categories

Resources