Есть ли способ заблокировать строку в прокручиваемом tableLayout? - PullRequest
0 голосов
/ 16 апреля 2020

Я пытаюсь выполнить прокручиваемую таблицу tableLayout в AndroidStudio, я уже делаю код xml, чтобы сделать его прокручиваемым, мне нужно заблокировать первую строку таблицы.

Вот мой XML код:

<?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>

NB. Я создаю строки, которые я динамически помещаю в tableLayout, поэтому, если вам также нужен этот код, просто напишите его в комментарий.

Надеюсь, вы могли бы помочь мне.

1 Ответ

0 голосов
/ 17 апреля 2020

Нет, для этого нет встроенной функции tablelayout. Для этого вы можете использовать нижеприведенный трюк.

  • Возьмите еще один TableLayout чуть выше ScrollView, добавьте в него только одну таблицу.
  • Остальная часть вашего кода останется такой же, В ScrollView, TablLayout

Как показано ниже.

<TableLayout>
<TableRow></TableRow> // this row will be frozen, will not scroll
</TableLayout>

<ScrollView>
<TableLayout>
 // Add multiple tablerows in this table dynamically.
</TableLayout>
</ScrollView>

И для сохранения четности столбцов обеих таблиц используйте следующий код

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()));
        }
    }
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...