没有合适的资源?快使用搜索试试~ 我知道了~
现在的Android应用,只要有一个什么新的创意,过不了多久,几乎所有的应用都带这个创意。这不,咱们公司最近的一个持续性的项目,想在首页加个从左滑动出来的菜单,我查阅网上资料,并自己摸索,实现了左、右两边都能滑出菜单,并且,左、右菜单中,都可以加ListView等这类需要解决GestureDetector冲突的问题(如在首页面中,含有ListView,上下滚动时,左右不动,相反,左右滑动菜单时,上下不动,听着头就大了吧!) 先上几张图,给大家瞧瞧,对整体有个了解: 一、首页布局: 代码如下: <RelativeLayout xmlns:android=”http://schemas.and
资源推荐
资源详情
资源评论
Android左右滑出菜单实例分析左右滑出菜单实例分析
现在的Android应用,只要有一个什么新的创意,过不了多久,几乎所有的应用都带这个创意。这不,咱们公司最近的一个持
续性的项目,想在首页加个从左滑动出来的菜单,我查阅网上资料,并自己摸索,实现了左、右两边都能滑出菜单,并且,
左、右菜单中,都可以加ListView等这类需要解决GestureDetector冲突的问题(如在首页面中,含有ListView,上下滚动时,
左右不动,相反,左右滑动菜单时,上下不动,听着头就大了吧!)
先上几张图,给大家瞧瞧,对整体有个了解:
一、首页布局:一、首页布局:
代码如下:
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity” >
<!– 主布局 –>
<RelativeLayout
android:id=”@+id/mainLayout”
android:layout_width=”match_parent”
android:layout_height=”match_parent”>
<RelativeLayout
android:id=”@+id/titlebar”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:background=”#ffffff”
android:padding=”5dip”>
<ImageView
android:id=”@+id/ivMore”
android:src=”@drawable/nav_more_normal”
android:contentDescription=”@string/img_desc”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_centerVertical=”true”
android:layout_alignParentLeft=”true”
android:layout_marginLeft=”10dip”/>
<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_centerHorizontal=”true”
android:layout_centerVertical=”true”
android:text=”@string/title”
android:textSize=”20sp”
android:textColor=”#000000″/>
<ImageView
android:id=”@+id/ivSettings”
android:src=”@drawable/nav_setting_normal”
android:contentDescription=”@string/img_desc”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_centerVertical=”true”
android:layout_alignParentRight=”true”
android:layout_marginRight=”10dip”/>
</RelativeLayout>
<ImageView
android:src=”@drawable/picture”
android:contentDescription=”@string/img_desc”
android:scaleType=”fitXY”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:layout_below=”@id/titlebar”/>
</RelativeLayout>
<!– 左侧菜单导航 –>
<RelativeLayout
android:id=”@+id/leftLayout”
android:layout_width=”140dip”
android:layout_height=”match_parent”
android:background=”#000000″>
<RelativeLayout
android:id=”@+id/leftTitlebar”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:background=”@color/grey21″
android:padding=”5dip”>
<TextView
android:layout_marginLeft=”5dip”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentLeft=”true”
android:layout_centerVertical=”true”
android:text=”@string/leftNav”
android:textSize=”20sp”
android:textColor=”#ffffff”/>
</RelativeLayout>
<com.chris.lr.slidemenu.LayoutRelative
android:id=”@+id/layoutSlideMenu”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:layout_below=”@id/leftTitlebar”>
<ListView
android:id=”@+id/listMore”
android:cacheColorHint=”#00000000″
android:layout_width=”match_parent”
android:layout_height=”match_parent”/>
</com.chris.lr.slidemenu.LayoutRelative>
</RelativeLayout>
<!– 右侧菜单导航 –>
<RelativeLayout
android:id=”@+id/rightLayout”
android:layout_width=”140dip”
android:layout_height=”match_parent”
android:background=”#000000″>
<RelativeLayout
android:id=”@+id/rightTitlebar”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:background=”@color/gold”
android:padding=”5dip”>
<TextView
android:layout_marginLeft=”5dip”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentLeft=”true”
android:layout_centerVertical=”true”
android:text=”@string/right_title”
android:textSize=”20sp”
android:textColor=”#ffffff”/>
</RelativeLayout>
<TextView
android:text=”@string/rightNav”
android:textColor=”#ff00ff”
android:textSize=”18sp”
android:layout_width=”match_parent”
android:layout_height=”30dip”
android:layout_below=”@id/rightTitlebar”
android:background=”#000000″/>
</RelativeLayout>
</RelativeLayout>
布局很简单,我个人比较推荐用RelativeLayout,因为这个是几个Layout中,性能最好的,而LinearLayout则不好,原因在
于,某个子视图的宽高变动,会引起这个布局中其它地方也需要重新调整。
布局中,有com.chris.lr.slidemenu.LayoutRelative这个自定义控件是继承RelativeLayout的,里面只是加了些手势的处理,它
的作用实际上就是最开始讲到的,如果含有ListView这类需要判断手势的,则就用到它,先由它来判断,然后在视情况是否拦
截由自己来处理。
二、自定义控件:二、自定义控件:
代码如下:
package com.chris.lr.slidemenu;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.widget.RelativeLayout;
public class LayoutRelative extends RelativeLayout {
private static final String TAG = “ChrisSlideMenu”;
private GestureDetector mGestureDetector;
private boolean bLockScrollX = false;
private boolean bTouchIntercept = false;
private OnScrollListener mOnScrollListenerCallback = null;
public LayoutRelative(Context context) {
this(context, null);
}
public LayoutRelative(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public LayoutRelative(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mGestureDetector = new GestureDetector(new LayoutGestureListener());
}
/**
* 设置滚动监听接口
* @param l
*/
public void setOnScrollListener(OnScrollListener l){
mOnScrollListenerCallback = l;
}
/*
* Progress:
* 1. 重载dispatchTouchEvent,将消息传递给GestureDetector;
* 2. 重载手势中onDown 和 onScroll两个函数;
* 3. 在onDown中,默认对水平滚动方向加锁;
* 4. 在onScroll中,判断e1与e2的水平方向与垂直方向距离:
* a. 如果垂直方向大,则表明是上下滚动,且返回false表明当前手势不用拦截;
* b. 如果水平方向大,则表明是左右滚动,且返回true表明当前手势需要拦截;
* 5. 重载onInterceptTouchEvent,如果手势返回为true,则onInterceptTouchEvent也返回true;
* 6. 如果要拦截手势消息,则需要重载onTouchEvent,或子视图中重载这个函数,来处理这条消息;
* 7. 如果自己处理,则对水平方向滚动去锁(表明当前用户想左右滚动);
*
* ———-
* ———————- ——>| onDown |
* | | | ———-
* | dispatchTouchEvent | <—- —— false: 上下滚动
* | | | ———— /
剩余13页未读,继续阅读
资源评论
weixin_38538585
- 粉丝: 3
- 资源: 956
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功