如何使 GridView 在 Kotlin 中包装其所有 child 视图
How to make GridView wrap all its child views in Kotlin
我想以网格形式对我的卡片布局进行动态分组,但我不确定是否要使用 GridView
或 GridLayout
来这样做。
由于 child 视图是动态添加的,因此我不知道行数或列数,所以我不能选择 GridLayout
但是如果我实现 GridView
那么内容就会滚动我不想这样。
[我想要网格视图两个包裹所有 child 而不显示任何滚动]
所以,要么我应该禁用滚动 GridView
来包装它的内容,要么我缺少其他地方。
我尝试使用自定义视图包装 child 视图。但即使这样也不能解决问题。
这是自定义视图class:
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)
}
}
所以我想知道如何将 child 视图包装在 gridView
中?
另外,我应该使用 GridView
, GridLayout
还是其他东西?
尝试回收器视图后更新代码:
<?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的解决方案,但仍然没有结果。
你可以试试这个库:FitGridView
否则你可以使用 Gridlayout,或者 LinearLayout 的组合,但是你需要大量的计算,这取决于你想要的卡片的最小和最大尺寸,每行每行的卡片数量。
我解决了这个问题。由于 布局没有 ScrollView
.
,因此 gridview 无法包装其子项
我是网页设计出身,网页的基本属性是根据内容滚动,但在android中我们必须添加ScrollView
来制作一个Activity滚动。
刚刚添加了一个 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 的更新版本,因此最好使用它。
我想以网格形式对我的卡片布局进行动态分组,但我不确定是否要使用 GridView
或 GridLayout
来这样做。
由于 child 视图是动态添加的,因此我不知道行数或列数,所以我不能选择 GridLayout
但是如果我实现 GridView
那么内容就会滚动我不想这样。
[我想要网格视图两个包裹所有 child 而不显示任何滚动]
所以,要么我应该禁用滚动 GridView
来包装它的内容,要么我缺少其他地方。
我尝试使用自定义视图包装 child 视图。但即使这样也不能解决问题。
这是自定义视图class:
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)
}
}
所以我想知道如何将 child 视图包装在 gridView
中?
另外,我应该使用 GridView
, GridLayout
还是其他东西?
尝试回收器视图后更新代码:
<?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的解决方案,但仍然没有结果。
你可以试试这个库:FitGridView
否则你可以使用 Gridlayout,或者 LinearLayout 的组合,但是你需要大量的计算,这取决于你想要的卡片的最小和最大尺寸,每行每行的卡片数量。
我解决了这个问题。由于 布局没有 ScrollView
.
我是网页设计出身,网页的基本属性是根据内容滚动,但在android中我们必须添加ScrollView
来制作一个Activity滚动。
刚刚添加了一个 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 的更新版本,因此最好使用它。