如何在android中使用GridView?

时间:2015-10-30 10:09:01

标签: android android-recyclerview

我正在使用RecyclerView和GridLayoutManager

我需要在android中构建一个简单的Matching game。这是我的Model课程:

public class Model{
 private String name;
 private String SchoolName;
 .....
 // Getter and Setter methods
 ....
} 

我正在使用GridView来显示数据并且工作正常但是我想在填充GridView中添加以下约束:

  • 在第一列中加载名称
  • 在第二栏中加载SchoolName

目前正按随机顺序加载:

Name1                 |  Name 2
SchoolName 1          | Name 3
Name 3                | SchoolName 2

我该怎么做?做这件事的好方法是什么?

2 个答案:

答案 0 :(得分:0)

使用具有此布局的自定义列表视图

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="HTTP://sachems.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ddd"
    android:padding="8dp"
    android:orientation="horizontal">

   <TextView
       android:id="@+id/name"
       android:layout_width="wrap_content"
       android:layout_height="match_parent"
       android:elevation="1dp"
       android:layout_marginRight="8dp"
       android:layout_weight="1"
       android:background="#fff"/>

    <TextView
        android:id="@+id/school_name"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:elevation="1dp"
        android:layout_weight="1"
        android:background="#fff"/>
</LinearLayout>

然后在第一个textview上显示名称,在第二个textview上显示学校名称

或者你可以做到

position%2==0 {
 show name 
}
else {
 show school name
}

答案 1 :(得分:0)

取两个数组一个用于名称,另一个用于学校名称,并使用位置属性设置它们。

类似的东西:

private int[] names = {
      //names here
    };

private int[] schoolnames = {

        //school names here
    };

然后在适配器中:

public View getView(int position, View convertView, ViewGroup arg2) {
            // TODO Auto-generated method stub

            if(position == x){
                //add name
            }
             else{
                //add school name
                   }

            return view;
        }