evrencoskun / TableView

TableView is a powerful Android library for displaying complex data structures and rendering tabular data composed of rows, columns and cells.
MIT License
3.14k stars 457 forks source link

column header can not be fixed while tableview in a nestscorllview / scrollview #371

Closed gaoyl87 closed 1 year ago

gaoyl87 commented 3 years ago

Thank you for this rich usage library!!

TableView works fine without any scrollable parent.It also can scroll in a nestscorllview, but column header can not be fixed. In my case, I have some TextViews above TableView, they scroll up with NestedScrollView. My expection is that when TextViews scoll out of screen, column header can stay in top of screen. What shoud I do?

Here is my layout xml (rewrite by tablesampleapp's layout xml) `<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent">

<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Row 1"
            android:textSize="48sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Row 2"
            android:textSize="48sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Row 3"
            android:textSize="48sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Row 4"
            android:textSize="48sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Row 5"
            android:textSize="48sp" />

        <com.ctsec.common.view.tableview.TableView
            android:id="@+id/my_TableView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            app:table_view_column_header_height="32dp"
            app:table_view_row_header_width="100dp"
            app:table_view_selected_color="@color/blue_4590ED"
            app:table_view_separator_color="@color/white"
            app:table_view_separator_divider_drawable="@drawable/table_view_divider"
            app:table_view_shadow_color="@color/blue_4590ED"
            app:table_view_show_horizontal_separator="false"
            app:table_view_unselected_color="@color/white" />
    </LinearLayout>

</androidx.core.widget.NestedScrollView>

<ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center" />

`

Here is gif. demo

I'm looking forward to your reply soon!

Zardozz commented 3 years ago

This would be a bug with your NestedScrollView implementation, by nesting it inside another scrollview you have made Tableview larger than the screen and not in control of it's vertical positioning.

Tableview can only control the position of it's child view elements (which include it's fixed headers) when you move TableView as a whole too far upwards by wrapping it with the NestScrollView it is the NestedScrollView that is causing the problem not TableView.

You might be able to fix your NestedScrollView implementation but I'm not sure this would work.

You need to make sure that the TableView's size is no bigger than the screen, you cannot use "match_parent" because the parent's size will include the size of tableview plus the TextView's you've added. You would probably need to create the TableView programmatically to fix the size to the size of the NestedScrollView's parent i.e this would be in a fixed dp number based size).

Once you have the size right then the NestedScrollView won't be able scroll the top of TableView off screen.

gaoyl87 commented 3 years ago

@Zardozz Thank you for your reply. I changed TableView's height size in code and make mTableView.getCellRecyclerView().setNestedScrollingEnabled(true).

View root = view.findViewById(R.id.fl_root); root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { root.getViewTreeObserver().removeOnGlobalLayoutListener(this); mTableView.getLayoutParams().height = root.getHeight(); } }); mTableView.getCellRecyclerView().setNestedScrollingEnabled(true);

The root is FrameLayout and it works file.But NestedScorllView can not scorll when I fling TableView.I must touch on TextViews and scroll them out of screen,then I can scroll TableView.How can I scroll TableView and make TextViews scroll together?

17382910c4baf5b37456f2ba93d0986e

gaoyl87 commented 3 years ago

@Zardozz If it is convenient for you,I hope you can provide an extense sampleapp with NestedScrollView / CoordinatorLayout.Thank you for your help.

Zardozz commented 3 years ago

This looks like a generic issue of nesting one scrollable item in another, I won't be extending the sample app to show your particular usecase. I would seek help on StackOverflow.

gaoyl87 commented 3 years ago

I tried to write CustomBehavior to scroll TableView in CoordinatorLayout,but it is useless.Can you give me some idea?

KoTius commented 3 years ago

I would say more. Since it is in NestedScroll and the table view doesn't have fixed height, the TableView recycling is not working. It inflates and draws in this case all its items which will cause you frame drops(actually very visible for users, like when you open the screen with big table UI freezes) if there are many rows. It doesn't play well within Nested scrolls, unfortunately.

Need to play with setting to the table view fixed width but then scroll behavior issues arise