【发布时间】:2019-06-21 15:40:00
【问题描述】:
我想以网格形式动态地对卡片布局进行分组,但我不确定使用 GridView 或 GridLayout 这样做。
由于子视图是动态添加的,因此我不知道行数或列数,所以我不能选择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 中?
另外,我应该使用 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的解决方案,还是没有结果。
【问题讨论】:
-
@NJ 你可以提供解释链接或阅读有关
recyclerview的内容。请不要对新成员无礼。先生,您可以点击此链接查看 recyclerview developer.android.com/guide/topics/ui/layout/recyclerview -
@Ashish 我的问题有答案。没有什么可粗鲁的。是你这样想的。
-
好的,我试试 Recycler View
-
@Ashish 我实现了 Recycler 视图,但问题同样是子内容没有包装在 Recycler 视图中。查看我的更新代码
标签: android gridview kotlin grid adapter