1

I'm trying to create a table that it values can scroll but header row and indexes column should not be scrolled. the table values are created dynamic and I'm generating rows programmatically.

I tried to use separate tables for values, header and indexes and let the values table to scroll but not the others.

Also used onTouchListener to scroll indexes and header table to values table.

Here is my xml:

<!--  headers table  -->
<ScrollView
    android:id="@+id/headersScroll"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.1"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/tableIndexes"
    app:layout_constraintTop_toTopOf="@+id/tableIndexes">

    <TableLayout
        android:id="@+id/tableHeaders"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:padding="0dp"
        android:stretchColumns="*" />
</ScrollView>

<androidx.appcompat.widget.LinearLayoutCompat
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.9"
    android:orientation="horizontal"
    tools:context=".MainActivity">

    
    <ScrollView
        android:id="@+id/indexes_scroll"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/tableIndexes"
        app:layout_constraintTop_toTopOf="@+id/tableIndexes">

        <!--  index table  -->
        <TableLayout
            android:id="@+id/tableIndexes"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:padding="0dp"
            android:stretchColumns="*" />
    </ScrollView>

    
    <ScrollView
        android:id="@+id/data_table_scroll"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.8"
        android:fillViewport="true"
        android:scrollbarStyle="outsideInset">

        <HorizontalScrollView
            android:id="@+id/horizontalScrollView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/tableIndexes"
            app:layout_constraintTop_toTopOf="@+id/tableIndexes">

            <!--  values table  -->
            <TableLayout
                android:id="@+id/tableInvoices"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="0dp"
                android:stretchColumns="*" />

        </HorizontalScrollView>
    </ScrollView>

</androidx.appcompat.widget.LinearLayoutCompat>

and this is how I'm handle indexes scrolling:

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {

    int scrollX = view.getScrollX();
    int scrollY = view.getScrollY();
    switch (view.getId()) {
        case (R.id.indexes_scroll):
            binding.dataTableScroll.scrollTo(scrollX, scrollY);
            break;
        case (R.id.data_table_scroll):
            binding.indexesScroll.scrollTo(scrollX, scrollY);
            break;
    }

    return false;

}

with this approach scrolling of indexes table not work correctly and for example sometimes indexes table not scrolling to exact position of values table.

how should I implement this table ? please help! Thanks alot

0 Answers0