I am creating a linear layout programmatically but for some reason I am only able to see the first view. I can see that the space for the other views is being created and that this space is being created correctly. For example it has vertical orientation and holds 6 views. It is creating the space needed for the 6 views, and doing it vertically, but only showing the very first one.
I have seen a lot of answers for this but am unable to get it working. I have tried calling invalidate and postInvalidate on the linear layout and the parent layout but am not getting a refresh. I made sure the code is running on my Main UI thread. I have removed the view right before and then added it but to no avail. I am stuck on this one and can't seem to figure out where my linear layout is going wrong.
Here is some code showing how I've done this before and how I first tried:
for (int i = 0; i < pdpComparisonAdapter.getCount(); i++) {
final View item = pdpComparisonAdapter.getView(i, null, null);
pdpComparisonsLinearLayout.addView(item);
}
Here is the Linear Layout xml:
<LinearLayout
android:layout_below="@+id/comparisons_title"
android:id="@+id/comparisons_linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#ffffff"
android:orientation="vertical"/>
Here is the view layout that I am trying to add:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:src="@drawable/notable_amenity_star"
android:layout_marginLeft="10dp"
android:layout_alignParentLeft="true"
android:id="@+id/comparison_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_toRightOf="@+id/comparison_iv"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/comparison_value_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/comparison_label_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"/>
<TextView
android:id="@+id/comparison_explanation_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
I have also tried working the java code differently like these:
for (int i = 0; i < pdpComparisonAdapter.getCount(); i++) {
final View item = pdpComparisonAdapter.getView(i, null, null);
item.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
View oldView = pdpComparisonsLinearLayout.getChildAt(i);
pdpComparisonsLinearLayout.removeView(oldView);
pdpComparisonsLinearLayout.addView(item,i);
pdpComparisonsLinearLayout.getChildAt(i).setVisibility(View.VISIBLE);
}
pdpComparisonsLinearLayout.invalidate();