【问题标题】:Understanding Workaround in Cardview in a scrollable GridView了解可滚动 GridView 中 Cardview 中的解决方法
【发布时间】: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


【解决方案1】:

所以这结束了这个线程我想写下我发现的东西。

在顶部的代码中,我错误地使用了“weightsum”,因此我必须使用 gridview 来解决这个问题。显然不是正确的做法。

关于我现在看过的 cmets 和教程,我建议使用 Recyclerview 来解决这个问题。

【讨论】:

  • 您可以添加一条评论,无需不接受我的回答
【解决方案2】:

如果您有多个具有相同布局的 CardView,请查看 RecyclerView 上的教程,它将减少您的代码并提高您的应用程序速度。

您所要做的就是编写一个 CardView XML 并使用适配器在您的 RecyclerView 中调用它,您可以使用 ArrayList 或 A 数据库扩充数据

【讨论】:

  • 我查看了有关 RecyclerView 的教程,一般实现似乎很容易。虽然我做到了,但你能给我一个提示,以便我可以有 3 列。如果我用 3 列进行总体布局,我显然总是有 3 列带有 CardViews。但是当我只想在最后一行显示 1 或 2 个 Cardview 时该怎么办?
  • 参考我最后的推荐,我自己想通了。 RecyclerView 为此提供了一个很好且简单的选择。我推荐这个教程来做我想做的事情link
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-29
  • 1970-01-01
  • 2011-06-18
相关资源
最近更新 更多