在可滚动的 GridView 中了解 Cardview 中的解决方法
Understanding Workaround in Cardview in a scrollable GridView
我刚开始使用 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 来 "something wired" 处理代码。
您能否向我解释一下为什么这有效或适当的修复使其正常工作?
这是我的代码:
<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 个卡片视图。我在例子中只放了两个。它可能与 android:weightsum 有关,因为我玩过它。
谨致问候
CG
P.S。我使用 TextView 来显示图像,因为我想我也可以在图像上使用文本。现在我没有这样做,所以我可以回到 ImageView。
P.S.S.我在另一个线程中读到,不应将 Gridview 与 Scrollview 一起使用,但也许这就是问题所在。但是从我的知识角度来看没有其他方法可以做到这一点,所以我一直在使用它。
如果您有多个具有相同布局的 CardView,请查看 RecyclerView 上的教程,它将减少您的代码并提高您的应用程序速度。
您所要做的就是编写一个 CardView XML 并使用适配器在您的 RecyclerView 中调用它,然后您可以使用 ArrayList 或 A 数据库填充数据
所以这个话题到此结束我想写下我发现的东西。
在顶部的代码中,我使用了 "weightsum" 错误,因此我不得不使用 gridview 来解决此问题。显然不是正确的做法。
看到现在看到的评论和教程我推荐用Recyclerview来解决这个问题
我刚开始使用 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 来 "something wired" 处理代码。 您能否向我解释一下为什么这有效或适当的修复使其正常工作?
这是我的代码:
<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 个卡片视图。我在例子中只放了两个。它可能与 android:weightsum 有关,因为我玩过它。
谨致问候 CG
P.S。我使用 TextView 来显示图像,因为我想我也可以在图像上使用文本。现在我没有这样做,所以我可以回到 ImageView。
P.S.S.我在另一个线程中读到,不应将 Gridview 与 Scrollview 一起使用,但也许这就是问题所在。但是从我的知识角度来看没有其他方法可以做到这一点,所以我一直在使用它。
如果您有多个具有相同布局的 CardView,请查看 RecyclerView 上的教程,它将减少您的代码并提高您的应用程序速度。
您所要做的就是编写一个 CardView XML 并使用适配器在您的 RecyclerView 中调用它,然后您可以使用 ArrayList 或 A 数据库填充数据
所以这个话题到此结束我想写下我发现的东西。
在顶部的代码中,我使用了 "weightsum" 错误,因此我不得不使用 gridview 来解决此问题。显然不是正确的做法。
看到现在看到的评论和教程我推荐用Recyclerview来解决这个问题