回答編集履歴

1

追記

2021/05/26 16:16

投稿

hoshi-takanori
hoshi-takanori

スコア7899

test CHANGED
@@ -133,3 +133,91 @@
133
133
  }
134
134
 
135
135
  ```
136
+
137
+
138
+
139
+ 最後まで行ったら最初に戻るようにするには、次のように変更すると良いでしょう。
140
+
141
+
142
+
143
+ ```diff
144
+
145
+ public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.CustomViewHolder> {
146
+
147
+ private RecyclerView recyclerView;
148
+
149
+ + private LinearLayoutManager layoutManager;
150
+
151
+ private List<Item> items;
152
+
153
+ + private int focusPosition = -1;
154
+
155
+
156
+
157
+ - public CustomAdapter(RecyclerView recyclerView, List<Item> items) {
158
+
159
+ + public CustomAdapter(RecyclerView recyclerView, LinearLayoutManager layoutManager, List<Item> items) {
160
+
161
+ this.recyclerView = recyclerView;
162
+
163
+ + this.layoutManager = layoutManager;
164
+
165
+ this.items = items;
166
+
167
+ +
168
+
169
+ + recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
170
+
171
+ + @Override
172
+
173
+ + public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
174
+
175
+ + if (newState == RecyclerView.SCROLL_STATE_IDLE && focusPosition >= 0) {
176
+
177
+ + setFocus(focusPosition);
178
+
179
+ + focusPosition = -1;
180
+
181
+ + }
182
+
183
+ + }
184
+
185
+ + });
186
+
187
+ }
188
+
189
+
190
+
191
+ // 略
192
+
193
+
194
+
195
+ public void focusNext() {
196
+
197
+ int position = getAdapterPosition() + 1;
198
+
199
+ - if (position < items.size()) {
200
+
201
+ + if (position == items.size()) {
202
+
203
+ + position = 0;
204
+
205
+ + }
206
+
207
+ + if (position >= layoutManager.findFirstCompletelyVisibleItemPosition() &&
208
+
209
+ + position <= layoutManager.findLastCompletelyVisibleItemPosition()) {
210
+
211
+ setFocus(position);
212
+
213
+ + } else {
214
+
215
+ + focusPosition = position;
216
+
217
+ + recyclerView.smoothScrollToPosition(position);
218
+
219
+ }
220
+
221
+ }
222
+
223
+ ```