1

How to create table using GridView like this:

example

I mean:

1st column - 2 rows;
2nd column - 1 row;
3rd column - 2 rows;
.... and so on 
4

1 回答 1

1

您可以为 gridview 创建一个自定义适配器。

假设网格中的每个单元格都包含一个按钮,因此您需要一些列表

ArrayList<Button> listOfButtons = new ArrayList<Button>();

您的 GridView 将有 7 列,您需要在布局 xml 文件中指定这些列

android:numColumns="5"

您的适配器类将检查列表并查看正在实例化的单元格。如果它是正确的单元格,则对其进行修改。

public class MyAdapter extends BaseAdapter {

private Context context;

public MyAdapter(Context context) {
    this.context = context;
}

public int getCount() {
    return listOfButtons.size();
}

@Override
public boolean areAllItemsEnabled() {
    return true;
}

public boolean isEnabled(int position) {
    return true;
    // return true for clickable, false for not
}

public Button getItem(int position) {
    return listOfButtons.get(position);
}

public long getItemId(int position) {
    return listOfButtons.get(position).getId();
}

@Override
public int getViewTypeCount() {
    return 1;
}

public View getView(int position, View convertView, ViewGroup parent) {
        Button b;

        //Here is where you check to see if it's the correct cell
        //You can use the methods declared above to check
        //say i wanted every even item to be 1 cell

        if (position % 2 = 0)
            b.setHeight(100);//however high two columns are

        /*you'd also need to make the other button that's 
         * being overlapped invisible. This requires that 
         * you know what position it's going to be in. 
         * Using the same eample with 7 columns, 
         * position 8, 10, 12 and 14 will be overlapped so 
         * you can do something like*/
        if (position == 8 || position == 10 || 
            position ==12 || position ==14)
        {
            b.setVisibility(View.GONE);
            return b;
        }
        if (convertView == null) {
            b.setPadding(10, 10, 10, 10);
        } else {
            b = (Button) convertView;
        }

        //Set properties of each
        return b;
    }

}

希望这可以帮助

于 2012-09-13T14:16:42.243 回答