Android實現(xiàn)上下菜單雙向滑動
本文實例為大家分享了Android實現(xiàn)上下菜單雙向滑動的具體代碼,供大家參考,具體內(nèi)容如下
這是研究了網(wǎng)上大神雙向左右滑動后實現(xiàn)的上下雙向滑動特效,有興趣的朋友可以看下面代碼,注釋很詳細(xì),原理就是根據(jù)手指滑動的方向,來將上下兩個布局進(jìn)行顯示與隱藏。主要用了onTouch方法,獲取滑動的距離進(jìn)行偏移。
import 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{ /** * 滾動顯示和隱藏上側(cè)布局時,手指滑動需要達(dá)到的速度。 */ public static final int SNAP_VELOCITY = 200; /** * 滑動狀態(tài)的一種,表示未進(jìn)行任何滑動。 */ public static final int DO_NOTHING = 0; /** * 滑動狀態(tài)的一種,表示正在滑出上側(cè)菜單。 */ public static final int SHOW_UP_MENU = 1; /** * 滑動狀態(tài)的一種,表示正在滑出下側(cè)菜單。 */ public static final int SHOW_DOWN_MENU = 2; /** * 滑動狀態(tài)的一種,表示正在隱藏上側(cè)菜單。 */ public static final int HIDE_UP_MENU = 3; /** * 滑動狀態(tài)的一種,表示正在隱藏下側(cè)菜單。 */ public static final int HIDE_DOWN_MENU = 4; /** * 記錄當(dāng)前的滑動狀態(tài) */ private int slideState; /** * 屏幕寬度值。 */ private int screenWidth; private int screenHeight; /** * 在被判定為滾動之前用戶手指可以移動的最大值。 */ private int touchSlop; /** * 記錄手指按下時的橫坐標(biāo)。 */ private float xDown; /** * 記錄手指按下時的縱坐標(biāo)。 */ private float yDown; /** * 記錄手指移動時的橫坐標(biāo)。 */ private float xMove; /** * 記錄手指移動時的縱坐標(biāo)。 */ private float yMove; /** * 記錄手機(jī)抬起時的縱坐標(biāo)。 */ private float yUp; /** * 上側(cè)菜單當(dāng)前是顯示還是隱藏。只有完全顯示或隱藏時才會更改此值,滑動過程中此值無效。 */ private boolean isUpMenuVisible; /** * 下側(cè)菜單當(dāng)前是顯示還是隱藏。只有完全顯示或隱藏時才會更改此值,滑動過程中此值無效。 */ private boolean isDownMenuVisible; /** * 是否正在滑動。 */ private boolean isSliding; /** * 上側(cè)菜單布局對象。 */ private View upMenuLayout; /** * 下側(cè)菜單布局對象。 */ private View downMenuLayout; /** * 內(nèi)容布局對象。 */ private View contentLayout; /** * 用于監(jiān)聽滑動事件的View。 */ private View mBindView; /** * 上側(cè)菜單布局的參數(shù)。 */ private MarginLayoutParams upMenuLayoutParams; /** * 下側(cè)菜單布局的參數(shù)。 */ private MarginLayoutParams downMenuLayoutParams; /** * 內(nèi)容布局的參數(shù)。 */ 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(); } /** * 綁定監(jiān)聽滑動事件的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: // 手指按下時,記錄按下時的坐標(biāo) xDown = event.getRawX(); yDown = event.getRawY(); // 將滑動狀態(tài)初始化為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); // 檢查當(dāng)前的滑動狀態(tài) 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) { // 手指抬起時,進(jìn)行判斷當(dāng)前手勢的意圖 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) { // 當(dāng)上側(cè)菜單顯示時,如果用戶點擊一下內(nèi)容部分,則直接滾動到內(nèi)容界面 scrollToContentFromUpMenu(); } else if (upDistanceY < touchSlop && isDownMenuVisible) { // 當(dāng)下側(cè)菜單顯示時,如果用戶點擊一下內(nèi)容部分,則直接滾動到內(nèi)容界面 scrollToContentFromDownMenu(); } recycleVelocityTracker(); break; } return true; } /** * 創(chuàng)建VelocityTracker對象,并將觸摸事件加入到VelocityTracker當(dāng)中。 * * @param event * */ private void createVelocityTracker(MotionEvent event) { if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(event); } /** * 根據(jù)手指移動的距離,判斷當(dāng)前用戶的滑動意圖,然后給slideState賦值成相應(yīng)的滑動狀態(tài)值。 * * @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); // 如果用戶想要滑動上側(cè)菜單,將上側(cè)菜單顯示,下側(cè)菜單隱藏 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); // 如果用戶想要滑動下側(cè)菜單,將下側(cè)菜單顯示,上側(cè)菜單隱藏 upMenuLayout.setVisibility(View.GONE); downMenuLayout.setVisibility(View.VISIBLE); } } } /** * 在滑動過程中檢查上側(cè)菜單的邊界值,防止綁定布局滑出屏幕。 */ private void checkUpMenuBorder() { if (contentLayoutParams.bottomMargin > 0) { contentLayoutParams.bottomMargin = 0; } else if (contentLayoutParams.bottomMargin < -upMenuLayoutParams.height) { contentLayoutParams.bottomMargin = -upMenuLayoutParams.height; } } /** * 在滑動過程中檢查下側(cè)菜單的邊界值,防止綁定布局滑出屏幕。 */ private void checkDownMenuBorder() { if (contentLayoutParams.topMargin > 0) { contentLayoutParams.topMargin = 0; } else if (contentLayoutParams.topMargin < -downMenuLayoutParams.height) { contentLayoutParams.topMargin = -downMenuLayoutParams.height; } } /** * 判斷是否應(yīng)該滾動將上側(cè)菜單展示出來。如果手指移動距離大于上側(cè)菜單寬度的1/2,或者手指移動速度大于SNAP_VELOCITY, * 就認(rèn)為應(yīng)該滾動將上側(cè)菜單展示出來。 * * @return 如果應(yīng)該將上側(cè)菜單展示出來返回true,否則返回false。 */ private boolean shouldScrollToUpMenu() { return yUp - yDown > upMenuLayoutParams.height / 2 || getScrollVelocity() > SNAP_VELOCITY; } /** * 判斷是否應(yīng)該滾動將下側(cè)菜單展示出來。如果手指移動距離大于下側(cè)菜單寬度的1/2,或者手指移動速度大于SNAP_VELOCITY, * 就認(rèn)為應(yīng)該滾動將下側(cè)菜單展示出來。 * * @return 如果應(yīng)該將下側(cè)菜單展示出來返回true,否則返回false。 */ private boolean shouldScrollToDownMenu() { return yDown - yUp > downMenuLayoutParams.height / 2 || getScrollVelocity() > SNAP_VELOCITY; } /** * 判斷是否應(yīng)該從上側(cè)菜單滾動到內(nèi)容布局,如果手指移動距離大于上側(cè)菜單寬度的1/2,或者手指移動速度大于SNAP_VELOCITY, * 就認(rèn)為應(yīng)該從上側(cè)菜單滾動到內(nèi)容布局。 * * @return 如果應(yīng)該從上側(cè)菜單滾動到內(nèi)容布局返回true,否則返回false。 */ private boolean shouldScrollToContentFromUpMenu() { return yDown - yUp > upMenuLayoutParams.height / 2 || getScrollVelocity() > SNAP_VELOCITY; } /** * 判斷是否應(yīng)該從下側(cè)菜單滾動到內(nèi)容布局,如果手指移動距離大于下側(cè)菜單寬度的1/2,或者手指移動速度大于SNAP_VELOCITY, * 就認(rèn)為應(yīng)該從下側(cè)菜單滾動到內(nèi)容布局。 * * @return 如果應(yīng)該從下側(cè)菜單滾動到內(nèi)容布局返回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; // 根據(jù)傳入的速度來滾動界面,當(dāng)滾動到達(dá)邊界值時,跳出循環(huán)。 while (true) { bottomMargin = bottomMargin + speed[0]; if (bottomMargin < -upMenuLayoutParams.height) { bottomMargin = -upMenuLayoutParams.height; break; } if (bottomMargin > 0) { bottomMargin = 0; break; } publishProgress(bottomMargin); // 為了要有滾動效果產(chǎn)生,每次循環(huán)使線程睡眠一段時間,這樣肉眼才能夠看到滾動動畫。 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; // 根據(jù)傳入的速度來滾動界面,當(dāng)滾動到達(dá)邊界值時,跳出循環(huán)。 while (true) { topMargin = topMargin + speed[0]; if (topMargin < -downMenuLayoutParams.height) { topMargin = -downMenuLayoutParams.height; break; } if (topMargin > 0) { topMargin = 0; break; } publishProgress(topMargin); // 為了要有滾動效果產(chǎn)生,每次循環(huán)使線程睡眠一段時間,這樣肉眼才能夠看到滾動動畫。 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); } } /** * 使當(dāng)前線程睡眠指定的毫秒數(shù)。 * * @param millis * 指定當(dāng)前線程睡眠多久,以毫秒為單位 */ 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); } } /** * 將界面滾動到上側(cè)菜單界面,滾動速度設(shè)定為-30. */ public void scrollToUpMenu() { new UpMenuScrollTask().execute(-30); } /** * 將界面滾動到下側(cè)菜單界面,滾動速度設(shè)定為-30. */ public void scrollToDownMenu() { new DownMenuScrollTask().execute(-30); } /** * 將界面從上側(cè)菜單滾動到內(nèi)容界面,滾動速度設(shè)定為30. */ public void scrollToContentFromUpMenu() { new UpMenuScrollTask().execute(30); } /** * 將界面從下側(cè)菜單滾動到內(nèi)容界面,滾動速度設(shè)定為30. */ public void scrollToContentFromDownMenu() { new DownMenuScrollTask().execute(30); } /** * 上側(cè)菜單是否完全顯示出來,滑動過程中此值無效。 * * @return 上側(cè)菜單完全顯示返回true,否則返回false。 */ public boolean isUpLayoutVisible() { return isUpMenuVisible; } /** * 下側(cè)菜單是否完全顯示出來,滑動過程中此值無效。 * * @return 下側(cè)菜單完全顯示返回true,否則返回false。 */ public boolean isDownLayoutVisible() { return isDownMenuVisible; } /** * 在onLayout中重新設(shè)定上側(cè)菜單、下側(cè)菜單、以及內(nèi)容布局的參數(shù)。 */ @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); if (changed) { // 獲取上側(cè)菜單布局對象 upMenuLayout = getChildAt(0); upMenuLayoutParams = (MarginLayoutParams) upMenuLayout.getLayoutParams(); // 獲取下側(cè)菜單布局對象 downMenuLayout = getChildAt(1); downMenuLayoutParams = (MarginLayoutParams) downMenuLayout.getLayoutParams(); // 獲取內(nèi)容布局對象 contentLayout = getChildAt(2); contentLayoutParams = (RelativeLayout.LayoutParams) contentLayout.getLayoutParams(); contentLayoutParams.height = screenHeight; contentLayout.setLayoutParams(contentLayoutParams); } } /** * 回收VelocityTracker對象。 */ private void recycleVelocityTracker() { mVelocityTracker.recycle(); mVelocityTracker = null; } }
下面是使用實例:
import 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; /** * 在內(nèi)容布局上顯示的ListView */ private ListView contentList; private LinearLayout ll; /** * ListView的適配器 */ private ArrayAdapter<String> contentListAdapter; /** * 用于填充contentListAdapter的數(shù)據(jù)源。 */ 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); } }
布局文件:
<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>
更多關(guān)于滑動功能的文章,請點擊專題: 《Android滑動功能》
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android自定義view實現(xiàn)滑動解鎖效果
- Android實現(xiàn)小米相機(jī)底部滑動指示器
- Android實現(xiàn)View滑動效果的6種方法
- Android自定義SeekBar實現(xiàn)滑動驗證且不可點擊
- Android SeekBar實現(xiàn)禁止滑動
- Android實現(xiàn)一個比相冊更高大上的左右滑動特效(附源碼)
- Android 滑動Scrollview標(biāo)題欄漸變效果(仿京東toolbar)
- Android RecycleView滑動停止后自動吸附效果的實現(xiàn)代碼(滑動定位)
- Android實現(xiàn)滑動效果
- Android實現(xiàn)手勢滑動(左滑和右滑)
- Android RecyclerView實現(xiàn)滑動刪除
- 詳解Android使用CoordinatorLayout+AppBarLayout+CollapsingToolbarLayou實現(xiàn)手指滑動效果
- Android實現(xiàn)三段式滑動效果
相關(guān)文章
Android系統(tǒng)設(shè)置中的清除數(shù)據(jù)會清除哪些數(shù)據(jù)?
這篇文章主要介紹了Android系統(tǒng)設(shè)置中的清除數(shù)據(jù)會清除哪些數(shù)據(jù)?本文對比了清除前和清除后的數(shù)據(jù)情況,從而得出到底清除了哪些數(shù)據(jù),需要的朋友可以參考下2015-01-01Android?DataBinding類關(guān)系深入探究
看了谷歌官方文章確實寫的太簡略了,甚至看完之后有很多地方還不知道怎么回事兒或者怎么用,那么接下來我將通過文章全面介紹一下DataBinding類關(guān)系2022-11-11Android自定義view實現(xiàn)有header和footer作為layout使用的滾動控件
這篇文章主要介紹了Android自定義view實現(xiàn)有header和footer的滾動控件,可以在XML中當(dāng)Layout使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-11-11Android TextSwitcher實現(xiàn)文字上下翻牌效果(銅板街)
這篇文章主要介紹了Android TextSwitcher實現(xiàn)文字上下翻牌效果(銅板街),需要的朋友可以參考下2017-05-05Android中用StaticLayout實現(xiàn)文本繪制自動換行詳解
StaticLayout是android中處理文字換行的一個工具類,StaticLayout已經(jīng)實現(xiàn)了文本繪制換行處理,下面這篇文章主要介紹了Android中用StaticLayout實現(xiàn)文本繪制自動換行的相關(guān)資料,需要的朋友可以參考。2017-03-03Android實現(xiàn)一個包含表格的圖標(biāo)庫實例代碼
這篇文章主要介紹了Android實現(xiàn)一個包含表格的圖標(biāo)庫的實例代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2018-01-01