本文实例为大家分享了Android实现上下菜单双向滑动的具体代码,供大家参考,具体内容如下
这是研究了网上大神双向左右滑动后实现的上下双向滑动特效,有兴趣的朋友可以看下面代码,注释很详细,原理就是根据手指滑动的方向,来将上下两个布局进行显示与隐藏。主要用了onTouch方法,获取滑动的距离进行偏移。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556import android.content.Context; import android.os.AsyncTask; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.VelocityTracker; import android.view.View; import android.view.ViewConfiguration; import android.view.WindowManager; import android.view.View.OnTouchListener; import android.widget.RelativeLayout; public class UpAndDownSlidinglayout extends RelativeLayout implements OnTouchListener{ /** * 滚动显示和隐藏上侧布局时,手指滑动需要达到的速度。 */ public static final int SNAP_VELOCITY = 200; /** * 滑动状态的一种,表示未进行任何滑动。 */ public static final int DO_NOTHING = 0; /** * 滑动状态的一种,表示正在滑出上侧菜单。 */ public static final int SHOW_UP_MENU = 1; /** * 滑动状态的一种,表示正在滑出下侧菜单。 */ public static final int SHOW_DOWN_MENU = 2; /** * 滑动状态的一种,表示正在隐藏上侧菜单。 */ public static final int HIDE_UP_MENU = 3; /** * 滑动状态的一种,表示正在隐藏下侧菜单。 */ public static final int HIDE_DOWN_MENU = 4; /** * 记录当前的滑动状态 */ private int slideState; /** * 屏幕宽度值。 */ private int screenWidth; private int screenHeight; /** * 在被判定为滚动之前用户手指可以移动的最大值。 */ private int touchSlop; /** * 记录手指按下时的横坐标。 */ private float xDown; /** * 记录手指按下时的纵坐标。 */ private float yDown; /** * 记录手指移动时的横坐标。 */ private float xMove; /** * 记录手指移动时的纵坐标。 */ private float yMove; /** * 记录手机抬起时的纵坐标。 */ private float yUp; /** * 上侧菜单当前是显示还是隐藏。只有完全显示或隐藏时才会更改此值,滑动过程中此值无效。 */ private boolean isUpMenuVisible; /** * 下侧菜单当前是显示还是隐藏。只有完全显示或隐藏时才会更改此值,滑动过程中此值无效。 */ private boolean isDownMenuVisible; /** * 是否正在滑动。 */ private boolean isSliding; /** * 上侧菜单布局对象。 */ private View upMenuLayout; /** * 下侧菜单布局对象。 */ private View downMenuLayout; /** * 内容布局对象。 */ private View contentLayout; /** * 用于监听滑动事件的View。 */ private View mBindView; /** * 上侧菜单布局的参数。 */ private MarginLayoutParams upMenuLayoutParams; /** * 下侧菜单布局的参数。 */ private MarginLayoutParams downMenuLayoutParams; /** * 内容布局的参数。 */ private RelativeLayout.LayoutParams contentLayoutParams; /** * 用于计算手指滑动的速度。 */ private VelocityTracker mVelocityTracker; public UpAndDownSlidinglayout(Context context, AttributeSet attrs) { super(context, attrs); WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); screenWidth = wm.getDefaultDisplay().getWidth(); screenHeight = wm.getDefaultDisplay().getHeight(); touchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); } /** * 绑定监听滑动事件的View。 * * @param bindView * 需要绑定的View对象。 */ public void setScrollEvent(View bindView) { mBindView = bindView; mBindView.setOnTouchListener(this); } @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub createVelocityTracker(event); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // 手指按下时,记录按下时的坐标 xDown = event.getRawX(); yDown = event.getRawY(); // 将滑动状态初始化为DO_NOTHING slideState = DO_NOTHING; break; case MotionEvent.ACTION_MOVE: xMove = event.getRawX(); yMove = event.getRawY(); int moveDistanceX = (int) (xMove - xDown); int moveDistanceY = (int) (yMove - yDown); // 检查当前的滑动状态 checkSlideState(moveDistanceX, moveDistanceY); switch (slideState) { case SHOW_UP_MENU: contentLayoutParams.bottomMargin = -moveDistanceY; checkUpMenuBorder(); contentLayout.setLayoutParams(contentLayoutParams); break; case HIDE_UP_MENU: contentLayoutParams.bottomMargin = -upMenuLayoutParams.height - moveDistanceY; checkUpMenuBorder(); contentLayout.setLayoutParams(contentLayoutParams); case SHOW_DOWN_MENU: contentLayoutParams.topMargin = moveDistanceY; checkDownMenuBorder(); contentLayout.setLayoutParams(contentLayoutParams); break; case HIDE_DOWN_MENU: contentLayoutParams.topMargin = -downMenuLayoutParams.height + moveDistanceY; checkDownMenuBorder(); contentLayout.setLayoutParams(contentLayoutParams); default: break; } break; case MotionEvent.ACTION_UP: yUp = event.getRawY(); int upDistanceY = (int) (yUp - yDown); if (isSliding) { // 手指抬起时,进行判断当前手势的意图 switch (slideState) { case SHOW_UP_MENU: if (shouldScrollToUpMenu()) { scrollToUpMenu(); } else { scrollToContentFromUpMenu(); } break; case HIDE_UP_MENU: if (shouldScrollToContentFromUpMenu()) { scrollToContentFromUpMenu(); } else { scrollToUpMenu(); } break; case SHOW_DOWN_MENU: if (shouldScrollToDownMenu()) { scrollToDownMenu(); } else { scrollToContentFromDownMenu(); } break; case HIDE_DOWN_MENU: if (shouldScrollToContentFromDownMenu()) { scrollToContentFromDownMenu(); } else { scrollToDownMenu(); } break; default: break; } }else if (upDistanceY < touchSlop && isUpMenuVisible) { // 当上侧菜单显示时,如果用户点击一下内容部分,则直接滚动到内容界面 scrollToContentFromUpMenu(); } else if (upDistanceY < touchSlop && isDownMenuVisible) { // 当下侧菜单显示时,如果用户点击一下内容部分,则直接滚动到内容界面 scrollToContentFromDownMenu(); } recycleVelocityTracker(); break; } return true; } /** * 创建VelocityTracker对象,并将触摸事件加入到VelocityTracker当中。 * * @param event * */ private void createVelocityTracker(MotionEvent event) { if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(event); } /** * 根据手指移动的距离,判断当前用户的滑动意图,然后给slideState赋值成相应的滑动状态值。 * * @param moveDistanceX * 横向移动的距离 * @param moveDistanceY * 纵向移动的距离 */ private void checkSlideState(int moveDistanceX, int moveDistanceY) { if(isUpMenuVisible){ if (!isSliding && Math.abs(moveDistanceY) >= touchSlop && moveDistanceY < 0) { isSliding = true; slideState = HIDE_UP_MENU; } }else if(isDownMenuVisible){ if (!isSliding && Math.abs(moveDistanceY) >= touchSlop && moveDistanceY > 0) { isSliding = true; slideState = HIDE_DOWN_MENU; } }else{ if (!isSliding && Math.abs(moveDistanceY) >= touchSlop && moveDistanceY > 0 && Math.abs(moveDistanceX) < touchSlop) { isSliding = true; slideState = SHOW_UP_MENU; contentLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0); contentLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); contentLayout.setLayoutParams(contentLayoutParams); // 如果用户想要滑动上侧菜单,将上侧菜单显示,下侧菜单隐藏 upMenuLayout.setVisibility(View.VISIBLE); downMenuLayout.setVisibility(View.GONE); }else if(!isSliding && Math.abs(moveDistanceY) >= touchSlop && moveDistanceY < 0 && Math.abs(moveDistanceX) < touchSlop){ isSliding = true; slideState = SHOW_DOWN_MENU; contentLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 0); contentLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP); contentLayout.setLayoutParams(contentLayoutParams); // 如果用户想要滑动下侧菜单,将下侧菜单显示,上侧菜单隐藏 upMenuLayout.setVisibility(View.GONE); downMenuLayout.setVisibility(View.VISIBLE); } } } /** * 在滑动过程中检查上侧菜单的边界值,防止绑定布局滑出屏幕。 */ private void checkUpMenuBorder() { if (contentLayoutParams.bottomMargin > 0) { contentLayoutParams.bottomMargin = 0; } else if (contentLayoutParams.bottomMargin < -upMenuLayoutParams.height) { contentLayoutParams.bottomMargin = -upMenuLayoutParams.height; } } /** * 在滑动过程中检查下侧菜单的边界值,防止绑定布局滑出屏幕。 */ private void checkDownMenuBorder() { if (contentLayoutParams.topMargin > 0) { contentLayoutParams.topMargin = 0; } else if (contentLayoutParams.topMargin < -downMenuLayoutParams.height) { contentLayoutParams.topMargin = -downMenuLayoutParams.height; } } /** * 判断是否应该滚动将上侧菜单展示出来。如果手指移动距离大于上侧菜单宽度的1/2,或者手指移动速度大于SNAP_VELOCITY, * 就认为应该滚动将上侧菜单展示出来。 * * @return 如果应该将上侧菜单展示出来返回true,否则返回false。 */ private boolean shouldScrollToUpMenu() { return yUp - yDown > upMenuLayoutParams.height / 2 || getScrollVelocity() > SNAP_VELOCITY; } /** * 判断是否应该滚动将下侧菜单展示出来。如果手指移动距离大于下侧菜单宽度的1/2,或者手指移动速度大于SNAP_VELOCITY, * 就认为应该滚动将下侧菜单展示出来。 * * @return 如果应该将下侧菜单展示出来返回true,否则返回false。 */ private boolean shouldScrollToDownMenu() { return yDown - yUp > downMenuLayoutParams.height / 2 || getScrollVelocity() > SNAP_VELOCITY; } /** * 判断是否应该从上侧菜单滚动到内容布局,如果手指移动距离大于上侧菜单宽度的1/2,或者手指移动速度大于SNAP_VELOCITY, * 就认为应该从上侧菜单滚动到内容布局。 * * @return 如果应该从上侧菜单滚动到内容布局返回true,否则返回false。 */ private boolean shouldScrollToContentFromUpMenu() { return yDown - yUp > upMenuLayoutParams.height / 2 || getScrollVelocity() > SNAP_VELOCITY; } /** * 判断是否应该从下侧菜单滚动到内容布局,如果手指移动距离大于下侧菜单宽度的1/2,或者手指移动速度大于SNAP_VELOCITY, * 就认为应该从下侧菜单滚动到内容布局。 * * @return 如果应该从下侧菜单滚动到内容布局返回true,否则返回false。 */ private boolean shouldScrollToContentFromDownMenu() { return yUp - yDown > downMenuLayoutParams.height / 2 || getScrollVelocity() > SNAP_VELOCITY; } /** * 获取手指在绑定布局上的滑动速度。 * * @return 滑动速度,以每秒钟移动了多少像素值为单位。 */ private int getScrollVelocity() { mVelocityTracker.computeCurrentVelocity(1000); int velocity = (int) mVelocityTracker.getXVelocity(); return Math.abs(velocity); } class UpMenuScrollTask extends AsyncTask<Integer, Integer, Integer> { @Override protected Integer doInBackground(Integer... speed) { int bottomMargin = contentLayoutParams.bottomMargin; // 根据传入的速度来滚动界面,当滚动到达边界值时,跳出循环。 while (true) { bottomMargin = bottomMargin + speed[0]; if (bottomMargin < -upMenuLayoutParams.height) { bottomMargin = -upMenuLayoutParams.height; break; } if (bottomMargin > 0) { bottomMargin = 0; break; } publishProgress(bottomMargin); // 为了要有滚动效果产生,每次循环使线程睡眠一段时间,这样肉眼才能够看到滚动动画。 sleep(15); } if (speed[0] > 0) { isUpMenuVisible = false; } else { isUpMenuVisible = true; } isSliding = false; return bottomMargin; } @Override protected void onProgressUpdate(Integer... bottomMargin) { contentLayoutParams.bottomMargin = bottomMargin[0]; contentLayout.setLayoutParams(contentLayoutParams); unFocusBindView(); } @Override protected void onPostExecute(Integer bottomMargin) { contentLayoutParams.bottomMargin = bottomMargin; contentLayout.setLayoutParams(contentLayoutParams); } } class DownMenuScrollTask extends AsyncTask<Integer, Integer, Integer> { @Override protected Integer doInBackground(Integer... speed) { int topMargin = contentLayoutParams.topMargin; // 根据传入的速度来滚动界面,当滚动到达边界值时,跳出循环。 while (true) { topMargin = topMargin + speed[0]; if (topMargin < -downMenuLayoutParams.height) { topMargin = -downMenuLayoutParams.height; break; } if (topMargin > 0) { topMargin = 0; break; } publishProgress(topMargin); // 为了要有滚动效果产生,每次循环使线程睡眠一段时间,这样肉眼才能够看到滚动动画。 sleep(15); } if (speed[0] > 0) { isDownMenuVisible = false; } else { isDownMenuVisible = true; } isSliding = false; return topMargin; } @Override protected void onProgressUpdate(Integer... topMargin) { contentLayoutParams.topMargin = topMargin[0]; contentLayout.setLayoutParams(contentLayoutParams); unFocusBindView(); } @Override protected void onPostExecute(Integer topMargin) { contentLayoutParams.topMargin = topMargin; contentLayout.setLayoutParams(contentLayoutParams); } } /** * 使当前线程睡眠指定的毫秒数。 * * @param millis * 指定当前线程睡眠多久,以毫秒为单位 */ private void sleep(long millis) { try { Thread.sleep(millis); } catch (InterruptedException e) { e.printStackTrace(); } } /** * 使用可以获得焦点的控件在滑动的时候失去焦点。 */ private void unFocusBindView() { if (mBindView != null) { mBindView.setPressed(false); mBindView.setFocusable(false); mBindView.setFocusableInTouchMode(false); } } /** * 将界面滚动到上侧菜单界面,滚动速度设定为-30. */ public void scrollToUpMenu() { new UpMenuScrollTask().execute(-30); } /** * 将界面滚动到下侧菜单界面,滚动速度设定为-30. */ public void scrollToDownMenu() { new DownMenuScrollTask().execute(-30); } /** * 将界面从上侧菜单滚动到内容界面,滚动速度设定为30. */ public void scrollToContentFromUpMenu() { new UpMenuScrollTask().execute(30); } /** * 将界面从下侧菜单滚动到内容界面,滚动速度设定为30. */ public void scrollToContentFromDownMenu() { new DownMenuScrollTask().execute(30); } /** * 上侧菜单是否完全显示出来,滑动过程中此值无效。 * * @return 上侧菜单完全显示返回true,否则返回false。 */ public boolean isUpLayoutVisible() { return isUpMenuVisible; } /** * 下侧菜单是否完全显示出来,滑动过程中此值无效。 * * @return 下侧菜单完全显示返回true,否则返回false。 */ public boolean isDownLayoutVisible() { return isDownMenuVisible; } /** * 在onLayout中重新设定上侧菜单、下侧菜单、以及内容布局的参数。 */ @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); if (changed) { // 获取上侧菜单布局对象 upMenuLayout = getChildAt(0); upMenuLayoutParams = (MarginLayoutParams) upMenuLayout.getLayoutParams(); // 获取下侧菜单布局对象 downMenuLayout = getChildAt(1); downMenuLayoutParams = (MarginLayoutParams) downMenuLayout.getLayoutParams(); // 获取内容布局对象 contentLayout = getChildAt(2); contentLayoutParams = (RelativeLayout.LayoutParams) contentLayout.getLayoutParams(); contentLayoutParams.height = screenHeight; contentLayout.setLayoutParams(contentLayoutParams); } } /** * 回收VelocityTracker对象。 */ private void recycleVelocityTracker() { mVelocityTracker.recycle(); mVelocityTracker = null; } }
下面是使用实例:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.LinearLayout; import android.widget.ListView; import android.app.Activity; /** * 滑动菜单Demo主Activity * * @author guolin */ public class MainActivity2 extends Activity { /** * 双向滑动菜单布局 */ private UpAndDownSlidinglayout updownSldingLayout; /** * 在内容布局上显示的ListView */ private ListView contentList; private LinearLayout ll; /** * ListView的适配器 */ private ArrayAdapter<String> contentListAdapter; /** * 用于填充contentListAdapter的数据源。 */ private String[] contentItems = { "Content Item 1", "Content Item 2", "Content Item 3", "Content Item 4", "Content Item 5", "Content Item 6", "Content Item 7", "Content Item 8", "Content Item 9", "Content Item 10", "Content Item 11", "Content Item 12", "Content Item 13", "Content Item 14", "Content Item 15", "Content Item 16" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); ll = (LinearLayout) findViewById(R.id.content); updownSldingLayout = (UpAndDownSlidinglayout) findViewById(R.id.updown_sliding_layout); contentList = (ListView) findViewById(R.id.contentList); contentListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, contentItems); contentList.setAdapter(contentListAdapter); updownSldingLayout.setScrollEvent(ll); } }
布局文件:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57<com.example.UpAndDownSlidinglayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/updown_sliding_layout" android:layout_width="fill_parent" android:layout_height="fill_parent" > <RelativeLayout android:id="@+id/up_menu" android:layout_width="fill_parent" android:layout_height="300dp" android:layout_alignParentTop="true" android:background="#00ccff" android:visibility="invisible" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="This is up menu" android:textColor="#000000" android:textSize="28sp" /> </RelativeLayout> <RelativeLayout android:id="@+id/down_menu" android:layout_width="fill_parent" android:layout_height="300dp" android:layout_alignParentBottom="true" android:background="#00ffcc" android:visibility="invisible" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="This is down menu" android:textColor="#000000" android:textSize="28sp" /> </RelativeLayout> <LinearLayout android:id="@+id/content" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:background="#e9e9e9" > <ListView android:id="@+id/contentList" android:layout_width="fill_parent" android:layout_height="500dp" android:scrollbars="none" android:cacheColorHint="#00000000" > </ListView> </LinearLayout> </com.example.UpAndDownSlidinglayout>
更多关于滑动功能的文章,请点击专题: 《Android滑动功能》
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持靠谱客。
最后
以上就是踏实大侠最近收集整理的关于Android实现上下菜单双向滑动的全部内容,更多相关Android实现上下菜单双向滑动内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复