【问题标题】:Unable to scroll LinearLayout in ScrollView无法在 ScrollView 中滚动 LinearLayout
【发布时间】:2017-11-27 08:38:07
【问题描述】:

我想制作一个显示多个项目的布局,但我无法让它们完全展开,所以我使用了 ScrollView。但是,我发现我只能滚动第一个GridView,最后两项不能向上滚动。 我需要的是完全显示两个GridView,并且所有项目都可以滚动,但我不知道如何处理这个问题。

<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>

【问题讨论】:

    标签: android android-layout android-gridview android-scrollview


    【解决方案1】:
    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"/>
    

    【讨论】:

      【解决方案2】:

      您将 GridView 放入 ScrollView。它不会像您预期的那样工作。

      尝试将GridLayoutManagerRecyclerView 一起使用并将其放入NestedScrollView

      详细解释可以查看this问题。

      如果你仍然想使用 GridView,你可以使用扩展 GridView 的 ExpandableHeightGridView

      【讨论】:

      • 我只想在两个GridView中完整显示所有项目,能不能简单点?
      • @IfChan 更新了我的答案并为您添加了 GridView 选项。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-04
      • 2020-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多