/
MultiBrowseDemoFragment.java
137 lines (117 loc) · 5.23 KB
/
MultiBrowseDemoFragment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* Copyright 2022 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.carousel;
import io.material.catalog.R;
import android.os.Bundle;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.material.carousel.CarouselLayoutManager;
import com.google.android.material.carousel.MultiBrowseCarouselStrategy;
import com.google.android.material.divider.MaterialDividerItemDecoration;
import com.google.android.material.materialswitch.MaterialSwitch;
import com.google.android.material.slider.Slider;
import com.google.android.material.slider.Slider.OnSliderTouchListener;
import io.material.catalog.feature.DemoFragment;
/** A fragment that displays the multi-browse variants of the Carousel. */
public class MultiBrowseDemoFragment extends DemoFragment {
private MaterialDividerItemDecoration horizontalDivider;
@NonNull
@Override
public View onCreateDemoView(
@NonNull LayoutInflater layoutInflater,
@Nullable ViewGroup viewGroup,
@Nullable Bundle bundle) {
return layoutInflater.inflate(
R.layout.cat_carousel_multi_browse_fragment, viewGroup, false /* attachToRoot */);
}
@Override
@SuppressWarnings("RestrictTo")
public void onViewCreated(@NonNull View view, @Nullable Bundle bundle) {
super.onViewCreated(view, bundle);
horizontalDivider =
new MaterialDividerItemDecoration(
requireContext(), MaterialDividerItemDecoration.HORIZONTAL);
MaterialSwitch debugSwitch = view.findViewById(R.id.debug_switch);
MaterialSwitch forceCompactSwitch = view.findViewById(R.id.force_compact_arrangement_switch);
MaterialSwitch drawDividers = view.findViewById(R.id.draw_dividers_switch);
AutoCompleteTextView itemCountDropdown = view.findViewById(R.id.item_count_dropdown);
Slider positionSlider = view.findViewById(R.id.position_slider);
// A start-aligned multi-browse carousel
RecyclerView multiBrowseStartRecyclerView =
view.findViewById(R.id.multi_browse_start_carousel_recycler_view);
CarouselLayoutManager multiBrowseStartCarouselLayoutManager = new CarouselLayoutManager();
multiBrowseStartCarouselLayoutManager.setDebuggingEnabled(
multiBrowseStartRecyclerView, debugSwitch.isChecked());
multiBrowseStartRecyclerView.setLayoutManager(multiBrowseStartCarouselLayoutManager);
multiBrowseStartRecyclerView.setNestedScrollingEnabled(false);
debugSwitch.setOnCheckedChangeListener(
(buttonView, isChecked) -> {
multiBrowseStartRecyclerView.setBackgroundResource(
isChecked ? R.drawable.dashed_outline_rectangle : 0);
multiBrowseStartCarouselLayoutManager.setDebuggingEnabled(
multiBrowseStartRecyclerView, isChecked);
});
forceCompactSwitch.setOnCheckedChangeListener(
(buttonView, isChecked) ->
multiBrowseStartCarouselLayoutManager.setCarouselStrategy(
new MultiBrowseCarouselStrategy(isChecked)));
drawDividers.setOnCheckedChangeListener(
(buttonView, isChecked) -> {
if (isChecked) {
multiBrowseStartRecyclerView.addItemDecoration(horizontalDivider);
} else {
multiBrowseStartRecyclerView.removeItemDecoration(horizontalDivider);
}
});
CarouselAdapter adapter =
new CarouselAdapter(
(item, position) -> multiBrowseStartRecyclerView.scrollToPosition(position));
itemCountDropdown.setOnItemClickListener(
(parent, view1, position, id) -> {
adapter.submitList(
CarouselData.createItems().subList(0, position),
updateSliderRange(positionSlider, adapter));
});
positionSlider.addOnSliderTouchListener(
new OnSliderTouchListener() {
@Override
public void onStartTrackingTouch(@NonNull Slider slider) {}
@Override
public void onStopTrackingTouch(@NonNull Slider slider) {
multiBrowseStartRecyclerView.smoothScrollToPosition((int) slider.getValue() - 1);
}
});
multiBrowseStartRecyclerView.setAdapter(adapter);
adapter.submitList(CarouselData.createItems(), updateSliderRange(positionSlider, adapter));
}
private static Runnable updateSliderRange(Slider slider, CarouselAdapter adapter) {
return () -> {
if (adapter.getItemCount() == 0) {
slider.setValueFrom(0);
slider.setValueTo(0);
return;
}
slider.setValue(1);
slider.setValueFrom(1);
slider.setValueTo(adapter.getItemCount());
};
}
}