代码之家  ›  专栏  ›  技术社区  ›  mrmeaaan

了解可滚动网格视图中cardview的解决方法

  •  0
  • mrmeaaan  · 技术社区  · 7 年前

    我刚开始使用Android Studio,我正在尝试创建自己的第一个应用程序。 为了得到一个头巾,我结合了一些我喜欢的教程,最后遇到了一个问题。

    在我的主要活动中,我使用 可滚动网格视图中的cardView。 因为我的卡视图中有40张以上的卡,所以我有很多帧可以滚动浏览。

    这一问题伴随着这些卡片,因为它们有时无法正确地可视化。我找到了一个解决方法,在我使用的网格视图(maingrid)下设置另一个网格视图(比如maingrid2),它甚至在我的屏幕上都不可见。

    在解决方案方面,它看起来是这样的: Gridview working, but has workaround

    如果我删除了我的工作区[删除maingrid2],它看起来是这样的: Gridview stretched all the way, for some reason .

    所以基本上,我需要另一个网格视图来处理与代码相关的“连线”。 你能给我解释一下为什么这个能正常工作,或是一个合适的修复方法吗?

    这是我的代码:

    <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

    另外,我使用文本视图来显示图像,因为我认为我也可以在图像上使用文本。现在我没有这样做,所以我可以回到ImageView。

    另外,我在另一个线程中读到,一个人不应该使用带滚动视图的GridView,但是,也许这就是问题所在。但从我的知识观点来看,没有其他方法可以做到这一点,因此我一直在使用它。

    2 回复  |  直到 7 年前
        1
  •  0
  •   Rizwan Ahmed Shivalli    7 年前

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

    您所要做的就是编写一个cardview xml代码,并使用适配器在recyclerview中调用它,您可以使用arraylist或数据库来填充数据。

        2
  •  0
  •   mrmeaaan    7 年前

    所以这就总结了我想写下我发现的线索。

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

    考虑到我现在看到的评论和教程,我建议对此使用RecyclerView。