java - 如何使用 RecyclerView 创建两列?

标签 java android xml eclipse android-recyclerview

我是 Android 编程的新手。 我创建了一个音板,它看起来像这样: https://gyazo.com/2a08bcd731fb1cfeb07e72f75bc05e7e

但我不明白如何使用 RecyclerView 执行此操作。 这是包含 2 列和只有 3 行的代码(通常有 200 行):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background2"
    android:orientation="vertical"
    android:layout_marginTop="50dp">


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <Button
                android:id="@+id/button1"
                android:layout_weight = "1"
                android:layout_height="100dp"
                android:layout_column="0"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/mybutton"
                android:onClick="Button1"
                android:padding="3dp"
                android:text="Button1"
                android:textColor="#ffffff"
                android:textSize="20dp" />

            <Button
                android:id="@+id/Button2"
                android:layout_weight = "1"
                android:layout_height="100dp"
                android:layout_column="0"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"                       
                android:background="@drawable/mybutton"
                android:onClick="Button2"
                android:padding="5dp"
                android:text="Button2"
                android:textColor="#ffffff"
                android:textSize="20dp" />

            <Button
                android:id="@+id/Button3"
                android:layout_weight = "1"
                android:layout_height="100dp"
                android:layout_column="0"
                android:layout_column="1"
                android:layout_row="1"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/mybutton"
                android:onClick="Button3"
                android:padding="5dp"
                android:text="Button3"
                android:textColor="#ffffff"
                android:textSize="20dp" />

            <Button
                android:id="@+id/Button4"
                android:layout_weight = "1"
                android:layout_height="100dp"
                android:layout_column="0"
                android:layout_column="0"
                android:layout_row="1"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/mybutton"
                android:onClick="Button3"
                android:padding="5dp"
                android:text="Button4"
                android:textColor="#ffffff"
                android:textSize="20dp" />

            <Button
                android:id="@+id/Button5"
                android:layout_weight = "1"
                android:layout_height="100dp"
                android:layout_column="0"
                android:layout_column="0"
                android:layout_row="2"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/mybutton"
                android:onClick="Button5"
                android:padding="5dp"
                android:text="Button5"
                android:textColor="#ffffff"
                android:textSize="20dp" />

            <Button
                android:id="@+id/Button6"
                android:layout_weight = "1"
                android:layout_height="100dp"
                android:layout_column="0"
                android:layout_column="1"
                android:layout_row="2"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/mybutton"
                android:onClick="Button6"
                android:padding="5dp"
                android:text="Button6"
                android:textColor="#ffffff"
                android:textSize="20dp" />

    </GridLayout>

</ScrollView>

如何使用 RecyclerView 和适配器执行此操作?我读了很多教程,但我不明白。我怎样才能制作2列?有没有很好的教程来解释这种情况? 或者有人可以向我解释一下吗? 声音不会从数据库中取出。

最佳答案

使用 RecyclerView, View 的元素根据您连接到 RecyclerView 实例的 LayoutManager 进行布局。如果您希望将元素布置在 2 列网格中,您可以使用 GridLayoutManager

根据文档,您可以使用构造函数创建一个包含 N 列的新 GridLayoutManager:new GridLayoutManager(context, N);

关于java - 如何使用 RecyclerView 创建两列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42752129/

相关文章:

java - 难以使用 Ant 构建 Pig

java - 使用java检索mongodb数组元素

java - 创建自定义注释并在java中使用它?

java - Appium + Android studio -> 重复条目 : com/thoughtworks/selenium/CommandProcessor. 类

java - 使用java将XML字符串转换为文档

xml - 如何使用现有的 Nant 或 Nant Contrib 任务附加 XML 节点?

java - 如何在preparedStatement中将字符编码格式更改为UTF-8。

java - AlarmManager 一旦触发就会不断重复

java - Android TLSv1.2 握手失败

mysql - 如何在mysql中使用EXTRACTVALUE()来获取xmlfrag?