【问题标题】:How to make image view scroll together with grid view in scroll view如何在滚动视图中使图像视图与网格视图一起滚动
【发布时间】:2020-06-17 12:20:26
【问题描述】:

我在网格视图上方有一些图像视图和文本视图,用于显示客户的评论/cmets。图像视图用于产品说明,文本视图用于产品描述。 当我滚动网格视图时,只有网格视图部分被滚动。我怎样才能使图像和文字也向上滚动。 click here to view the image

<?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="match_parent"
>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/choseitem_image"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="@android:color/black"
        android:id="@+id/choseitem_title"
        />
 <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal">
        <TextView
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:textColor="@android:color/holo_red_light"
            android:id="@+id/choseitem_price"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="15sp"
            android:id="@+id/score"
            android:textColor="@android:color/black"
            />

 </LinearLayout>
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="15sp"
    android:id="@+id/description"
    android:textColor="@android:color/black"
    />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:paddingLeft="20dp"
        android:text="Comment"
        android:textColor="@android:color/black"
        />

    <GridView
        android:id="@+id/comment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    </GridView>

</LinearLayout>

【问题讨论】:

    标签: android gridview imageview scrollview android-scrollview


    【解决方案1】:

    您可以使用 NestedScrollView 包装所有视图以使其滚动。

        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    <!--        put your content layout here-->
        </androidx.core.widget.NestedScrollView>
    

    你的布局应该是这样的

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
            <ImageView
                android:id="@+id/choseitem_image"
                android:layout_width="match_parent"
                android:layout_height="250dp" />
    
            <TextView
                android:id="@+id/choseitem_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@android:color/black"
                android:textSize="20sp" />
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
    
                <TextView
                    android:id="@+id/choseitem_price"
                    android:layout_width="250dp"
                    android:layout_height="wrap_content"
                    android:textColor="@android:color/holo_red_light"
                    android:textSize="20sp" />
    
                <TextView
                    android:id="@+id/score"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="@android:color/black"
                    android:textSize="15sp" />
    
            </LinearLayout>
    
            <TextView
                android:id="@+id/description"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@android:color/black"
                android:textSize="15sp" />
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="20dp"
                android:text="Comment"
                android:textColor="@android:color/black"
                android:textSize="15sp" />
    
            <GridView
                android:id="@+id/comment"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
    
            </GridView>
    
        </LinearLayout>
    </androidx.core.widget.NestedScrollView>
    

    【讨论】:

      【解决方案2】:

      是的,正如 Nguyen 所说,将您的整个 XML 代码封装在嵌套滚动视图中是可行的,他甚至慷慨地帮助您提供了最终代码。

      也就是说,我想尝试详细说明一下为什么会这样。 目前您的 GridView 滚动主要是因为它是 GridView 布局的属性。从文档中可以看出 “在二维滚动网格中显示项目的视图。网格中的项目来自与此视图关联的 ListAdapter。” https://developer.android.com/reference/android/widget/GridView

      要使任何其他组件可滚动,您需要一个 ScrollView,它可以使该 View 垂直滚动,如果您想要水平滚动视图,您可以选择添加 Horizo​​ntalScrollView 。

      但是对于您的特定用例,您需要添加一个 NestedScrollView,因为您有多个视图,而不仅仅是一个子视图。正如定义的那样,“NestedScrollView 就像 ScrollView,但它支持在新旧版本的 Android 上充当嵌套滚动父项和子项。默认情况下启用嵌套滚动。”

      https://developer.android.com/reference/androidx/core/widget/NestedScrollView

      希望这可以帮助您更好地回答解决方案,而不仅仅是使用给出的答案!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-20
        • 1970-01-01
        • 2017-08-02
        • 2017-07-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多