From bed638257360ae8f4ef0079930f3717f6e3b01b6 Mon Sep 17 00:00:00 2001 From: Michael Gangolf Date: Tue, 9 Aug 2022 23:40:54 +0200 Subject: [PATCH] feat(android): add position to TableView scrollToIndex (#13505) * feat(android): add position to TableView scrollToIndex * fix(docs): add deprecation info --- .../modules/titanium/ui/TableViewProxy.java | 25 +++++++++++++-- .../ui/TableViewScrollPositionModule.java | 31 +++++++++++++++++++ apidoc/Titanium/UI/ListViewScrollPosition.yml | 2 +- apidoc/Titanium/UI/TableView.yml | 4 +-- .../Titanium/UI/TableViewScrollPosition.yml | 29 +++++++++++++++++ .../UI/iOS/ListViewScrollPosition.yml | 11 ++++--- .../UI/iOS/TableViewScrollPosition.yml | 11 ++++--- 7 files changed, 100 insertions(+), 13 deletions(-) create mode 100644 android/modules/ui/src/java/ti/modules/titanium/ui/TableViewScrollPositionModule.java create mode 100644 apidoc/Titanium/UI/TableViewScrollPosition.yml diff --git a/android/modules/ui/src/java/ti/modules/titanium/ui/TableViewProxy.java b/android/modules/ui/src/java/ti/modules/titanium/ui/TableViewProxy.java index 9ea2a668027..0585e120475 100644 --- a/android/modules/ui/src/java/ti/modules/titanium/ui/TableViewProxy.java +++ b/android/modules/ui/src/java/ti/modules/titanium/ui/TableViewProxy.java @@ -12,6 +12,7 @@ import org.appcelerator.kroll.KrollDict; import org.appcelerator.kroll.annotations.Kroll; +import org.appcelerator.titanium.TiApplication; import org.appcelerator.titanium.TiC; import org.appcelerator.titanium.TiDimension; import org.appcelerator.titanium.proxy.TiViewProxy; @@ -21,6 +22,8 @@ import android.view.View; import androidx.recyclerview.selection.SelectionTracker; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.LinearSmoothScroller; import androidx.recyclerview.widget.RecyclerView; import ti.modules.titanium.ui.widget.TiUITableView; @@ -812,6 +815,14 @@ public void scrollToIndex(int index, @Kroll.argument(optional = true) KrollDict { final TiTableView tableView = getTableView(); final boolean animated = animation == null || animation.optBoolean(TiC.PROPERTY_ANIMATED, true); + final int position = animation != null ? animation.optInt(TiC.PROPERTY_POSITION, 0) : 0; + final RecyclerView.SmoothScroller smoothScrollerToTop = + new LinearSmoothScroller(TiApplication.getAppCurrentActivity()) + { + @Override + protected int getVerticalSnapPreference() + { return LinearSmoothScroller.SNAP_TO_START; } + }; if (tableView != null) { final RecyclerView recyclerView = tableView.getRecyclerView(); @@ -823,9 +834,19 @@ public void scrollToIndex(int index, @Kroll.argument(optional = true) KrollDict final int rowAdapterIndex = tableView.getAdapterIndex(index); final Runnable action = () -> { if (animated) { - recyclerView.smoothScrollToPosition(rowAdapterIndex); + if (position == ListViewScrollPositionModule.TOP) { + smoothScrollerToTop.setTargetPosition(rowAdapterIndex); + recyclerView.getLayoutManager().startSmoothScroll(smoothScrollerToTop); + } else { + recyclerView.smoothScrollToPosition(rowAdapterIndex); + } } else { - recyclerView.scrollToPosition(rowAdapterIndex); + if (position == ListViewScrollPositionModule.TOP) { + ((LinearLayoutManager) recyclerView.getLayoutManager()) + .scrollToPositionWithOffset(rowAdapterIndex, 0); + } else { + recyclerView.scrollToPosition(rowAdapterIndex); + } } }; diff --git a/android/modules/ui/src/java/ti/modules/titanium/ui/TableViewScrollPositionModule.java b/android/modules/ui/src/java/ti/modules/titanium/ui/TableViewScrollPositionModule.java new file mode 100644 index 00000000000..371b7094b77 --- /dev/null +++ b/android/modules/ui/src/java/ti/modules/titanium/ui/TableViewScrollPositionModule.java @@ -0,0 +1,31 @@ +/** + * Appcelerator Titanium Mobile + * Copyright (c) 2009-2020 by Appcelerator, Inc. All Rights Reserved. + * Licensed under the terms of the Apache Public License + * Please see the LICENSE included with this distribution for details. + */ +package ti.modules.titanium.ui; + +import org.appcelerator.kroll.KrollModule; +import org.appcelerator.kroll.annotations.Kroll; + +import ti.modules.titanium.ui.UIModule; + +@Kroll.module(parentModule = UIModule.class) +public class TableViewScrollPositionModule extends KrollModule +{ + @Kroll.constant + public static final int TOP = 1; + @Kroll.constant + public static final int BOTTOM = 2; + @Kroll.constant + public static final int MIDDLE = 3; + @Kroll.constant + public static final int NONE = 0; + + @Override + public String getApiName() + { + return "Ti.UI.TableViewScrollPositionModule"; + } +} diff --git a/apidoc/Titanium/UI/ListViewScrollPosition.yml b/apidoc/Titanium/UI/ListViewScrollPosition.yml index e6171f85c80..192b6a3304a 100644 --- a/apidoc/Titanium/UI/ListViewScrollPosition.yml +++ b/apidoc/Titanium/UI/ListViewScrollPosition.yml @@ -6,7 +6,7 @@ summary: | `deleteSectionAt`, `insertSectionAt` and `replaceSectionAt` methods. extends: Titanium.Proxy platforms: [android, iphone, ipad, macos] -since: {android: "10.2.0", iphone: "5.4.0", ipad: "5.4.0", macos: "9.2.0"} +since: 11.1.0 excludes: properties: [bubbleParent] methods: [addEventListener, applyProperties, fireEvent, removeEventListener] diff --git a/apidoc/Titanium/UI/TableView.yml b/apidoc/Titanium/UI/TableView.yml index 47efb4c87a2..31c42515de1 100644 --- a/apidoc/Titanium/UI/TableView.yml +++ b/apidoc/Titanium/UI/TableView.yml @@ -1823,8 +1823,8 @@ properties: - name: position summary: Specifies what position to scroll the selected row to. type: Number - constants: Titanium.UI.iOS.TableViewScrollPosition.* - default: + constants: Titanium.UI.TableViewScrollPosition.* + default: --- name: TableViewIndexEntry diff --git a/apidoc/Titanium/UI/TableViewScrollPosition.yml b/apidoc/Titanium/UI/TableViewScrollPosition.yml new file mode 100644 index 00000000000..51f95d42908 --- /dev/null +++ b/apidoc/Titanium/UI/TableViewScrollPosition.yml @@ -0,0 +1,29 @@ +--- +name: Titanium.UI.TableViewScrollPosition +summary: | + A set of constants for the position value that can be used for the `position` property of + when invoking `scrollToIndex`. +extends: Titanium.Proxy +platforms: [android, iphone, ipad, macos] +since: 11.1.0 +createable: false +properties: + - name: BOTTOM + summary: The table view scrolls the row of interest to the bottom of the visible table view. + type: Number + permission: read-only + + - name: MIDDLE + summary: The table view scrolls the row of interest to the middle of the visible table view. + type: Number + permission: read-only + + - name: NONE + summary: The table view scrolls the row of interest to be fully visible with a minimum of movement. If the row is already fully visible, no scrolling occurs. For example, if the row is above the visible area, the behavior is identical to that specified by `TOP`. This is the default. + type: Number + permission: read-only + + - name: TOP + summary: The table view scrolls the row of interest to the top of the visible table view. + type: Number + permission: read-only diff --git a/apidoc/Titanium/UI/iOS/ListViewScrollPosition.yml b/apidoc/Titanium/UI/iOS/ListViewScrollPosition.yml index a81a7f568b1..cc75c0e9768 100644 --- a/apidoc/Titanium/UI/iOS/ListViewScrollPosition.yml +++ b/apidoc/Titanium/UI/iOS/ListViewScrollPosition.yml @@ -1,7 +1,10 @@ --- name: Titanium.UI.iOS.ListViewScrollPosition +deprecated: + since: 11.1.0 + notes: Use [Titanium.UI.ListViewScrollPosition](Titanium.UI.ListViewScrollPosition) instead. summary: | - A set of constants for the position value that can be used for the `position` property of + A set of constants for the position value that can be used for the `position` property of when invoking the ListView's `scrollToItem`, `appendSection`, `deleteSectionAt`, `insertSectionAt` and `replaceSectionAt` methods. extends: Titanium.Proxy @@ -16,12 +19,12 @@ properties: summary: The list view scrolls the row of interest to the bottom of the visible list view. type: Number permission: read-only - + - name: MIDDLE summary: The list view scrolls the row of interest to the middle of the list table view. type: Number permission: read-only - + - name: NONE summary: | The table view scrolls the row of interest to be fully visible with a minimal movement. @@ -29,7 +32,7 @@ properties: visible area, the behavior is identical to that specified by `TOP`. This is the default. type: Number permission: read-only - + - name: TOP summary: The list view scrolls the row of interest to the top of the visible list view. type: Number diff --git a/apidoc/Titanium/UI/iOS/TableViewScrollPosition.yml b/apidoc/Titanium/UI/iOS/TableViewScrollPosition.yml index d53d3f1d2d2..63d3fd00431 100644 --- a/apidoc/Titanium/UI/iOS/TableViewScrollPosition.yml +++ b/apidoc/Titanium/UI/iOS/TableViewScrollPosition.yml @@ -1,7 +1,10 @@ --- name: Titanium.UI.iOS.TableViewScrollPosition +deprecated: + since: 11.1.0 + notes: Use [Titanium.UI.ListViewScrollPosition](Titanium.UI.ListViewScrollPosition) instead. summary: | - A set of constants for the position value that can be used for the `position` property of + A set of constants for the position value that can be used for the `position` property of when invoking `scrollToIndex`. extends: Titanium.Proxy platforms: [iphone, ipad, macos] @@ -12,17 +15,17 @@ properties: summary: The table view scrolls the row of interest to the bottom of the visible table view. type: Number permission: read-only - + - name: MIDDLE summary: The table view scrolls the row of interest to the middle of the visible table view. type: Number permission: read-only - + - name: NONE summary: The table view scrolls the row of interest to be fully visible with a minimum of movement. If the row is already fully visible, no scrolling occurs. For example, if the row is above the visible area, the behavior is identical to that specified by `TOP`. This is the default. type: Number permission: read-only - + - name: TOP summary: The table view scrolls the row of interest to the top of the visible table view. type: Number