Skip to content

Commit 480bbc6

Browse files
manabu-nakamuraleticiarossi
authored andcommittedAug 29, 2023
[Catalog][Carousel] Update slider position when carousel is scrolled in uncontained carousel demo
Resolves #3539 GIT_ORIGIN_REV_ID=c520f2f5fa88a9c08e65e9d3e34db075fdb938dd PiperOrigin-RevId: 560815648
1 parent aa5b5bc commit 480bbc6

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed
 

‎catalog/java/io/material/catalog/carousel/UncontainedCarouselDemoFragment.java

+24-1
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,31 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle bundle) {
103103

104104
CarouselAdapter adapter =
105105
new CarouselAdapter(
106-
(item, position) -> uncontainedRecyclerView.scrollToPosition(position),
106+
(item, position) -> {
107+
uncontainedRecyclerView.scrollToPosition(position);
108+
positionSlider.setValue(position + 1);
109+
},
107110
R.layout.cat_carousel_item_narrow);
111+
uncontainedRecyclerView.addOnScrollListener(
112+
new RecyclerView.OnScrollListener() {
113+
private boolean dragged = false;
114+
115+
@Override
116+
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
117+
if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
118+
dragged = true;
119+
} else if (dragged && newState == RecyclerView.SCROLL_STATE_IDLE) {
120+
if (recyclerView.computeHorizontalScrollRange() != 0) {
121+
positionSlider.setValue(
122+
(adapter.getItemCount() - 1)
123+
* recyclerView.computeHorizontalScrollOffset()
124+
/ recyclerView.computeHorizontalScrollRange()
125+
+ 1);
126+
}
127+
dragged = false;
128+
}
129+
}
130+
});
108131

109132
itemCountDropdown.setOnItemClickListener(
110133
(parent, view1, position, id) ->

0 commit comments

Comments
 (0)