Dynamically allocating height of for list view - android

I have one list view and one expandable list view and i want to show both on same view (I can fix the height of list view but that will scroll my list inside that defined height only).
i have these below link so far but by using this method it allocation blank space although it is allocating dynamic height.
http://kk-brothers.blogspot.in/2011/09/dynamically-change-listview-height.html

Please check padding/margin also get place in calculation. Conversion of dp into px also required. But I will request not to design like this. It will destroy recycling property of your listview/recyclerview.

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

Issue with custom views in Relative Layout

I have multiple custom views rendered on my screen in RelativeLayout using LayoutParams. Now first time user touches on view it increase it's size, after that when user once again clicks on that view, that view should get disappear. Now the problem is when a view gets disappeared other views on screen changes their size and location. By debugging I found that this happens when I pass dynamic values as width and height in LayoutParams. If I go for static then all works good. But then I'm not able to increase the size of view. How can I solve this? I also tried changing View.GONE to View.INVISIBLE but that also doesn't help.
I create all views in code and place them on screen. In my xml I don not have any view entry. I do this because initial size of views should be random. So I create view and assign him initial size in LayoutParams(width, height) and then use topMargin and leftMargin to place them at various positions on screen. More details can be found here: Custom object click issue in android
Best guess without seeing any code... It sounds like you are using layout_weight, and if a view is removed, the other views with a layout_weight will expand to fill the now available space.

Android: settings all items to the same layout height in a ListView

I have a list view which is rendered each time with a different list of items, with variable height.
What I want is to set all the Views in the list view to the same layout height according to the view with the highest layout_height, when setting it to layout_height="wrap_content" for each View.
Also I would like to apply min and max values for the height.
So if I define min=30dp,max=100dp, and the biggest View is automatically rendered with 70dp
all Views in the ListView should be set to 70dp.
I have no idea how to go about it, expect for calculating in the code the max view height values, and setting them to all views, but this doesn't seem very elegant to me, especially as I need to translate it to DPs in the code.
Any simpler ideas?
Your right what you try to achieve wont be as pretty as the regular way, but it'll work ;) usually you would decide height beforehand and then go with it :)

How can I find the measured height of views?

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.

Categories

Resources