Skip to content

[Slider] Fixed behaviour when Slider is in a scrolling container #4511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions catalog/java/io/material/catalog/slider/SliderFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ public Fragment createFragment() {
return new SliderVerticalDemoFragment();
}
});
additionalDemos.add(
new Demo(R.string.cat_slider_demo_vertical_scroll_container_title) {
@Override
public Fragment createFragment() {
return new SliderVerticalScrollContainerDemoFragment();
}
});
return additionalDemos;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.material.catalog.slider;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.google.android.material.slider.Slider;
import com.google.android.material.slider.SliderOrientation;

import io.material.catalog.R;
import io.material.catalog.feature.DemoFragment;

/**
* A fragment that displays a list of vertical {@link Slider} inside a HorizontalScrollView to
* showcase how touch events are handled.
*/
public class SliderVerticalScrollContainerDemoFragment extends DemoFragment {

@Override
public View onCreateDemoView(
LayoutInflater layoutInflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle) {
View view =
layoutInflater.inflate(
R.layout.cat_slider_demo_vertical_scroll, viewGroup, false /* attachToRoot */);
LinearLayout sliderContainer = view.findViewById(R.id.sliderContainer);
for (int i = 0; i < 50; i++) {
Slider slider = new Slider(layoutInflater.getContext());
slider.setValueTo(11f);
slider.setOrientation(SliderOrientation.VERTICAL);
sliderContainer.addView(
slider,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.MATCH_PARENT);
}
return view;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2019 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:id="@+id/sliderContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp">


</LinearLayout>
</HorizontalScrollView>
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<string name="cat_slider_demo_corner_title" description="Title for the Slider with different corner sizes demo [CHAR LIMIT=NONE]">Slider custom corners demo</string>
<string name="cat_slider_demo_icon_title" description="Title for the Slider with different track icons demo [CHAR LIMIT=NONE]">Slider track icons demo</string>
<string name="cat_slider_demo_vertical_title" description="Title for the vertical Slider demo [CHAR LIMIT=NONE]">Slider vertical demo</string>
<string name="cat_slider_demo_vertical_scroll_container_title" description="Title for the vertical Slider inside scrolling container demo [CHAR LIMIT=NONE]">Vertical Slider in scrolling container demo</string>

<string name="cat_slider_description" description="Body text describing the Slider widget within the design system [CHAR LIMIT=NONE]">
Sliders let users select from a range of values by moving the slider thumb.
Expand Down
42 changes: 36 additions & 6 deletions lib/java/com/google/android/material/slider/BaseSlider.java
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ abstract class BaseSlider<
@Px private int trackIconSize;
private int labelPadding;
private float touchDownX;
private float touchDownY;
private MotionEvent lastEvent;
private LabelFormatter formatter;
private boolean thumbIsPressed = false;
Expand Down Expand Up @@ -2863,18 +2864,25 @@ public boolean onTouchEvent(@NonNull MotionEvent event) {
return false;
}

float eventCoordinate = isVertical() ? event.getY() : event.getX();
touchPosition = (eventCoordinate - trackSidePadding) / trackWidth;
float eventCoordinateX = isVertical() ? event.getY() : event.getX();
float eventCoordinateY = isVertical() ? event.getX() : event.getY();
touchPosition = (eventCoordinateX - trackSidePadding) / trackWidth;
touchPosition = max(0, touchPosition);
touchPosition = min(1, touchPosition);

switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
touchDownX = eventCoordinate;
touchDownX = eventCoordinateX;
touchDownY = eventCoordinateY;

// If we're inside a vertical scrolling container,
// we should start dragging in ACTION_MOVE
if (isPotentialVerticalScroll(event)) {
if (!isVertical() && isPotentialVerticalScroll(event)) {
break;
}
// If we're inside a horizontal scrolling container,
// we should start dragging in ACTION_MOVE
if (isVertical() && isPotentialHorizontalScroll(event)) {
break;
}

Expand All @@ -2897,8 +2905,13 @@ public boolean onTouchEvent(@NonNull MotionEvent event) {
case MotionEvent.ACTION_MOVE:
if (!thumbIsPressed) {
// Check if we're trying to scroll vertically instead of dragging this Slider
if (isPotentialVerticalScroll(event)
&& abs(eventCoordinate - touchDownX) < scaledTouchSlop) {
if (!isVertical() && isPotentialVerticalScroll(event)
&& abs(eventCoordinateX - touchDownX) < scaledTouchSlop) {
return false;
}
// Check if we're trying to scroll horizontally instead of dragging this Slider
if (isVertical() && isPotentialHorizontalScroll(event)
&& abs(eventCoordinateY - touchDownY) < scaledTouchSlop / 1.2) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain this 1.2?

Copy link
Contributor Author

@manabu-nakamura manabu-nakamura Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is difficult for me to explain it...
If scaledTouchSlop is not divided by 1.2, it is difficult to slide (drag) vertical sliders. Try it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this also applicable for the vertical scroll or is it specific to this horizontal scroll?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to be specific to horizontal scroll. Don't you think so?

return false;
}
getParent().requestDisallowInterceptTouchEvent(true);
Expand Down Expand Up @@ -3353,6 +3366,19 @@ private boolean isInVerticalScrollingContainer() {
return false;
}

private boolean isInHorizontalScrollingContainer() {
ViewParent p = getParent();
while (p instanceof ViewGroup) {
ViewGroup parent = (ViewGroup) p;
boolean canScrollHorizontally = parent.canScrollHorizontally(1) || parent.canScrollHorizontally(-1);
if (canScrollHorizontally && parent.shouldDelayChildPressedState()) {
return true;
}
p = p.getParent();
}
return false;
}

private static boolean isMouseEvent(MotionEvent event) {
return event.getToolType(0) == MotionEvent.TOOL_TYPE_MOUSE;
}
Expand All @@ -3361,6 +3387,10 @@ private boolean isPotentialVerticalScroll(MotionEvent event) {
return !isMouseEvent(event) && isInVerticalScrollingContainer();
}

private boolean isPotentialHorizontalScroll(MotionEvent event) {
return !isMouseEvent(event) && isInHorizontalScrollingContainer();
}

@SuppressWarnings("unchecked")
private void dispatchOnChangedProgrammatically() {
for (L listener : changeListeners) {
Expand Down