Search code examples
androidandroid-layoutandroid-tablelayout

Is there a way to lock a row in a scrollable tableLayout?


I'm trying to do a scollable tableLayout in AndroidStudio, I alredy make the xml code to make it scrollable, what I need to do is to lock the first row of the table.

Here is my XML code:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity_storico_partite"
android:id="@+id/activity">
<Button
    android:id="@+id/btn_back"
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:layout_marginTop="25dp"
    android:layout_marginLeft="10dp"
    android:background="@color/transparent"
    android:foreground ="@drawable/ic_arrow_back_black_24dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/tx_titolo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="100dp"
    android:text="Storico partite \n terminate"
    android:textSize="30dp"
    android:textAlignment="center"
    android:textStyle="bold"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:gravity="center_horizontal" />

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:layout_span="4"
    android:layout_weight="1"
    android:scrollbars="vertical"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tx_noPartiteGiocate">

    <TableLayout
        android:id="@+id/tableDati"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textAlignment="center"
      >

        <TableRow
            android:id="@+id/tb_fisso"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="10dp">

            <TextView
                android:id="@+id/tx_nomeGiocatore"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="15dp"
                android:gravity="center_horizontal"
                android:text="GIOCATORE"
                android:textAlignment="center"
                android:textColor="@color/Black"
                android:textSize="16dp" />

            <TextView
                android:id="@+id/tx_difficolta"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="15dp"
                android:gravity="center_horizontal"
                android:text="DIFFICOLTA'"
                android:textAlignment="center"
                android:textColor="@color/Black"
                android:textSize="16dp" />

            <TextView
                android:id="@+id/tx_tempo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="15dp"
                android:gravity="center_horizontal"
                android:text="TEMPO"
                android:textAlignment="center"
                android:textColor="@color/Black"
                android:textSize="16dp" />

            <TextView
                android:id="@+id/tx_punteggio"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dp"
                android:gravity="center_horizontal"
                android:text="PUNTI"
                android:textAlignment="center"
                android:textColor="@color/Black"
                android:textSize="16dp" />

        </TableRow>

        <TableRow android:layout_marginBottom="20dp">

            <View
                android:id="@+id/separatore"
                android:layout_width="wrap_content"
                android:layout_height="2dp"
                android:layout_span="5"
                android:layout_weight="1"
                android:background="@color/CobaltBlue"
                android:gravity="center_horizontal"
                android:textAlignment="center" />
        </TableRow>


    </TableLayout>
</ScrollView>

<TextView
    android:id="@+id/tx_noPartiteGiocate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/Black"
    android:text="Nessuna partita"
    android:textSize="18dp"
    android:visibility="invisible"

    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tx_titolo" />

</androidx.constraintlayout.widget.ConstraintLayout>

N.B. I'm creating the rows that I put in the tableLayout dynamically, so if you need also that code just write it in the comment.

Hope you could help me.


Solution

  • No there is no inbuilt feature of tablelayout for this. You can use below trick for this.

    • Take another TableLayout just above ScrollView, Add only one tablerow to it.
    • rest of your code will remain same, In ScrollView , TablLayout

    Like Below.

    <TableLayout>
    <TableRow></TableRow> // this row will be frozen, will not scroll
    </TableLayout>
    
    <ScrollView>
    <TableLayout>
     // Add multiple tablerows in this table dynamically.
    </TableLayout>
    </ScrollView>
    

    And to keep the column parity of both the tables use the below code

    belowTable.post(new Runnable() {
        @Override
        public void run() {
            TableRow tableRow = (TableRow)belowTable.getChildAt(0);
            for(int i = 0; i < headerRow.getChildCount(); i++){
                headerRow.getChildAt(i).setLayoutParams(new TableRow.LayoutParams(tableRow.getChildAt(i).getMeasuredWidth(), tableRow.getChildAt(i).getMeasuredHeight()));
            }
        }
    });