【问题标题】:How to make GridView wrap all its child views in Kotlin如何使 GridView 在 Kotlin 中包装其所有子视图
【发布时间】:2019-06-21 15:40:00
【问题描述】:

我想以网格形式动态地对卡片布局进行分组,但我不确定使用 GridViewGridLayout 这样做。 由于子视图是动态添加的,因此我不知道行数或列数,所以我不能选择GridLayout,但如果我实现GridView,那么内容会滚动,我不希望它滚动。

[我想要两个网格视图包裹所有的孩子而不显示任何滚动]

所以,要么我应该禁用 GridView 的滚动以包装其内容,要么我缺少其他地方。

我尝试使用自定义视图包装子视图。但即使这样也不能解决问题。

这是自定义视图类:

package ...

import ...

class WrappingGridView : GridView {

    constructor(context: Context) : super(context) {}

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}

    constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {}

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        var heightSpec = heightMeasureSpec
        if (layoutParams.height == LayoutParams.WRAP_CONTENT) {
            heightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE shr 2, MeasureSpec.AT_MOST)
        }
        super.onMeasure(widthMeasureSpec, heightSpec)
    }

}

所以我想知道如何将子视图包装在gridView 中? 另外,我应该使用 GridViewGridLayout 还是其他?

尝试回收站视图后更新的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              tools:context=".ShopFragment"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:background="@android:color/white">

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

        <TextView android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:textSize="20sp"
                  android:padding="2dp"
                  android:layout_marginLeft="5dp"
                  android:layout_marginStart="5dp"
                  style="@style/TextAppearance.AppCompat.Title"
                  android:text="Trending Tastes"
                  android:textColor="@color/colorPrimaryDark"
                  android:drawableLeft="@drawable/ic_whatshot_gradient_900_24dp"
                  android:drawableStart="@drawable/ic_whatshot_gradient_900_24dp"
                  android:drawablePadding="5dp"/>

        <HorizontalScrollView android:layout_width="match_parent"
                              android:scrollbars="none"
                              android:id="@+id/circularCardCollection"
                              android:layout_height="wrap_content">

            <LinearLayout android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:orientation="horizontal">
                <include layout="@layout/card_small_layout"/>
                <include layout="@layout/card_small_layout"/>
                <include layout="@layout/card_small_layout"/>
                <include layout="@layout/card_small_layout"/>
            </LinearLayout>
        </HorizontalScrollView>
    </LinearLayout>
    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
                  android:orientation="vertical">
        <TextView android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:textSize="20sp"
                  android:padding="2dp"
                  android:layout_marginLeft="5dp"
                  android:layout_marginStart="5dp"
                  style="@style/TextAppearance.AppCompat.Title"
                  android:text=" @Treat -Must Try"
                  android:textColor="@color/colorPrimaryDark"
                  android:drawableLeft="@drawable/ic_restaurant_gradient_24dp"
                  android:drawableStart="@drawable/ic_restaurant_gradient_24dp"
                  android:drawablePadding="5dp"/>
<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content">
    <android.support.v7.widget.RecyclerView android:layout_width="match_parent"
                                            android:layout_height="wrap_content"
                                            android:padding="10dp"
                                            android:horizontalSpacing="5dp"
                                            android:verticalSpacing="10dp"
                                            android:gravity="center"
                                            android:id="@+id/grid"/>
</RelativeLayout>


    </LinearLayout>
</LinearLayout>

在主片段中:

   gridview.layoutManager = GridLayoutManager(view.context,2,LinearLayoutManager.VERTICAL,false)

这并不能解决问题。看了Stack Question的解决方案,还是没有结果。

【问题讨论】:

  • @NJ 你可以提供解释链接或阅读有关recyclerview 的内容。请不要对新成员无礼。先生,您可以点击此链接查看 recyclerview developer.android.com/guide/topics/ui/layout/recyclerview
  • @Ashish 我的问题有答案。没有什么可粗鲁的。是你这样想的。
  • 好的,我试试 Recycler View
  • @Ashish 我实现了 Recycler 视图,但问题同样是子内容没有包装在 Recycler 视图中。查看我的更新代码

标签: android gridview kotlin grid adapter


【解决方案1】:

你可以试试这个库:FitGridView

否则,您可以使用 Gridlayout 或 LinearLayout 的组合,但您需要进行大量计算,具体取决于所需卡片的最小和最大尺寸,以及按行和行的卡片数量。

【讨论】:

  • 这需要大量计算,所以我尝试使用RecyclerView,但即使这样也行不通。如果您能提供帮助,请查看更新后的问题?
【解决方案2】:

我解决了这个问题。网格视图无法包裹其子视图,因为布局没有ScrollView

我来自网页设计背景,网页的基本属性是根据其内容滚动,但在 android 中我们必须添加 ScrollView 来制作 Activity Scroll。

刚刚添加了一个ScrollView 作为最顶部的元素,它就像一个魅力!

<ScrollView  xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              tools:context=".ShopFragment"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
        //Other Views

<GridView  android:layout_width="match_parent"
              android:layout_height="wrap_content"/>
</ScrollView>

RecyclerView 也是 GridView 的更新版本,所以最好使用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-06
    • 2014-08-10
    • 2018-03-04
    • 1970-01-01
    • 2021-10-05
    相关资源
    最近更新 更多