1

次のように GridView を使用してテーブルを作成する方法:

例

つまり:

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

1 に答える 1

1

グリッドビュー用のカスタム アダプターを作成できます。

グリッドの各セルにボタンが含まれていると仮定すると、リストが必要になります。

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

GridView には、レイアウト xml ファイルで指定する必要がある 7 つの列があります。

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 に答える