2
2
3
3
import static androidx .core .app .ActivityCompat .finishAffinity ;
4
4
5
+ import android .annotation .SuppressLint ;
5
6
import android .os .Bundle ;
7
+ import android .view .GestureDetector ;
6
8
import android .view .LayoutInflater ;
9
+ import android .view .MotionEvent ;
7
10
import android .view .View ;
8
11
import android .view .ViewGroup ;
9
12
import android .widget .TextView ;
12
15
import android .widget .ProgressBar ;
13
16
14
17
import androidx .annotation .NonNull ;
18
+ import androidx .annotation .Nullable ;
15
19
import androidx .fragment .app .Fragment ;
20
+ import androidx .navigation .NavOptions ;
16
21
import androidx .navigation .fragment .NavHostFragment ;
22
+ import androidx .swiperefreshlayout .widget .SwipeRefreshLayout ;
17
23
18
24
import com .example .one .databinding .FragmentFirstBinding ;
19
25
22
28
import okhttp3 .OkHttpClient ;
23
29
import okhttp3 .Request ;
24
30
import okhttp3 .Response ;
31
+
25
32
import org .json .JSONObject ;
33
+
26
34
import java .io .IOException ;
27
35
28
36
import android .util .Log ;
37
+
29
38
import java .nio .charset .StandardCharsets ;
30
39
31
40
import android .os .CountDownTimer ;
32
41
33
- public class FirstFragment extends Fragment {
42
+ public class FirstFragment extends Fragment implements GestureDetector . OnGestureListener { // 实现 OnGestureListener 接口
34
43
35
44
private FragmentFirstBinding binding ;
36
45
private OkHttpClient client = new OkHttpClient ();
@@ -39,17 +48,37 @@ public class FirstFragment extends Fragment {
39
48
private TextView hitokotoFrom ;
40
49
private TextView hitokotoFromWho ;
41
50
42
- private ProgressBar progressBarCountdown ;
43
- private Button buttonToggle ;
44
- private boolean isAutoFetchEnabled = false ;
45
- private CountDownTimer countDownTimer ;
51
+ // private ProgressBar progressBarCountdown;
52
+ // private Button buttonToggle;
53
+ // private boolean isAutoFetchEnabled = false;
54
+ // private CountDownTimer countDownTimer;
55
+
56
+
57
+ private androidx .swiperefreshlayout .widget .SwipeRefreshLayout swipeRefreshLayout ;
58
+ private GestureDetector gestureDetector ;
59
+
60
+ private static final int SWIPE_THRESHOLD = 50 ;
61
+ private static final int SWIPE_VELOCITY_THRESHOLD = 50 ;
46
62
47
63
@ Override
48
64
public View onCreateView (
49
65
@ NonNull LayoutInflater inflater , ViewGroup container ,
50
66
Bundle savedInstanceState
51
67
) {
52
68
binding = FragmentFirstBinding .inflate (inflater , container , false );
69
+
70
+ // 获取 SwipeRefreshLayout 实例
71
+ swipeRefreshLayout = binding .getRoot ().findViewById (R .id .swipeRefreshLayout1 ); // Assuming you added SwipeRefreshLayout in your XML
72
+ swipeRefreshLayout .setOnRefreshListener (new SwipeRefreshLayout .OnRefreshListener () {
73
+ @ Override
74
+ public void onRefresh () {
75
+ // This method is called when the user pulls down to refresh
76
+ // Perform the refresh operation here
77
+ fetchHitokoto ();
78
+ }
79
+ });
80
+
81
+
53
82
return binding .getRoot ();
54
83
}
55
84
@@ -61,66 +90,76 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
61
90
hitokotoType = binding .hitokotoType ;
62
91
hitokotoFrom = binding .hitokotoFrom ;
63
92
hitokotoFromWho = binding .hitokotoFromWho ;
64
- progressBarCountdown = view .findViewById (R .id .progressBar_auto_1 );
65
- buttonToggle = view .findViewById (R .id .button_auto_1 );
66
-
67
93
68
- binding .buttonSecond1 .setOnClickListener (v ->
69
- NavHostFragment .findNavController (FirstFragment .this )
70
- .navigate (R .id .action_to_SecondFragment )
71
- );
72
94
73
- binding .buttonThird1 .setOnClickListener (v ->
74
- NavHostFragment .findNavController (FirstFragment .this )
75
- .navigate (R .id .action_to_ThirdFragment )
76
- );
77
-
78
- binding .buttonClose1 .setOnClickListener (v ->{
79
- finishAffinity (getActivity ());
80
- System .exit (0 );
95
+ gestureDetector = new GestureDetector (getContext (), (GestureDetector .OnGestureListener ) this );
96
+ view .setOnTouchListener ((v , event ) -> {
97
+ gestureDetector .onTouchEvent (event );
98
+ return true ;
81
99
});
82
100
83
- binding .buttonFresh1 .setOnClickListener (v ->
84
- fetchHitokoto ()
85
- );
86
-
87
- buttonToggle .setOnClickListener (v -> {
88
- isAutoFetchEnabled = !isAutoFetchEnabled ;
89
- if (isAutoFetchEnabled ) {
90
- buttonToggle .setText (R .string .stop );
91
- startAutoFetch ();
92
- } else {
93
- buttonToggle .setText (R .string .auto );
94
- stopAutoFetch ();
95
- }
96
- });
97
101
98
102
fetchHitokoto ();
99
- }
100
-
101
- private void startAutoFetch () {
102
- countDownTimer = new CountDownTimer (5000 , 50 ) {
103
- @ Override
104
- public void onTick (long millisUntilFinished ) {
105
- int progress = (int ) (100 - millisUntilFinished / 50 );
106
- progressBarCountdown .setProgress (progress );
107
- }
108
103
109
- @ Override
110
- public void onFinish () {
111
- fetchHitokoto ();
112
- if (isAutoFetchEnabled ) {
113
- startAutoFetch ();
114
- }
115
- }
116
- }.start ();
104
+ // progressBarCountdown = view.findViewById(R.id.progressBar_auto_1);
105
+ // buttonToggle = view.findViewById(R.id.button_auto_1);
106
+
107
+ //
108
+ // binding.buttonSecond1.setOnClickListener(v ->
109
+ // NavHostFragment.findNavController(FirstFragment.this)
110
+ // .navigate(R.id.action_to_SecondFragment)
111
+ // );
112
+ //
113
+ // binding.buttonThird1.setOnClickListener(v ->
114
+ // NavHostFragment.findNavController(FirstFragment.this)
115
+ // .navigate(R.id.action_to_ThirdFragment)
116
+ // );
117
+ //
118
+ // binding.buttonClose1.setOnClickListener(v ->{
119
+ // finishAffinity(getActivity());
120
+ // System.exit(0);
121
+ // });
122
+ //
123
+ // binding.buttonFresh1.setOnClickListener(v ->
124
+ // fetchHitokoto()
125
+ // );
126
+
127
+ // buttonToggle.setOnClickListener(v -> {
128
+ // isAutoFetchEnabled = !isAutoFetchEnabled;
129
+ // if (isAutoFetchEnabled) {
130
+ // buttonToggle.setText(R.string.stop);
131
+ // startAutoFetch();
132
+ // } else {
133
+ // buttonToggle.setText(R.string.auto);
134
+ // stopAutoFetch();
135
+ // }
136
+ // });
117
137
}
118
138
119
- private void stopAutoFetch () {
120
- if (countDownTimer != null ) {
121
- countDownTimer .cancel ();
122
- }
123
- }
139
+ // private void startAutoFetch() {
140
+ // countDownTimer = new CountDownTimer(5000, 50) {
141
+ // @Override
142
+ // public void onTick(long millisUntilFinished) {
143
+ // int progress = (int) (100 - millisUntilFinished / 50);
144
+ // progressBarCountdown.setProgress(progress);
145
+ // }
146
+ //
147
+ // @Override
148
+ // public void onFinish() {
149
+ // fetchHitokoto();
150
+ // if (isAutoFetchEnabled) {
151
+ // startAutoFetch();
152
+ // }
153
+ // }
154
+ // }.start();
155
+ // }
156
+ //
157
+ // private void stopAutoFetch() {
158
+ // if (countDownTimer != null) {
159
+ // countDownTimer.cancel();
160
+ // }
161
+ // }
162
+
124
163
125
164
private void fetchHitokoto () {
126
165
Request request = new Request .Builder ()
@@ -150,9 +189,11 @@ public void onResponse(Call call, Response response) throws IOException {
150
189
151
190
hitokotoFrom .setText (json .has ("from" ) && !json .isNull ("from" ) ? json .getString ("from" ) : "" );
152
191
hitokotoFromWho .setText (json .has ("from_who" ) && !json .isNull ("from_who" ) ? json .getString ("from_who" ) : "" );
153
-
192
+ swipeRefreshLayout . setRefreshing ( false );
154
193
} catch (Exception e ) {
155
194
e .printStackTrace ();
195
+ hitokotoText .setText ("Error: " + e .getMessage ());
196
+ swipeRefreshLayout .setRefreshing (false );
156
197
}
157
198
});
158
199
}
@@ -196,9 +237,63 @@ private void renderTextViewByType(TextView textView, String type) {
196
237
textView .setText (description );
197
238
}
198
239
240
+
199
241
@ Override
200
242
public void onDestroyView () {
201
243
super .onDestroyView ();
202
244
binding = null ;
203
245
}
246
+
247
+ @ Override
248
+ public boolean onDown (@ NonNull MotionEvent motionEvent ) {
249
+ return false ;
250
+ }
251
+
252
+ @ Override
253
+ public void onShowPress (@ NonNull MotionEvent motionEvent ) {
254
+
255
+ }
256
+
257
+ @ Override
258
+ public boolean onSingleTapUp (@ NonNull MotionEvent motionEvent ) {
259
+ return false ;
260
+ }
261
+
262
+ @ Override
263
+ public boolean onScroll (MotionEvent e1 , MotionEvent e2 , float distanceX , float distanceY ) {
264
+ // Check if SwipeRefreshLayout is not refreshing
265
+ if (!swipeRefreshLayout .isRefreshing ()) {
266
+ // Check if the scroll is primarily vertical
267
+ if (Math .abs (distanceY ) > Math .abs (distanceX )) {
268
+ // Check if the scroll distance is significant enough to be considered a pull-down gesture
269
+ if (distanceY < -SWIPE_THRESHOLD ) {
270
+ // Handle pull-down gesture for refreshing
271
+ swipeRefreshLayout .setRefreshing (true );
272
+ fetchHitokoto ();
273
+ return true ;
274
+ }
275
+ }
276
+ }
277
+ // Handle regular scroll gesture
278
+ return false ;
279
+ }
280
+
281
+ @ Override
282
+ public void onLongPress (MotionEvent e ) {
283
+ }
284
+
285
+
286
+ @ Override
287
+ public boolean onFling (MotionEvent e1 , MotionEvent e2 , float velocityX , float velocityY ) {
288
+ if (e1 .getX () - e2 .getX () > SWIPE_THRESHOLD && Math .abs (velocityX ) > SWIPE_VELOCITY_THRESHOLD ) {
289
+ // 向左滑动
290
+ NavHostFragment .findNavController (this )
291
+ .navigate (R .id .action_to_SecondFragment , null , new NavOptions .Builder ().setPopUpTo (R .id .FirstFragment , true ).build ());
292
+ } else if (e2 .getX () - e1 .getX () > SWIPE_THRESHOLD && Math .abs (velocityX ) > SWIPE_VELOCITY_THRESHOLD ) {
293
+ // 向右滑动
294
+ NavHostFragment .findNavController (this )
295
+ .navigate (R .id .action_to_ThirdFragment , null , new NavOptions .Builder ().setPopUpTo (R .id .FirstFragment , true ).build ());
296
+ }
297
+ return true ; // 返回 true 表示事件已处理
298
+ }
204
299
}
0 commit comments