質問編集履歴
3
コメントを少し削除してimport分を追加しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -17,12 +17,30 @@
|
|
17
17
|
### 該当のソースコード
|
18
18
|
|
19
19
|
```TourokuActivity.java
|
20
|
+
package com.example.furaheal;
|
21
|
+
import android.content.DialogInterface;
|
22
|
+
import android.content.Intent;
|
23
|
+
import android.os.Bundle;
|
24
|
+
import android.view.View;
|
25
|
+
import android.widget.EditText;
|
26
|
+
import androidx.annotation.NonNull;
|
27
|
+
import androidx.appcompat.app.AppCompatActivity;
|
28
|
+
import androidx.recyclerview.widget.RecyclerView;
|
29
|
+
import androidx.recyclerview.widget.LinearLayoutManager;
|
30
|
+
import androidx.recyclerview.widget.ItemTouchHelper;
|
31
|
+
import androidx.recyclerview.widget.ItemTouchHelper.SimpleCallback;
|
32
|
+
import android.app.AlertDialog;
|
33
|
+
import android.content.DialogInterface;
|
34
|
+
import android.widget.EditText;
|
35
|
+
import android.widget.Toast;
|
36
|
+
import java.util.ArrayList;
|
37
|
+
import java.util.Collections;
|
20
38
|
public class TourokuActivity extends AppCompatActivity {
|
21
39
|
// データ格納用のList
|
22
40
|
private ArrayList<String> arrayList;
|
23
|
-
|
41
|
+
|
24
42
|
private SamAdapter adapter;
|
25
|
-
|
43
|
+
|
26
44
|
ItemTouchHelper itemTouchHelper;
|
27
45
|
@Override
|
28
46
|
protected void onCreate(Bundle savedInstanceState) {
|
@@ -31,24 +49,18 @@
|
|
31
49
|
// データ準備
|
32
50
|
arrayList = new ArrayList<>();
|
33
51
|
|
34
|
-
// リサイクラービューへの参照を取得
|
35
52
|
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.mainList);
|
36
|
-
// レイアウトマネージャーを準備
|
37
53
|
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
|
38
|
-
|
54
|
+
|
39
55
|
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
|
40
|
-
|
56
|
+
|
41
57
|
recyclerView.setLayoutManager(layoutManager);
|
42
|
-
// アダプターを生成
|
43
58
|
adapter = new SamAdapter(arrayList);
|
44
|
-
// リサイクラービューにアダプターを設定
|
45
59
|
recyclerView.setAdapter(adapter);
|
46
60
|
|
47
|
-
// リストをクリックしてカレンダーに飛ぶ
|
48
61
|
adapter.setOnItemClickListener(new View.OnClickListener() {
|
49
62
|
@Override
|
50
63
|
public void onClick(View v) {
|
51
|
-
// クリックされたリストアイテムの位置を取得
|
52
64
|
int position = recyclerView.getChildAdapterPosition(v);
|
53
65
|
|
54
66
|
// クリックされたアイテムのデータを取得
|
@@ -68,16 +80,13 @@
|
|
68
80
|
itemTouchHelper = new ItemTouchHelper(
|
69
81
|
new SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN ,
|
70
82
|
ItemTouchHelper.LEFT){
|
71
|
-
// 長押しで移動
|
72
83
|
@Override
|
73
84
|
public boolean onMove(@NonNull RecyclerView recyclerView,
|
74
85
|
@NonNull RecyclerView.ViewHolder viewHolder,
|
75
86
|
@NonNull RecyclerView.ViewHolder target) {
|
76
87
|
final int fromPos = viewHolder.getAdapterPosition();
|
77
88
|
final int toPos = target.getAdapterPosition();
|
78
|
-
// データを入れ替え
|
79
89
|
Collections.swap(arrayList, fromPos, toPos);
|
80
|
-
// 移動したことを通知
|
81
90
|
adapter.notifyItemMoved(fromPos, toPos);
|
82
91
|
return true;
|
83
92
|
}
|
@@ -102,7 +111,6 @@
|
|
102
111
|
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
103
112
|
builder.setTitle("名前を設定");
|
104
113
|
|
105
|
-
// ダイアログに表示するViewを作成
|
106
114
|
final EditText input = new EditText(this);
|
107
115
|
input.setHint("名前を入力してください");
|
108
116
|
builder.setView(input);
|
@@ -136,6 +144,29 @@
|
|
136
144
|
|
137
145
|
|
138
146
|
```SamAdapter.java
|
147
|
+
package com.example.furaheal;
|
148
|
+
|
149
|
+
import android.content.Intent;
|
150
|
+
import android.text.Editable;
|
151
|
+
import android.text.TextWatcher;
|
152
|
+
import android.view.View;
|
153
|
+
import android.view.ViewGroup;
|
154
|
+
import android.view.LayoutInflater;
|
155
|
+
import android.view.MotionEvent;
|
156
|
+
import android.widget.EditText;
|
157
|
+
import android.widget.ImageButton;
|
158
|
+
import android.annotation.SuppressLint;
|
159
|
+
import android.widget.LinearLayout;
|
160
|
+
import android.widget.TextView;
|
161
|
+
|
162
|
+
import androidx.annotation.NonNull;
|
163
|
+
import androidx.recyclerview.widget.RecyclerView;
|
164
|
+
|
165
|
+
import com.example.furaheal.CalenderActivity;
|
166
|
+
|
167
|
+
|
168
|
+
import java.util.List;
|
169
|
+
|
139
170
|
public class SamAdapter extends RecyclerView.Adapter<SamAdapter.SampViewHolder>{
|
140
171
|
|
141
172
|
private static List<String> arrayList;
|
@@ -218,7 +249,6 @@
|
|
218
249
|
|
219
250
|
}
|
220
251
|
|
221
|
-
// アイテム数を取得
|
222
252
|
@Override
|
223
253
|
public int getItemCount() {
|
224
254
|
return arrayList.size();
|
@@ -228,14 +258,13 @@
|
|
228
258
|
public static class SampViewHolder extends RecyclerView.ViewHolder {
|
229
259
|
|
230
260
|
// ビューに配置されたウィジェットへの参照を保持しておくためのフィールド
|
231
|
-
public TextView text_contents;
|
261
|
+
public TextView text_contents;
|
232
|
-
public ImageButton btn_move;
|
262
|
+
public ImageButton btn_move;
|
233
|
-
public ImageButton btn_del;
|
263
|
+
public ImageButton btn_del;
|
234
264
|
private final LinearLayout linearLayout;
|
235
265
|
|
236
266
|
// テキストウォッチャー
|
237
267
|
public TextWatcher textWatcher;
|
238
|
-
// ViewHolder内にviewHolderフィールドを追加
|
239
268
|
private SampViewHolder viewHolder;
|
240
269
|
TextView textView;
|
241
270
|
|
@@ -255,7 +284,7 @@
|
|
255
284
|
|
256
285
|
|
257
286
|
|
258
|
-
viewHolder = this;
|
287
|
+
viewHolder = this;
|
259
288
|
|
260
289
|
|
261
290
|
textView = itemView.findViewById(R.id.text_contents);
|
2
削除ボタンが二つあったため
title
CHANGED
File without changes
|
body
CHANGED
@@ -206,18 +206,6 @@
|
|
206
206
|
}
|
207
207
|
});
|
208
208
|
|
209
|
-
// 削除ボタンをクリック
|
210
|
-
holder.btn_del.setOnClickListener(new View.OnClickListener() {
|
211
|
-
@Override
|
212
|
-
public void onClick(View view) {
|
213
|
-
int adapterPosition = holder.getAdapterPosition();
|
214
|
-
if (adapterPosition != -1) {
|
215
|
-
arrayList.remove(adapterPosition);
|
216
|
-
notifyItemRemoved(adapterPosition);
|
217
|
-
}
|
218
|
-
}
|
219
|
-
});
|
220
|
-
|
221
209
|
holder.getLinearLayout().setOnClickListener(new View.OnClickListener() {
|
222
210
|
@Override
|
223
211
|
public void onClick(View view) {
|
1
修正中にAdapterの中ではなく、activityに対応したClassに書くことに気づいたので、コードを少し修正しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -16,71 +16,131 @@
|
|
16
16
|
|
17
17
|
### 該当のソースコード
|
18
18
|
|
19
|
-
```
|
19
|
+
```TourokuActivity.java
|
20
|
-
|
20
|
+
public class TourokuActivity extends AppCompatActivity {
|
21
|
-
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
22
|
-
xmlns:app="http://schemas.android.com/apk/res-auto"
|
23
|
-
|
21
|
+
// データ格納用のList
|
24
|
-
android:layout_width="match_parent"
|
25
|
-
|
22
|
+
private ArrayList<String> arrayList;
|
23
|
+
// アダプター
|
24
|
+
private SamAdapter adapter;
|
25
|
+
// ドラッグアンドドロップなどをするためのユーティリティクラス
|
26
|
+
ItemTouchHelper itemTouchHelper;
|
27
|
+
@Override
|
28
|
+
protected void onCreate(Bundle savedInstanceState) {
|
29
|
+
super.onCreate(savedInstanceState);
|
26
|
-
|
30
|
+
setContentView(R.layout.activity_touroku);
|
31
|
+
// データ準備
|
32
|
+
arrayList = new ArrayList<>();
|
27
33
|
|
34
|
+
// リサイクラービューへの参照を取得
|
35
|
+
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.mainList);
|
36
|
+
// レイアウトマネージャーを準備
|
37
|
+
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
|
38
|
+
// レイアウトマネージャーを縦スクロールに設定
|
39
|
+
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
|
40
|
+
// リサイクラービューにレイアウトマネージャーを設定
|
41
|
+
recyclerView.setLayoutManager(layoutManager);
|
42
|
+
// アダプターを生成
|
43
|
+
adapter = new SamAdapter(arrayList);
|
44
|
+
// リサイクラービューにアダプターを設定
|
28
|
-
|
45
|
+
recyclerView.setAdapter(adapter);
|
29
|
-
android:id="@+id/mainList"
|
30
|
-
android:layout_width="match_parent"
|
31
|
-
android:layout_height="match_parent"
|
32
|
-
app:layout_constraintBottom_toBottomOf="parent"
|
33
|
-
app:layout_constraintEnd_toEndOf="parent"
|
34
|
-
app:layout_constraintStart_toStartOf="parent"
|
35
|
-
app:layout_constraintTop_toTopOf="parent" />
|
36
46
|
|
37
|
-
<com.google.android.material.floatingactionbutton.FloatingActionButton
|
38
|
-
android:id="@+id/fab_reg"
|
39
|
-
android:layout_width="wrap_content"
|
40
|
-
android:layout_height="wrap_content"
|
41
|
-
android:layout_marginBottom="8dp"
|
42
|
-
android:layout_marginEnd="8dp"
|
43
|
-
|
47
|
+
// リストをクリックしてカレンダーに飛ぶ
|
44
|
-
android:clickable="true"
|
45
|
-
|
48
|
+
adapter.setOnItemClickListener(new View.OnClickListener() {
|
49
|
+
@Override
|
46
|
-
|
50
|
+
public void onClick(View v) {
|
47
|
-
app:srcCompat="@drawable/ic_baseline_add_24"
|
48
|
-
|
51
|
+
// クリックされたリストアイテムの位置を取得
|
49
|
-
app:backgroundTint="@color/purple_200"
|
50
|
-
|
52
|
+
int position = recyclerView.getChildAdapterPosition(v);
|
51
|
-
app:layout_constraintEnd_toEndOf="parent" />
|
52
53
|
|
54
|
+
// クリックされたアイテムのデータを取得
|
53
|
-
|
55
|
+
String clickedItem = arrayList.get(position);
|
54
|
-
```
|
55
56
|
|
57
|
+
// 別のActivityに遷移するためのIntentを作成し、データを渡す
|
58
|
+
Intent intent = new Intent(TourokuActivity.this, CalenderActivity.class);
|
59
|
+
intent.putExtra("item", clickedItem);
|
60
|
+
startActivity(intent);
|
61
|
+
|
62
|
+
}
|
63
|
+
});
|
56
64
|
|
57
|
-
```SamAdapter.java
|
58
|
-
package com.example.furaheal;
|
59
65
|
|
60
|
-
import android.content.Intent;
|
61
|
-
import android.text.Editable;
|
62
|
-
import android.text.TextWatcher;
|
63
|
-
import android.view.View;
|
64
|
-
import android.view.ViewGroup;
|
65
|
-
import android.view.LayoutInflater;
|
66
|
-
import android.view.MotionEvent;
|
67
|
-
import android.widget.EditText;
|
68
|
-
import android.widget.ImageButton;
|
69
|
-
import android.annotation.SuppressLint;
|
70
|
-
import android.widget.TextView;
|
71
66
|
|
67
|
+
// ドラッグアンドドロップで移動
|
72
|
-
|
68
|
+
itemTouchHelper = new ItemTouchHelper(
|
69
|
+
new SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN ,
|
70
|
+
ItemTouchHelper.LEFT){
|
71
|
+
// 長押しで移動
|
72
|
+
@Override
|
73
|
+
public boolean onMove(@NonNull RecyclerView recyclerView,
|
74
|
+
@NonNull RecyclerView.ViewHolder viewHolder,
|
75
|
+
@NonNull RecyclerView.ViewHolder target) {
|
76
|
+
final int fromPos = viewHolder.getAdapterPosition();
|
77
|
+
final int toPos = target.getAdapterPosition();
|
78
|
+
// データを入れ替え
|
79
|
+
Collections.swap(arrayList, fromPos, toPos);
|
80
|
+
// 移動したことを通知
|
81
|
+
adapter.notifyItemMoved(fromPos, toPos);
|
82
|
+
return true;
|
83
|
+
}
|
84
|
+
// スワイプで削除
|
85
|
+
@Override
|
86
|
+
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
|
87
|
+
// アイテムを削除
|
88
|
+
arrayList.remove(viewHolder.getAdapterPosition());
|
89
|
+
// 削除したことを通知
|
90
|
+
adapter.notifyItemRemoved(viewHolder.getAdapterPosition());
|
91
|
+
}
|
92
|
+
});
|
93
|
+
// ItemTouchHelper を RecyclerView にアタッチ
|
73
|
-
|
94
|
+
itemTouchHelper.attachToRecyclerView(recyclerView);
|
74
95
|
|
75
|
-
import com.example.furaheal.CalenderActivity;
|
76
96
|
|
97
|
+
}
|
77
98
|
|
78
|
-
import java.util.List;
|
79
99
|
|
100
|
+
// 「+」フローティング操作ボタンがタップされたときに実行される
|
101
|
+
public void onAddItem(View view) {
|
102
|
+
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
103
|
+
builder.setTitle("名前を設定");
|
104
|
+
|
105
|
+
// ダイアログに表示するViewを作成
|
106
|
+
final EditText input = new EditText(this);
|
107
|
+
input.setHint("名前を入力してください");
|
108
|
+
builder.setView(input);
|
109
|
+
|
110
|
+
// ダイアログの追加ボタンが押されたときの処理
|
111
|
+
builder.setPositiveButton("追加", new DialogInterface.OnClickListener() {
|
112
|
+
@Override
|
113
|
+
public void onClick(DialogInterface dialog, int which) {
|
114
|
+
String newName = input.getText().toString();
|
115
|
+
if (!newName.isEmpty()) {
|
116
|
+
arrayList.add(newName);
|
117
|
+
adapter.notifyItemInserted(arrayList.size() - 1);
|
118
|
+
}
|
119
|
+
}
|
120
|
+
});
|
121
|
+
|
122
|
+
// ダイアログのキャンセルボタンが押されたときの処理
|
123
|
+
builder.setNegativeButton("キャンセル", new DialogInterface.OnClickListener() {
|
124
|
+
@Override
|
125
|
+
public void onClick(DialogInterface dialog, int which) {
|
126
|
+
dialog.cancel();
|
127
|
+
}
|
128
|
+
});
|
129
|
+
|
130
|
+
// ダイアログを表示
|
131
|
+
builder.show();
|
132
|
+
}
|
133
|
+
|
134
|
+
}
|
135
|
+
```
|
136
|
+
|
137
|
+
|
138
|
+
```SamAdapter.java
|
80
139
|
public class SamAdapter extends RecyclerView.Adapter<SamAdapter.SampViewHolder>{
|
81
140
|
|
82
141
|
private static List<String> arrayList;
|
83
142
|
private static TourokuActivity activity;
|
143
|
+
private View.OnClickListener listener;
|
84
144
|
|
85
145
|
// アダプターのコンストラクタ
|
86
146
|
SamAdapter(List<String> arrayList) {
|
@@ -92,6 +152,8 @@
|
|
92
152
|
@Override
|
93
153
|
public SampViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
94
154
|
|
155
|
+
|
156
|
+
|
95
157
|
// レイアウトファイルに対応したViewオブジェクトを生成
|
96
158
|
View view = LayoutInflater.from(parent.getContext())
|
97
159
|
.inflate(R.layout.row_main, parent, false);
|
@@ -144,7 +206,28 @@
|
|
144
206
|
}
|
145
207
|
});
|
146
208
|
|
209
|
+
// 削除ボタンをクリック
|
210
|
+
holder.btn_del.setOnClickListener(new View.OnClickListener() {
|
211
|
+
@Override
|
212
|
+
public void onClick(View view) {
|
213
|
+
int adapterPosition = holder.getAdapterPosition();
|
214
|
+
if (adapterPosition != -1) {
|
215
|
+
arrayList.remove(adapterPosition);
|
216
|
+
notifyItemRemoved(adapterPosition);
|
217
|
+
}
|
218
|
+
}
|
219
|
+
});
|
147
220
|
|
221
|
+
holder.getLinearLayout().setOnClickListener(new View.OnClickListener() {
|
222
|
+
@Override
|
223
|
+
public void onClick(View view) {
|
224
|
+
listener.onClick(view);
|
225
|
+
}
|
226
|
+
});
|
227
|
+
|
228
|
+
|
229
|
+
|
230
|
+
|
148
231
|
}
|
149
232
|
|
150
233
|
// アイテム数を取得
|
@@ -160,6 +243,7 @@
|
|
160
243
|
public TextView text_contents; // リストの内容
|
161
244
|
public ImageButton btn_move; // 移動ボタン
|
162
245
|
public ImageButton btn_del; // 削除ボタン
|
246
|
+
private final LinearLayout linearLayout;
|
163
247
|
|
164
248
|
// テキストウォッチャー
|
165
249
|
public TextWatcher textWatcher;
|
@@ -169,6 +253,8 @@
|
|
169
253
|
|
170
254
|
|
171
255
|
|
256
|
+
|
257
|
+
|
172
258
|
// ビューホルダーのコンストラクタ
|
173
259
|
public SampViewHolder(View view) {
|
174
260
|
super(view);
|
@@ -177,6 +263,7 @@
|
|
177
263
|
text_contents = (TextView) view.findViewById(R.id.text_contents);
|
178
264
|
btn_move = (ImageButton) view.findViewById(R.id.btn_move);
|
179
265
|
btn_del = (ImageButton) view.findViewById(R.id.btn_del);
|
266
|
+
linearLayout = (LinearLayout) view.findViewById(R.id.listLinearLayout);
|
180
267
|
|
181
268
|
|
182
269
|
|
@@ -185,25 +272,10 @@
|
|
185
272
|
|
186
273
|
textView = itemView.findViewById(R.id.text_contents);
|
187
274
|
|
188
|
-
// ここでうまくいっていません
|
189
|
-
// リストをクリックしてカレンダーに飛ぶ
|
190
|
-
itemView.setOnClickListener(new View.OnClickListener() {
|
191
|
-
|
275
|
+
}
|
192
|
-
public void onClick(View v) {
|
193
|
-
// クリックされたリストアイテムの位置を取得
|
194
|
-
int position = getAdapterPosition();
|
195
276
|
|
196
|
-
// クリックされたアイテムのデータを取得
|
197
|
-
|
277
|
+
public LinearLayout getLinearLayout() {
|
198
|
-
|
199
|
-
// 別のActivityに遷移するためのIntentを作成し、データを渡す
|
200
|
-
Intent intent = new Intent(activity, CalenderActivity.class);
|
201
|
-
intent.putExtra("item", clickedItem);
|
202
|
-
|
278
|
+
return linearLayout;
|
203
|
-
}
|
204
|
-
});
|
205
|
-
|
206
|
-
|
207
279
|
}
|
208
280
|
|
209
281
|
|
@@ -225,12 +297,16 @@
|
|
225
297
|
}
|
226
298
|
};
|
227
299
|
}
|
300
|
+
|
301
|
+
public void setOnItemClickListener(View.OnClickListener listener) {
|
302
|
+
this.listener = listener;
|
303
|
+
}
|
228
304
|
}
|
229
|
-
|
230
305
|
```
|
231
306
|
### 試したこと
|
232
|
-
SamAdapterの
|
307
|
+
SamAdapterのOnclickの部分を変えたりしています。
|
233
308
|
### 補足情報(FW/ツールのバージョンなど)
|
234
309
|
|
235
310
|
android studio 言語はjava
|
236
311
|
APIレベル28で実行しています
|
312
|
+
文字数制限の関係でimport分は省略しています。
|