【发布时间】:2019-03-08 05:55:41
【问题描述】:
我刚开始使用 Android Studio,我正在尝试创建我自己的第一个应用程序。 为了抢占先机,我结合了一些我喜欢的教程,结果遇到了一个问题。
在我的 main_activity 中,我在可滚动的 GridView 中使用 CardView。 因为我的 CardView 中有 40 多张卡片,所以我有很多帧需要滚动。
问题出现在这么多卡片上,因为它们有时无法正确显示。我找到了一种解决方法,方法是在我使用的 Gridview (maingrid) 下设置另一个 Gridview (比如说 maingrid2),它甚至在我的屏幕上都没有可视化。
使用解决方法,它看起来像这样: Gridview working, but has workaround
如果我删除我的解决方法 [delete maingrid2],它看起来像这样: Gridview stretched all the way, for some reason.
所以基本上我需要另一个 GridView 来对代码进行“某些连接”。 您能否向我解释一下为什么这样可以正常工作或进行适当的修复以使其正常工作?
这是我的代码:
<ScrollView 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="edmt.dev.androidgridlayout.MainActivity"
android:fillViewport="true">
<!--android:background="@drawable/bg"-->
<LinearLayout
android:orientation="vertical"
android:fillViewport="true"
android:weightSum="10"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<GridLayout
android:id="@+id/mainGrid"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9.5"
android:alignmentMode="alignMargins"
android:columnCount="3"
android:rowCount="2"
android:columnOrderPreserved="false"
android:padding="0dp"
android:fillViewport="true"
>
<!--Cardviews-->
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="175dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
app:cardBackgroundColor="@color/cardview_dark_background"
app:cardElevation="5dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="1dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="@android:color/black"
android:background="@drawable/enchantress"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="175dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
app:cardBackgroundColor="@color/cardview_dark_background"
app:cardElevation="5dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="1dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="@android:color/black"
android:background="@drawable/enchantress"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- have around 40 more Cardviews like that -->
</GridLayout>
<GridLayout
android:id="@+id/maingrid2>
<!-- [....] -->
<!-- exact same as above!-->
</GridLayout>
</LinearLayout>
</ScrollView>
代码显然没有包含所有 40 个 Cardview。我在例子中只放了两个。它可能与 android:weightsum 有关,因为我玩过它。
致以最诚挚的问候 CG
附:我使用 TextView 来显示图像,因为我认为我也可以在图像上使用文本。现在我不这样做,所以我可以回到 ImageView。
附言我在另一个线程中读到,不应将 Gridview 与 Scrollview 一起使用,但也许这就是问题所在。但是从我的知识的角度来看,没有其他方法可以做到这一点,因此我一直使用它。
【问题讨论】:
-
这不是您使用列表和网格的方式。使用适配器并使用适配器将列表膨胀到 gridview
-
是的,您不应该将滚动视图与 gridview 一起使用。 Gridview有自己的滚动机制,所以不需要放入scrollview
-
你能告诉我更多关于你所说的适配器类型的信息吗?我看过一个关于它的教程,但我还没有看到改进。
-
我发现我可以在没有 layout_weights 的情况下删除我的 gridview。所以我要这样做。无论如何,已经谢谢你了,先生!解释为什么我应该使用适配器会很好:)
-
适配器充当视图和数据之间的链接。当您没有固定的数据量时,通常会使用适配器,例如。画廊图像。图片可以随时增加或减少,在这种情况下您不能有固定的行数和列数。
标签: android android-studio gridview scrollview cardview