とりあえずこんな感じでいけるのでは。
java
1public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.CustomViewHolder> {
2 private RecyclerView recyclerView;
3 private List<Item> items;
4
5 public CustomAdapter(RecyclerView recyclerView, List<Item> items) {
6 this.recyclerView = recyclerView;
7 this.items = items;
8 }
9
10 @NonNull
11 @Override
12 public CustomViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
13 LayoutInflater inflater = LayoutInflater.from(parent.getContext());
14 View view = inflater.inflate(R.layout.layout_item, parent, false);
15 return new CustomViewHolder(view);
16 }
17
18 @Override
19 public void onBindViewHolder(@NonNull CustomViewHolder holder, int position) {
20 holder.setItem(items.get(position));
21 }
22
23 @Override
24 public int getItemCount() {
25 return items.size();
26 }
27
28 public void setFocus(int position) {
29 CustomViewHolder holder = (CustomViewHolder) recyclerView.findViewHolderForLayoutPosition(position);
30 if (holder != null) {
31 holder.editText1.requestFocus();
32 }
33 }
34
35 public class CustomViewHolder extends RecyclerView.ViewHolder {
36 private EditText editText1;
37 private EditText editText2;
38
39 public CustomViewHolder(@NonNull View itemView) {
40 super(itemView);
41 editText1 = itemView.findViewById(R.id.editText1);
42 editText2 = itemView.findViewById(R.id.editText2);
43 editText2.setOnEditorActionListener(new TextView.OnEditorActionListener() {
44 @Override
45 public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
46 focusNext();
47 return true;
48 }
49 });
50 }
51
52 public void setItem(Item item) {
53 editText1.setText(item.text1);
54 editText2.setText(item.text2);
55 }
56
57 public void focusNext() {
58 int position = getAdapterPosition() + 1;
59 if (position < items.size()) {
60 setFocus(position);
61 }
62 }
63 }
64}
最後まで行ったら最初に戻るようにするには、次のように変更すると良いでしょう。
diff
1 public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.CustomViewHolder> {
2 private RecyclerView recyclerView;
3+ private LinearLayoutManager layoutManager;
4 private List<Item> items;
5+ private int focusPosition = -1;
6
7- public CustomAdapter(RecyclerView recyclerView, List<Item> items) {
8+ public CustomAdapter(RecyclerView recyclerView, LinearLayoutManager layoutManager, List<Item> items) {
9 this.recyclerView = recyclerView;
10+ this.layoutManager = layoutManager;
11 this.items = items;
12+
13+ recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
14+ @Override
15+ public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
16+ if (newState == RecyclerView.SCROLL_STATE_IDLE && focusPosition >= 0) {
17+ setFocus(focusPosition);
18+ focusPosition = -1;
19+ }
20+ }
21+ });
22 }
23
24 // 略
25
26 public void focusNext() {
27 int position = getAdapterPosition() + 1;
28- if (position < items.size()) {
29+ if (position == items.size()) {
30+ position = 0;
31+ }
32+ if (position >= layoutManager.findFirstCompletelyVisibleItemPosition() &&
33+ position <= layoutManager.findLastCompletelyVisibleItemPosition()) {
34 setFocus(position);
35+ } else {
36+ focusPosition = position;
37+ recyclerView.smoothScrollToPosition(position);
38 }
39 }