回答編集履歴

2

不要な import を削除

2019/10/22 09:32

投稿

jimbe
jimbe

スコア12632

test CHANGED
@@ -76,10 +76,6 @@
76
76
 
77
77
 
78
78
 
79
- import java.util.Timer;
80
-
81
-
82
-
83
79
  public class MainActivity extends AppCompatActivity {
84
80
 
85
81
  private ScrollStopDetector scrollStopDetector;

1

コード追加

2019/10/22 09:32

投稿

jimbe
jimbe

スコア12632

test CHANGED
@@ -1 +1,237 @@
1
1
  時間の監視でよろしければ, スクロールしたらリセットするタイマーを作り, そのタイマーが1秒経過したら「停止中」と判断しては如何でしょうか.
2
+
3
+
4
+
5
+ kotlin が使えないため java で失礼します.
6
+
7
+ 非常にざっくりです.
8
+
9
+
10
+
11
+ activity_main.xml
12
+
13
+ ```xml
14
+
15
+ <?xml version="1.0" encoding="utf-8"?>
16
+
17
+ <androidx.constraintlayout.widget.ConstraintLayout
18
+
19
+ xmlns:android="http://schemas.android.com/apk/res/android"
20
+
21
+ xmlns:tools="http://schemas.android.com/tools"
22
+
23
+ android:layout_width="match_parent"
24
+
25
+ android:layout_height="match_parent"
26
+
27
+ tools:context="com.teratail.q218060.MainActivity">
28
+
29
+
30
+
31
+ <ScrollView
32
+
33
+ android:layout_width="match_parent"
34
+
35
+ android:layout_height="match_parent"
36
+
37
+ android:id="@+id/scrollView">
38
+
39
+
40
+
41
+ <TextView
42
+
43
+ android:layout_width="match_parent"
44
+
45
+ android:layout_height="wrap_content"
46
+
47
+ android:id="@+id/textView"/>
48
+
49
+ </ScrollView>
50
+
51
+ </androidx.constraintlayout.widget.ConstraintLayout>
52
+
53
+ ```
54
+
55
+ MainActivity.java
56
+
57
+ ```java
58
+
59
+ package com.teratail.q218060;
60
+
61
+
62
+
63
+ import androidx.appcompat.app.AppCompatActivity;
64
+
65
+
66
+
67
+ import android.os.Bundle;
68
+
69
+ import android.util.Log;
70
+
71
+ import android.view.View;
72
+
73
+ import android.widget.ScrollView;
74
+
75
+ import android.widget.TextView;
76
+
77
+
78
+
79
+ import java.util.Timer;
80
+
81
+
82
+
83
+ public class MainActivity extends AppCompatActivity {
84
+
85
+ private ScrollStopDetector scrollStopDetector;
86
+
87
+ @Override
88
+
89
+ protected void onCreate(Bundle savedInstanceState) {
90
+
91
+ super.onCreate(savedInstanceState);
92
+
93
+ setContentView(R.layout.activity_main);
94
+
95
+
96
+
97
+ TextView textView = findViewById(R.id.textView);
98
+
99
+ for(int i=1; i<=50; i++) textView.append(i+"\n");
100
+
101
+
102
+
103
+ ScrollView scrollView = findViewById(R.id.scrollView);
104
+
105
+ scrollStopDetector = new ScrollStopDetector(scrollView, 1000, new ScrollStopDetector.Listener() {
106
+
107
+ @Override
108
+
109
+ public void stopped(View v) {
110
+
111
+ Log.d("MainActivity", "Scroll STOP");
112
+
113
+ }
114
+
115
+ });
116
+
117
+ scrollView.setOnScrollChangeListener(scrollStopDetector);
118
+
119
+
120
+
121
+ scrollStopDetector.start();
122
+
123
+ }
124
+
125
+ @Override
126
+
127
+ protected void onDestroy() {
128
+
129
+ super.onDestroy();
130
+
131
+ scrollStopDetector.finish();
132
+
133
+ }
134
+
135
+ }
136
+
137
+ ```
138
+
139
+ ScrollStopDetector.java
140
+
141
+ ```java
142
+
143
+ package com.teratail.q218060;
144
+
145
+
146
+
147
+ import android.view.View;
148
+
149
+ import android.widget.ScrollView;
150
+
151
+
152
+
153
+ class ScrollStopDetector implements View.OnScrollChangeListener {
154
+
155
+ interface Listener {
156
+
157
+ void stopped(View v);
158
+
159
+ }
160
+
161
+
162
+
163
+ private class Sleeper implements Runnable {
164
+
165
+ @Override
166
+
167
+ public void run() {
168
+
169
+ while(!finish) {
170
+
171
+ try {
172
+
173
+ Thread.sleep(millis); //[ms]
174
+
175
+ listener.stopped(view);
176
+
177
+ } catch(InterruptedException ignore) {
178
+
179
+ }
180
+
181
+ }
182
+
183
+ }
184
+
185
+ }
186
+
187
+
188
+
189
+ private ScrollView view;
190
+
191
+ private long millis;
192
+
193
+ private Listener listener;
194
+
195
+ private Thread thread;
196
+
197
+ private boolean finish;
198
+
199
+
200
+
201
+ ScrollStopDetector(ScrollView view, long millis, Listener listener) {
202
+
203
+ this.view = view;
204
+
205
+ this.millis = millis;
206
+
207
+ this.listener = listener;
208
+
209
+ }
210
+
211
+ void start() {
212
+
213
+ thread = new Thread(new Sleeper());
214
+
215
+ thread.start();
216
+
217
+ }
218
+
219
+ void finish() {
220
+
221
+ finish = true;
222
+
223
+ }
224
+
225
+
226
+
227
+ @Override
228
+
229
+ public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
230
+
231
+ if(thread != null) thread.interrupt();
232
+
233
+ }
234
+
235
+ }
236
+
237
+ ```