[go: nahoru, domu]

Support developer to change scroll bar thumb visibility.

When the length of list items varies a lot,the thunmb length will change
dramatically. It maybe confusing to the user. Demo videos are included in
the bug.

Bug: 114479411

Test: user can set scrollbar thumb visibility through modifying the activity_paged_list_view.xml file.
Change-Id: I4eb10a05f77d3b797a675d785535e4e881042851
diff --git a/car/core/api/current.txt b/car/core/api/current.txt
index 3c9784b..dad67d2 100644
--- a/car/core/api/current.txt
+++ b/car/core/api/current.txt
@@ -336,6 +336,7 @@
     method public int getPage(int);
     method public androidx.recyclerview.widget.RecyclerView getRecyclerView();
     method public int getRowsPerPage();
+    method public boolean getShowScrollBarThumb();
     method public void hideAlphaJump();
     method public boolean isAtEnd();
     method public boolean isAtStart();
@@ -362,6 +363,7 @@
     method public void setOnScrollListener(androidx.car.widget.PagedListView.OnScrollListener);
     method public void setScrollBarContainerWidth(int);
     method public void setScrollBarTopMargin(int);
+    method public void setShowScrollBarThumb(boolean);
     method public void setUpButtonIcon(android.graphics.drawable.Drawable);
     method public void snapToPosition(int);
     field public static final int DEFAULT_MAX_CLICKS = 6; // 0x6
@@ -402,6 +404,7 @@
     ctor public PagedScrollBarView(android.content.Context, android.util.AttributeSet);
     ctor public PagedScrollBarView(android.content.Context, android.util.AttributeSet, int);
     ctor public PagedScrollBarView(android.content.Context, android.util.AttributeSet, int, int);
+    method public boolean getShowScrollBarThumb();
     method public boolean isDownEnabled();
     method public boolean isDownPressed();
     method public boolean isUpPressed();
@@ -413,6 +416,7 @@
     method public void setPaginationListener(androidx.car.widget.PagedScrollBarView.PaginationListener);
     method public void setParameters(int, int, int, boolean);
     method public void setScrollbarThumbColor(int);
+    method public void setShowScrollBarThumb(boolean);
     method public void setUpButtonIcon(android.graphics.drawable.Drawable);
     method public void setUpEnabled(boolean);
   }
diff --git a/car/core/res-public/values/public_attrs.xml b/car/core/res-public/values/public_attrs.xml
index 47afc71..7ade86e 100644
--- a/car/core/res-public/values/public_attrs.xml
+++ b/car/core/res-public/values/public_attrs.xml
@@ -44,6 +44,7 @@
     <public type="attr" name="downButtonIcon" />
     <public type="attr" name="buttonTintColor" />
     <public type="attr" name="buttonRippleBackground" />
+    <public type="attr" name="showScrollBarThumb"/>
 
     <!-- ListItem -->
     <public type="attr" name="listItemBackgroundColor" />
diff --git a/car/core/res/values/attrs.xml b/car/core/res/values/attrs.xml
index 816f5d0..4f353ab 100644
--- a/car/core/res/values/attrs.xml
+++ b/car/core/res/values/attrs.xml
@@ -110,6 +110,8 @@
         <!-- The background for the buttons in the PagedListView. This background should provide
              the ripple. -->
         <attr name="buttonRippleBackground" format="reference" />
+        <!-- Whether to display the scrollthumb or not. Defaults to true. -->
+        <attr name="showScrollBarThumb" format="boolean" />
     </declare-styleable>
 
     <!-- The configuration for modifying the ListItem. These ListItems are only intended to be used
diff --git a/car/core/src/androidTest/java/androidx/car/widget/PagedListViewTest.java b/car/core/src/androidTest/java/androidx/car/widget/PagedListViewTest.java
index a16ade9..88027d8 100644
--- a/car/core/src/androidTest/java/androidx/car/widget/PagedListViewTest.java
+++ b/car/core/src/androidTest/java/androidx/car/widget/PagedListViewTest.java
@@ -349,6 +349,21 @@
     }
 
     @Test
+    public void testScrollBarThumbShowByDefault() {
+        // Just need enough items to ensure the scroll bar is showing.
+        setUpPagedListView(ITEMS_PER_PAGE * 10);
+        onView(withId(R.id.scrollbar_thumb)).check(matches(isDisplayed()));
+    }
+
+    @Test
+    public void testScrollBarThumbIsHidden() {
+        // Just need enough items to ensure the scroll bar is showing.
+        setUpPagedListView(ITEMS_PER_PAGE * 10);
+        mPagedListView.setShowScrollBarThumb(false);
+        onView(withId(R.id.scrollbar_thumb)).check(matches(not(isDisplayed())));
+    }
+
+    @Test
     public void testSetGutterNone() {
         // Just need enough items to ensure the scroll bar is showing.
         setUpPagedListView(ITEMS_PER_PAGE * 10);
diff --git a/car/core/src/main/java/androidx/car/widget/PagedListView.java b/car/core/src/main/java/androidx/car/widget/PagedListView.java
index 04cafda..4cf9ab4 100644
--- a/car/core/src/main/java/androidx/car/widget/PagedListView.java
+++ b/car/core/src/main/java/androidx/car/widget/PagedListView.java
@@ -464,6 +464,22 @@
     }
 
     /**
+     * Set the visibility of scroll bar thumb in the scroll bar, the default visibility is true.
+     *
+     * @param show Whether to show the scrollbar thumb or not.
+     */
+    public void setShowScrollBarThumb(boolean show) {
+        mScrollBarView.setShowScrollBarThumb(show);
+    }
+
+    /**
+     * Returns {@code true} if the scroll bar thumb is visible
+     */
+    public boolean getShowScrollBarThumb() {
+        return mScrollBarView.getShowScrollBarThumb();
+    }
+
+    /**
      * Sets an offset above the first item in the {@code PagedListView}. This offset is scrollable
      * with the contents of the list.
      *
diff --git a/car/core/src/main/java/androidx/car/widget/PagedScrollBarView.java b/car/core/src/main/java/androidx/car/widget/PagedScrollBarView.java
index ec4d48a..60c8275 100644
--- a/car/core/src/main/java/androidx/car/widget/PagedScrollBarView.java
+++ b/car/core/src/main/java/androidx/car/widget/PagedScrollBarView.java
@@ -70,6 +70,7 @@
     private final int mSeparatingMargin;
     private final int mScrollBarThumbWidth;
 
+    private boolean mShowScrollBarThumb;
     /** The amount of space that the scroll thumb is allowed to roam over. */
     private int mScrollThumbTrackHeight;
 
@@ -109,12 +110,13 @@
         mDownButton.setOnClickListener(mDownButtonClickListener);
         mAlphaJumpButton = findViewById(R.id.alpha_jump);
         mAlphaJumpButton.setOnClickListener(mAlphaJumpButtonClickListener);
-
         mScrollThumb = findViewById(R.id.scrollbar_thumb);
 
         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedScrollBarView,
                 defStyleAttrs, defStyleRes);
 
+        mShowScrollBarThumb = a.getBoolean(R.styleable.PagedScrollBarView_showScrollBarThumb, true);
+        setShowScrollBarThumb(mShowScrollBarThumb);
         Drawable upButtonIcon = a.getDrawable(R.styleable.PagedScrollBarView_upButtonIcon);
         if (upButtonIcon != null) {
             setUpButtonIcon(upButtonIcon);
@@ -179,6 +181,20 @@
         mAlphaJumpButton.setVisibility(show ? View.VISIBLE : View.GONE);
     }
 
+    /** Returns {@code true} if the scroll bar thumb is visible */
+    public boolean getShowScrollBarThumb() {
+        return mShowScrollBarThumb;
+    }
+    /**
+     * Sets whether or not the scroll bar thumb is visible, the default value is true.
+     *
+     * @param show {@code true} if the scroll bar thumb is visible.
+     */
+    public void setShowScrollBarThumb(boolean show) {
+        mShowScrollBarThumb = show;
+        mScrollThumb.setVisibility(mShowScrollBarThumb ? View.VISIBLE : View.GONE);
+    }
+
     /**
      * Sets the range, offset and extent of the scroll bar. The range represents the size of a
      * container for the scrollbar thumb; offset is the distance from the start of the container