0

I have a list view with two textviews, one of them has its visibility set to View.GONE by default, so the listview does the wrap_content for the minimum size, the size of the listview with all the secondary textviews GONE, when I turn one of them View.VISIBLE (by clicking on the item), the height stays the same height it calculated from the very first wrap_content when it was first viewed. Tried different approaches that I read but none of them worked on my case such as

mListView.invalidate();

mListView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)mListView.getLayoutParams();
params.height = params.WRAP_CONTENT;

is there any way to force the view to recalculate the height wrap_content value after each click?

Edit1:- This is my listview layout (two is the one that is set to GONE)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    <TextView
        android:id="@+id/list_item_one"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Question?"
        android:paddingLeft="4dp"
        android:paddingRight="4dp"
        android:paddingTop="8dp"
        android:paddingBottom="4dp"
        android:textStyle="bold"/>

    <TextView
        android:id="@+id/list_item_two"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Answer!"
        android:layout_below="@id/list_item_one"
        android:paddingLeft="4dp"
        android:paddingRight="4dp"
        android:paddingBottom="4dp"/>

</RelativeLayout>
javanewb
  • 1
  • 3

1 Answers1

3

You should call requestLayout() when you want to re-calculate the dimensions. That is according to this neat view lifecycle:

enter image description here

Original link here.

Simas
  • 43,548
  • 10
  • 88
  • 116
  • Well, i just tried to add a mListView.requestLayout() after the visibility change and still same problem, do I have to override the onMeasure? – javanewb Oct 13 '14 at 21:22
  • @javanewb Are the TextViews part of the ListView item layout? Seeing your layout would help. – Simas Oct 13 '14 at 21:23
  • I've just added the layout on the main post – javanewb Oct 13 '14 at 21:31
  • @javanewb So it's actually a ListView item that contains the hidden TextView? In that case you need to re-measure the item, and there's no easy way for thay. You can update all items by calling `notifyDataSetChanged` on your list's adapter. – Simas Oct 13 '14 at 21:48
  • Well, notifyDataSetChanged seems to negate the visibility changing part somehow if I set the default visibility in the adapter class, but works when I set it in the layout xml file, but still doesn't resize, I will try to look into view remeasuring, but are you sure I should remeasure the items not the the whole listview, because it seems like the items remeasure perfectly, less so for the listview. – javanewb Oct 13 '14 at 21:49
  • @javanewb ListView should stay at the same size. Since you hide TextViews inside a ListView item, the item itself should be re-measured. – Simas Oct 13 '14 at 21:51
  • Link is no longer available. Could you provide a new one? There's a question here (https://stackoverflow.com/questions/60294063/repeatedly-altering-visibility-of-a-child-view-and-measuring-parent-view-causes) pertaining to the lifecycle of a View and seems like it does not follow the diagram you gave. – Richard Feb 20 '20 at 04:14