Android实现上下菜单双向滑动
作者:hahashui123 发布时间:2023-06-10 02:43:37
标签:Android,菜单,滑动
本文实例为大家分享了Android实现上下菜单双向滑动的具体代码,供大家参考,具体内容如下
这是研究了网上大神双向左右滑动后实现的上下双向滑动特效,有兴趣的朋友可以看下面代码,注释很详细,原理就是根据手指滑动的方向,来将上下两个布局进行显示与隐藏。主要用了onTouch方法,获取滑动的距离进行偏移。
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{
/**
* 滚动显示和隐藏上侧布局时,手指滑动需要达到的速度。
*/
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;
}
}
下面是使用实例:
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;
/**
* 在内容布局上显示的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);
}
}
布局文件:
<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>
来源:https://blog.csdn.net/hahashui123/article/details/37600855


猜你喜欢
- MyBatis-Plus是通过version机制实现乐观锁的。大致思路:取出记录,携带记录的当前version;更新记录的时候,比较记录当前
- 返回集合为null还是空集合及空集合的三种写法个人认为在自己写接口时,需要返回集合时返回一个空集合,比如mybatis查询如果返回一个集合,
- 前言该篇文章主要总结的是自己平时工作中使用频率比较高的Xml文档操作的一些常用方法和收集网上写的比较好的一些通用Xml文档操作的方法(主要包
- 本文实例讲述了Android开发中使用Intent打开第三方应用及验证可用性的方法。分享给大家供大家参考,具体如下:Android中提供了I
- 目录1.下列关于计算机系统和Java编程语言的说法,正确的是()2.变量a是一个64位有符号的整数,初始值用16进制表示为:0Xf00000
- 通过拍照或相册中获取图片,并进行裁剪操作,然后把图片显示到ImageView上。 当然也可以上传到服务器(项目中绝大部分情况是上传
- 实时代码模板(Live Templates)我们先来看一个gif图:大兄弟,你看清我的操作了么?这个就是实时代码模板的功能。我们来看一下怎么
- Spring Expression Language (SpEL)是强大的表达式语言,支持查询、操作运行时对象图,以及解析逻辑、算术表达式。
- 关于Context我们首先应该知道:(1)它描述的是一个应用程序环境的信息,即上下文。(2)该类是一个抽象(abstract class)类
- 前言最近VS2019正式版发布了,装下来顺便试用了一下C#8.0,最大的看点应该就是可空引用类型了。不过C#8.0仍然处于Beta的状态,而
- 第1部分 ArrayList介绍ArrayList 是一个数组队列,相当于 动态数组。与Java中的数组相比,它的容量能动态增长。它继承于A
- 本文实例为大家分享了PhotoView实现图片双击放大单击退出的具体代码,供大家参考,具体内容如下实现思路1.复制PhotoView&nbs
- 引言之前写了一篇关于 TraceId 的文章:为全局请求添加 TraceId ,看日志再也不懵逼今天就接着 TraceId 做一些优化,如果
- Spring @RequestParam对象绑定在Spring中,如果在方法参数列表中使用@RequestParam标注多个参数,会让映射方
- 在之前博文中多次使用了点击事件的处理实现,有朋友就问了,发现了很多按钮的点击实现,但有很多博文中使用的实现方式有都不一样,到底是怎么回事。今
- 一、前言使用动态配置的原因: properties 和 yaml 是写到项目中的,好多时候有些配置需要修改,每次修改就要重新启动项目,不仅增
- maven-compiler-plugin编译Java源码,一般只需设置编译的jdk版本<plugin> <g
- 本文实例为大家分享了Android自定义输入法软键盘的具体代码,供大家参考,具体内容如下1 功能描述触屏设备主界面中有一个文本编辑框,底部区
- 事务处理基本原理 事务是将一系列操作作为一个单元执行,要么成功,要么失败,回滚到
- 说明使用工具:brew caskbrew cask是一个用命令行管理Mac下应用的工具,提供了自动安装和卸载功能,能够自动从官网上下载并安装