How can I find the measured height of views? - android

I need to calculate the measured height of my views without measuring all views, because these views contain images fetched from the internet. When I measure the views, they download images to memory and I an catch out of memory exception. I use custom ScrollViews, which work like ListViews, display some part of views and when scroll, add to bottom or top.
How can I calculate the height of all my views without throwing out of memory exceptions?
Or how can I make ListView calculate its height before rendering views?

You can use getLeft(), getTop(), getRight() and getBottom() these method's on view. for more information . see following link
http://developer.android.com/reference/android/view/View.html

You answered your own question:
display some part of views and when scroll, add to bottom or top.
ListView does NOT know the heights of items it has not yet seen. It lays out some or all of the items it knows, adds an extra bit of UI at top and bottom to visually indicate that there may be more items, and scrolls that. The total scrollable range for ListView can change as it adds more items.

Related

RecyclerView incorrect manual positioning

I'm trying to align some items inside a RecyclerView with others outside it. However, seems like all my tries are failing.
What I'm trying to do specifically: I have a couple of views as a header which are aligned using various methods* then a RecyclerView just below it, in its same parent, stretching the whole width, that inflates views.
The problem: Items inside the RecyclerView do not align perfectly to the items outside of it. Knowing that I'm using the same layout for both the parent and the items! And knowing that whatever method I used for horizontal alignment for the header items, is exactly used the same way for each item inflated for the RecyclerView. AND knowing that both items have same properties (they're TextViews, same size, width, height, etc...)
What methods have I tried? FOR BOTH SIDES (up and down), I tried the following:
Using ConstraintLayout for both parent and item layouts, and stretching the items between both start and end of parent, then using the constraint ratio to position it exactly where I want. Outcome for header is different from that of RecyclerView holders.
Using a TableLayout with children stretched across the whole width, same number of columns, same stretching, still different outcome, not pixel perfect.
Placing the header views exactly how I want, but not using any Android placement 'methodology' at all, but instead getX() and setX() later inside the adapter (using .post() so I ensure the position is accurate after inflation) and STILL the same wrong placement.
What am I missing? Shouldn't a RecyclerView inflating children that span the whole width when I specify they should match_parent? I tried debugging the X values for header and view holders, THEY'RE THE SAME but my eyes see different things. I delayed it for some milliseconds but this didn't change a thing.
So the problem turned out that whatever method I used, if the TextViews widths were wrap_content it'll always fit the word inside it. The solution was setting the width to 0dp and let the parent ViewGroup balance everything evenly. In my case I used a TableLayout.

ListItem height

I am creating a custom view for my Android app that should replace a huge Listview with custom cells. My cells used a RelativeLayout and layout_height="match_parent"
Now I wonder how the ListView knows how high each cell needs to be because it does obviously not fill the parent. So how does it determine how large the distance between the top and bottom elements of the RelativeLayout is.
Explaination:
If you are just wandering how does the listview know the cell's sizes, it is thanks to her onMeasure() method
As you can read:
Measure the view and its content to determine the measured width and
the measured height. This method is invoked by measure(int, int) and
should be overridden by subclasses to provide accurate and efficient
measurement of their contents.
In shorts, with this and other methods, the ListView can measure the view's sizes and it will resize the view itself based on those measurements.
I hope this helped, if I misunderstood, please let me know

ListView getChildCount not returning correct value when it has children with small height

So i'm trying to hack away an issue I'm having. To reach my goal of not having views i set the height of the view, in the adapter, to 5 pixel. To make sure they're being drawn i also set the background color to a whole different one.
When calling getChildCount in the custom listView It doesn't seem to count the smaller views even though they are clearly visible and on the screen.
I'm wondering if the listview is miscalculating the number of views in the screen due to the difference in height and if so how should i fix it.

Android ListView Scrollable

Is there a way to determine if a list view needs to scroll to display all of its contents?
In other words, are the combined heights of all the children greater than the height of the list view itself.
I have not tried using it this way, but in principle if getLastVisiblePosition() equals getCount()-1, all items should be visible. It's possible that there is still a bit of scrolling needed, less than the height of a row, and I don't know if that matters in your case or not.
If it does, you could always iterate over the visible children and sum their heights.
I tried some ways a while ago and ended with simply calculating and comparing the height of the list view with the sum of the heights of all items and the sum of all separators.

recycling views in scroll view

I have an app that show the tv guide for a list of channels. My UI is made from a a lot of custom views with different widths that show the tv programs, all these custom views are added into a horizontal scrollview that is added into a scrollview so my views can be scroll in 2 dimensions left-right and top-down. It all works good until i add add a lot of views and it starts to slow down very much. So i need a way to recycle views like listiew does in a scrollview maybe there is a custom made scrollview that does this, or someone has an idea how to do this, its strange that scrollview isnt backed up by an adapter like gridview and listview.
I did something similar only my Views were not connected as yours but they were all different sizes.
First you need to define if your entire area (not just viewing screen) has definitive or dynamical number of your custom views.
If you have definitive number of views and their positions you should create their position map with list of Rect's (Rect has a good function whether the xy point belongs). Then you define maximum of Views which are visible on your screen. For this to work without constant loading you should have maximum visible views + at least one line of border views of total objects. After all this you should easily have your own positioning system where you load views which are in bounds of your screen + some overhead (purpose of this is that you want your users to have smooth transition while scrolling at least for some length), if you need to load some in same time you unload (read reuse/ do not dispose objects and create them onScroll events) and place them according to your needs.
And if you want to determine which views should be visible you just go through list and ask whether Views Rect intersects with your Rect of area to be loaded.
Hope this image helps a little bit more
I know it sounds a little bit confusing and difficult to implement but you did not asked a simple question :)
Hope this helps and enjoy your work.
A ScrollView that is backed up by an adapter and recycles views is a ListView, with a couple of extra optional features on top.
Maybe you want a HorizontalScrollView that is backed by an adapter? Searching for HorizontalListView will give you a few results, ex: https://github.com/dinocore1/DevsmartLib-Android.

Categories

Resources