根据行数不同列数的GridView [英] GridView with different number of columns according to the number of row

查看:35
本文介绍了根据行数不同列数的GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了 2 天,我正在尝试为第一行有两列的视图制作 UI,然后在第二行有三列,第三行又是两列,依此类推..而且我还有实现动态数据(意味着行/列数是动态的).

I am trying this for 2 days,I am trying to make UIs for the view having two columns in first row,then in second row having three columns,and in third again two columns and so on..And also I have to implement dynamic data(means num of rows/columns are dynamic).

我不知道实现这种类型的视图.我也尝试过交错网格视图,但这是用于动态视图..但在此我有静态 2,3,2,3... 列1,2,3,4..rows.请帮助我.任何教程都会最有帮助.

I don't have any idea to implement this type of view.I have also try stragged grid view but this is for dynamic views..but in this I have static 2,3,2,3... columns for 1,2,3,4..rows.Please help me.Any tutorial will be most helpful.

推荐答案

终于解决了这个问题.我已经尝试过 Raghunandan 和 user3278416 的建议.并且在运行时做了所有的东西,除了表布局.这里是 xml 文件:

Finally I have solved this problem.I have tried with Raghunandan and user3278416's suggestion.And did all the stuff in runtime except Table layout.Here is the xml file:

 <ScrollView  android:layout_width="match_parent"
    android:layout_height="match_parent">
<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/table_layout"
     >

这两种布局一个是小布局(第二行),一个是大布局(第一行):

And the the two layouts one is for small layout(the second row) and one is for large one(first row):

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="130dp"
android:layout_height="160dp"
android:layout_margin="3dp"
android:background="@drawable/big_product_bg" >
<ImageView
    android:id="@+id/img_offersrowlayout"
    android:layout_width="120dp"
    android:layout_height="80dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:scaleType="fitXY"
    android:src="@drawable/img_bydefault" />

<TextView
    android:id="@+id/txt_title_offerrowlayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/img_offersrowlayout"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="8dp"
    android:gravity="center_horizontal"
    android:singleLine="true"
    android:text="BenQ Digital camera"
    android:textColor="@color/green_app"
    android:textSize="15dp"
    android:textStyle="bold" />

小的:

<RelativeLayout
android:layout_width="90dp"
android:layout_height="160dp"
android:layout_margin="3dp"
android:background="@drawable/big_product_bg" >

<ImageView
    android:id="@+id/img_offersrowlayout"
    android:layout_width="90dp"
    android:layout_height="80dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:scaleType="fitXY"
    android:src="@drawable/img_bydefault" />

<TextView
    android:id="@+id/txt_title_homesmallrowlayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/img_offersrowlayout"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="8dp"
    android:gravity="center_horizontal"
    android:singleLine="true"
    android:text="BenQ Digital camera"
    android:textColor="@color/green_app"
    android:textSize="15dp"
    android:textStyle="bold" />

现在是 Java 代码:

Now the Java Code:

 int leftMargin_small=2;
int topMargin_small=5;
int rightMargin_small=2;
int bottomMargin_small=5;

   int j = 0;
   while (Show.size()>j) {

try {
     LayoutInflater inflater =       (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     TableRow.LayoutParams param = new TableRow.LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT, 1.0f);
     param.setMargins(leftMargin_small, topMargin_small, rightMargin_small, bottomMargin_small);
     LinearLayout layout1 =  new LinearLayout(getApplicationContext());
     TableRow.LayoutParams param_layout = new TableRow.LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT, 1.0f);
     layout1.setLayoutParams(param_layout);
View question = inflater.inflate(R.layout.offers_rowlayout, null);

question.setLayoutParams(param);
//question.setId(pos);
TextView title = (TextView) question.findViewById(R.id.txt_title_offerrowlayout);
title.setText(""+">>"+Show.get(j).get("name"));

View question1 = inflater.inflate(R.layout.offers_rowlayout, null);
//question.setId(pos);
question1.setLayoutParams(param);
TextView title1 = (TextView) question1.findViewById(R.id.txt_title_offerrowlayout);
title1.setText(""+">>"+Show.get(j+1).get("name"));



View question_small = inflater.inflate(R.layout.home_row_small_layout, null);
//question.setId(pos);
question_small.setLayoutParams(param);
TextView title_small = (TextView) question_small.findViewById(R.id.txt_title_homesmallrowlayout);
title_small.setText(""+">>"+Show.get(j).get("name"));

View question1_small = inflater.inflate(R.layout.home_row_small_layout, null);
//question.setId(pos);
question1_small.setLayoutParams(param);
TextView title1_small = (TextView) question1_small.findViewById(R.id.txt_title_homesmallrowlayout);
title1_small.setText(""+">>"+Show.get(j+1).get("name"));

View question2_small = inflater.inflate(R.layout.home_row_small_layout, null);
//question.setId(pos);
question2_small.setLayoutParams(param);
TextView title2_small = (TextView) question2_small.findViewById(R.id.txt_title_homesmallrowlayout);
title2_small.setText(""+">>"+Show.get(j+2).get("name"));
       if (gett) {
      TableRow tr = new TableRow(getApplicationContext());

        TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(
                TableLayout.LayoutParams.MATCH_PARENT,
                TableLayout.LayoutParams.MATCH_PARENT,1);
      //  trParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
        tr.setLayoutParams(trParams);
       layout1.addView(question);
       layout1.addView(question1);
       tr.addView(layout1);
    tablee.addView(tr);
   gett = false;
        j = j+2;
}
else
{
      TableRow tr = new TableRow(getApplicationContext());

        TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(
                TableLayout.LayoutParams.MATCH_PARENT,
                TableLayout.LayoutParams.MATCH_PARENT,1);

        layout1.addView(question_small);
        layout1.addView(question1_small);
        layout1.addView(question2_small);
        tr.addView(layout1);
    tablee.addView(tr);
    gett = true;
    j = j+3;
}

} catch (Exception e) {
    e.printStackTrace();
}


  }

这里显示的是hashmap类型的arraylist,tablee是我在Xml文件中制作的tablelayout对象.

Here Show is the arraylist of hashmap type and tablee is the tablelayout object which I have made in Xml file.

这篇关于根据行数不同列数的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆