0

I want to achieve this layout. Table can have different number of rows, but once this number is set, it wouldn't change. Also, there is no need for scroll.

Well... I could do this with simple TableLayout, but it doesn't support data binding.

So I'm trying to implement custom AdapterView, but nothing is appeared in a screen.

public class MainActivity extends Activity {
String[][] data = new String[5][5];
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    RelativeLayout layout = (RelativeLayout) getLayoutInflater().inflate(R.layout.activity_main, null);
    setContentView(layout);
    SquareTableView sqt = new SquareTableView(this);
    sqt.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    sqt.setNumColumns(5);
    layout.addView(sqt);
    for (int i = 0; i < 5; i++)
        for (int j = 0; j < 5; j++)
            data[i][j] = "a";
    sqt.setAdapter(new CellArrayAdapter(this, data) {

    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

public class SquareTableView extends AdapterView<ListAdapter> {
private ListAdapter adapter;
private int numColumns;
public int getNumColumns() {
    return numColumns;
}
public void setNumColumns(int numColumns) {
    this.numColumns = numColumns;
}

public SquareTableView(Context context) {
    super(context);
}

@Override
public ListAdapter getAdapter() {
    return adapter;
}

@Override
public View getSelectedView() {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void setAdapter(ListAdapter adapter) {
    this.adapter = adapter;
    requestLayout();
}



@Override
public void setSelection(int position) {
    // TODO Auto-generated method stub

}


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    Context context = getContext();
    int orientation = context.getResources().getConfiguration().orientation;
    int sideSize;
    if (orientation ==  Configuration.ORIENTATION_PORTRAIT)
        sideSize = MeasureSpec.getSize(widthMeasureSpec);
    else {
        sideSize = MeasureSpec.getSize(heightMeasureSpec);
    }
    setMeasuredDimension(sideSize, sideSize);

}

@Override
protected void onLayout(boolean changed, int left, int top, int right,
        int bottom) {
     super.onLayout(changed, left, top, right, bottom);
    for (int i = 0; i < adapter.getCount(); i++) {
        int childSide = getWidth() / numColumns;
        int row = i / numColumns;
        int column = i % numColumns;
        int leftPosition = childSide * column;
        int rightPosition = leftPosition + childSide;
        int topPosition = childSide * row;
        int bottomPosition = topPosition + childSide;
        View child = adapter.getView(i, null, this);
        child.layout(leftPosition, topPosition, rightPosition, bottomPosition);
        child.forceLayout();
    }
}

enter image description here

4

0 回答 0