Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horizontal scroll view hiding some children

Android 4.1.0.

I have one horizontal scroll view, inside it - LinearLayout. In LinearLayout I add some children programmatically in fragment onResume. In the case when children fit in the view, everything is fine. In the case when children do not fit in the view, HorizontalView hides first n elements from the left and keep free space for n elements on the right.

I tried to call computeScroll or requestLayout/forceLayout, but it didn't help.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<HorizontalScrollView
    android:id="@+id/productListScroll"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/productListLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:orientation="horizontal"
        android:paddingBottom="10dp"
        android:paddingLeft="10dp"
        android:paddingTop="10dp" />
</HorizontalScrollView>
...
</LinearLayout>

and the code in fragment:

    public void onResume() {
         super.onResume();
         productsPanel.removeAllViews();
         for (Product product : currentOrder.getProductList()) {
             productsPanel.addView(new ...); //view with calculated height and width
             productsPanel.addView(new ...); //view with fixed width and height = MATCH_PARENT
         }
    }
like image 215
ilyavolk Avatar asked Dec 08 '22 18:12

ilyavolk


2 Answers

<HorizontalScrollView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal">
        <LinearLayout android:id="@+id/top_buttons"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <!-- Lots of items -->
        </LinearLayout>
    </HorizontalScrollView>

It was issue. Horizontal scroll view hides its children if you set android:layout_gravity="center_horizontal" for inner container. link is here

like image 139
Meenaxi Avatar answered Jan 05 '23 22:01

Meenaxi


The problem was solved by setting linear layout layout_gravity=left, gravity=center. I also moved view creation code to onActivityCreated, but I don't think it helped to solve this problem.

like image 27
ilyavolk Avatar answered Jan 05 '23 23:01

ilyavolk