Search code examples
androidandroid-layoutandroid-gridviewandroid-scrollview

Unable to scroll LinearLayout in ScrollView


I want to make a layout that display several items, but I cannot make them fully unfold, so I use a ScrollView. However, I find that I can only scroll the first GridView, the last two item can not be scroll up. What I need is to display two GridView fully and all items can be scrolled,but I do not know how to deal with the problem.

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.ifchan.reader.AllClassActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/all_class_tool_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorToolbar"
        app:title="All Class"
        app:titleTextColor="#ffffff"/>

    <ScrollView
        android:id="@+id/all_class_scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/all_class_tool_bar"
        android:fillViewport="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">


            <TextView
                android:id="@+id/all_class_male"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:text="male"
                android:textSize="20dp"/>

            <GridView
                android:id="@+id/all_class_male_grid_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:horizontalSpacing="10dp"
                android:numColumns="auto_fit"
                android:stretchMode="columnWidth"
                android:verticalSpacing="10dp"/>

            <TextView
                android:id="@+id/all_class_female"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:text="female"
                android:textSize="20dp"/>

            <GridView
                android:id="@+id/all_class_female_grid_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:horizontalSpacing="10dp"
                android:numColumns="auto_fit"
                android:stretchMode="columnWidth"
                android:verticalSpacing="10dp"/>


        </LinearLayout>

    </ScrollView>

</RelativeLayout>

Solution

  • Use Recycler View or a non scrollable gridview if you want to use scrollview.
    
        public class NonScrollGridView extends GridView{
    
            public NonScrollGridView (Context context) {
                super(context);
            }
            public NonScrollGridView (Context context, AttributeSet attrs) {
                super(context, attrs);
            }
            public NonScrollGridView (Context context, AttributeSet attrs, int defStyle) {
                super(context, attrs, defStyle);
            }
            @Override
            public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
                int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
                        Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
                super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
                ViewGroup.LayoutParams params = getLayoutParams();
                params.height = getMeasuredHeight();
            }
    
    
    
         <NonScrollGridView 
                        android:id="@+id/all_class_female_grid_view"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:horizontalSpacing="10dp"
                        android:numColumns="auto_fit"
                        android:stretchMode="columnWidth"
                        android:verticalSpacing="10dp"/>