Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a TableView with a fixed header row (does not scroll)

Tags:

android

I have a TableView called aTable. initialiseDTable() adds the first row with a RED background

GOAL

  1. Prevent the header row from scrolling (WORKING FINE)

  2. Keep the header row with the same width of each TextView which gives the feeling that it is the child of aTable (NOT WORKING)

ATTEMP

My attempt was to create a tableview before the scrollview and populate aTable then extract the header row and insert it as a child of this tableview.

CODE TO ADD HEADER ROW TO TABLE

    public void initialiseDTable() {
    LayoutInflater inflater = getLayoutInflater();
    TableRow row = (TableRow) inflater.inflate(R.layout.table_row, dTable,
            false);
    View v = (View) inflater.inflate(R.layout.table_cell, row, false);
    TextView A = (TextView) v.findViewById(R.id.table_cell_text);
    A.setText(tag1);
    row.addView(v);
    v = (View) inflater.inflate(R.layout.table_cell, row, false);
    A = (TextView) v.findViewById(R.id.table_cell_text);
    A.setText(tag2);
    row.addView(v);
    v = (View) inflater.inflate(R.layout.table_cell, row, false);
    A = (TextView) v.findViewById(R.id.table_cell_text);
    A.setText(tag3);
    row.addView(v);
    v = (View) inflater.inflate(R.layout.table_cell, row, false);
    A = (TextView) v.findViewById(R.id.table_cell_text);
    A.setText(tag4);
    row.addView(v);
    if (this.showEST) {
        v = (View) inflater.inflate(R.layout.table_cell, row, false);
        A = (TextView) v.findViewById(R.id.table_cell_text);
        A.setBackgroundColor(Color.RED);
        A.setText(tag5);
        row.addView(v);
    }
    dTable.addView(row);
}

CODE

public void updateATable() {
    aTable.removeAllViews();
    initialiseATable();
    for (int i = 0; i < newA.size(); i++)
        aTable.addView(newA.get(i)); //newA is an ArrayList <TableRow>
    View view = aTable.getChildAt(0);
    aTable.removeViewAt(0);
    aTableH.removeAllViews();
    aTableH.addView(view,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
}

XML

        <LinearLayout android:orientation="vertical"
            android:layout_width="fill_parent" android:layout_height="fill_parent"
            android:gravity="center" android:id="@+id/aLL">
            <TableLayout android:shrinkColumns="5"
                android:stretchColumns="1,2,3,4" android:layout_height="wrap_content"
                android:layout_width="wrap_content" android:id="@+id/aTableH">
            </TableLayout>
            <ScrollView android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:id="@+id/aTableSV">
                <TableLayout android:shrinkColumns="5"
                    android:stretchColumns="1,2,3,4" android:layout_height="fill_parent"
                    android:layout_width="fill_parent" android:id="@+id/aTable">
                </TableLayout>
            </ScrollView>
        </LinearLayout>
like image 955
Sherif elKhatib Avatar asked Jul 27 '11 11:07

Sherif elKhatib


1 Answers

I am writing a series of blog posts which cover this precise issue. The first is here and describes a similar approach to the answer already given. However the next two articles cover a much more elegant solution.

like image 166
Mark Allison Avatar answered Sep 20 '22 20:09

Mark Allison