teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

不要な import を削除

2019/10/22 09:32

投稿

jimbe
jimbe

スコア13350

answer CHANGED
@@ -37,8 +37,6 @@
37
37
  import android.widget.ScrollView;
38
38
  import android.widget.TextView;
39
39
 
40
- import java.util.Timer;
41
-
42
40
  public class MainActivity extends AppCompatActivity {
43
41
  private ScrollStopDetector scrollStopDetector;
44
42
  @Override

1

コード追加

2019/10/22 09:32

投稿

jimbe
jimbe

スコア13350

answer CHANGED
@@ -1,1 +1,119 @@
1
- 時間の監視でよろしければ, スクロールしたらリセットするタイマーを作り, そのタイマーが1秒経過したら「停止中」と判断しては如何でしょうか.
1
+ 時間の監視でよろしければ, スクロールしたらリセットするタイマーを作り, そのタイマーが1秒経過したら「停止中」と判断しては如何でしょうか.
2
+
3
+ kotlin が使えないため java で失礼します.
4
+ 非常にざっくりです.
5
+
6
+ activity_main.xml
7
+ ```xml
8
+ <?xml version="1.0" encoding="utf-8"?>
9
+ <androidx.constraintlayout.widget.ConstraintLayout
10
+ xmlns:android="http://schemas.android.com/apk/res/android"
11
+ xmlns:tools="http://schemas.android.com/tools"
12
+ android:layout_width="match_parent"
13
+ android:layout_height="match_parent"
14
+ tools:context="com.teratail.q218060.MainActivity">
15
+
16
+ <ScrollView
17
+ android:layout_width="match_parent"
18
+ android:layout_height="match_parent"
19
+ android:id="@+id/scrollView">
20
+
21
+ <TextView
22
+ android:layout_width="match_parent"
23
+ android:layout_height="wrap_content"
24
+ android:id="@+id/textView"/>
25
+ </ScrollView>
26
+ </androidx.constraintlayout.widget.ConstraintLayout>
27
+ ```
28
+ MainActivity.java
29
+ ```java
30
+ package com.teratail.q218060;
31
+
32
+ import androidx.appcompat.app.AppCompatActivity;
33
+
34
+ import android.os.Bundle;
35
+ import android.util.Log;
36
+ import android.view.View;
37
+ import android.widget.ScrollView;
38
+ import android.widget.TextView;
39
+
40
+ import java.util.Timer;
41
+
42
+ public class MainActivity extends AppCompatActivity {
43
+ private ScrollStopDetector scrollStopDetector;
44
+ @Override
45
+ protected void onCreate(Bundle savedInstanceState) {
46
+ super.onCreate(savedInstanceState);
47
+ setContentView(R.layout.activity_main);
48
+
49
+ TextView textView = findViewById(R.id.textView);
50
+ for(int i=1; i<=50; i++) textView.append(i+"\n");
51
+
52
+ ScrollView scrollView = findViewById(R.id.scrollView);
53
+ scrollStopDetector = new ScrollStopDetector(scrollView, 1000, new ScrollStopDetector.Listener() {
54
+ @Override
55
+ public void stopped(View v) {
56
+ Log.d("MainActivity", "Scroll STOP");
57
+ }
58
+ });
59
+ scrollView.setOnScrollChangeListener(scrollStopDetector);
60
+
61
+ scrollStopDetector.start();
62
+ }
63
+ @Override
64
+ protected void onDestroy() {
65
+ super.onDestroy();
66
+ scrollStopDetector.finish();
67
+ }
68
+ }
69
+ ```
70
+ ScrollStopDetector.java
71
+ ```java
72
+ package com.teratail.q218060;
73
+
74
+ import android.view.View;
75
+ import android.widget.ScrollView;
76
+
77
+ class ScrollStopDetector implements View.OnScrollChangeListener {
78
+ interface Listener {
79
+ void stopped(View v);
80
+ }
81
+
82
+ private class Sleeper implements Runnable {
83
+ @Override
84
+ public void run() {
85
+ while(!finish) {
86
+ try {
87
+ Thread.sleep(millis); //[ms]
88
+ listener.stopped(view);
89
+ } catch(InterruptedException ignore) {
90
+ }
91
+ }
92
+ }
93
+ }
94
+
95
+ private ScrollView view;
96
+ private long millis;
97
+ private Listener listener;
98
+ private Thread thread;
99
+ private boolean finish;
100
+
101
+ ScrollStopDetector(ScrollView view, long millis, Listener listener) {
102
+ this.view = view;
103
+ this.millis = millis;
104
+ this.listener = listener;
105
+ }
106
+ void start() {
107
+ thread = new Thread(new Sleeper());
108
+ thread.start();
109
+ }
110
+ void finish() {
111
+ finish = true;
112
+ }
113
+
114
+ @Override
115
+ public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
116
+ if(thread != null) thread.interrupt();
117
+ }
118
+ }
119
+ ```