activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/menu"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#dddddddd"
>
<ListView
android:id="@+id/list_menu"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:orientation="vertical" >
</ListView>
</LinearLayout>
<LinearLayout
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffff0000"
android:orientation="vertical" >
<ListView
android:id="@+id/list_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:orientation="vertical" >
</ListView>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:textColor="#ff00ff00"
/>
<TextView
android:id="@+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#ffffffff"
android:visibility="gone"
/>
</LinearLayout>
MainActivity.java:
package com.example.renrenslidemenudemo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class MainActivity extends Activity implements OnTouchListener {
/**
* 滚动显示和隐藏menu时,手指滑动需要达到的速度。
*/
public static final int SNAP_VELOCITY = 200;
/**
* 屏幕宽度值。
*/
private int screenWidth;
/**
* menu最多可以滑动到的左边缘。值由menu布局的宽度来定,marginLeft到达此值之后,不能再减少。
*/
private int leftEdge;
/**
* menu最多可以滑动到的右边缘。值恒为0,即marginLeft到达0之后,不能增加。
*/
private int rightEdge = 0;
/**
* menu完全显示时,留给content的宽度值。
*/
private int menuPadding = 80;
/**
* 主内容的布局。
*/
private View content;
private ListView listContent;
/**
* menu的布局。
*/
private View menu;
private ListView listMenu;
/**
* menu布局的参数,通过此参数来更改leftMargin的值。
*/
private LinearLayout.LayoutParams menuParams;
/**
* 记录手指按下时的横座标。
*/
private float xDown;
/**
* 记录手指移动时的横座标。
*/
private float xMove;
/**
* 记录手机擡起时的横座标。
*/
private float xUp;
/**
* menu当前是显示还是隐藏。只有完全显示或隐藏menu时才会更改此值,滑动过程中此值无效。
*/
private boolean isMenuVisible;
/**
* 用于计算手指滑动的速度。
*/
private VelocityTracker mVelocityTracker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initValues();
content.setOnTouchListener(this);
boolean flag = false;
listContent.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
TextView text2 = (TextView) arg1.findViewById(R.id.text2);
if (text2.getVisibility() == View.GONE)
text2.setVisibility(View.VISIBLE);
else
text2.setVisibility(View.GONE);
}
});
}
/**
* 初始化一些关键性数据。包括获取屏幕的宽度,给content布局重新设置宽度,给menu布局重新设置宽度和偏移距离等。
*/
private void initValues() {
WindowManager window = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
screenWidth = window.getDefaultDisplay().getWidth();
content = findViewById(R.id.content);
listContent = (ListView) findViewById(R.id.list_content);
listMenu = (ListView) findViewById(R.id.list_menu);
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < 15; i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("title", "侧滑、折叠效果 0" + i);
map.put("title1",
"初始化一些关键性数据。包括获取屏幕的宽度,给content布局重新设置宽度,给menu布局重新设置宽度和偏移距离,初始化一些关键性数据。包括获取屏幕的宽度,给content布局重新设置宽度,给menu布局重新设置宽度和偏移距离,初始化一些关键性数据。包括获取屏幕的宽度,给content布局重新设置宽度,给menu布局重新设置宽度和偏移距离");
list.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.text,
new String[] { "title", "title1" }, new int[] { R.id.text1,
R.id.text2 });
listContent.setAdapter(adapter);
menu = findViewById(R.id.menu);
List<String> list1 = new ArrayList<String>();
list1.add("内容1");
list1.add("内容2");
list1.add("内容3");
list1.add("内容4");
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, list1);
listMenu.setAdapter(adapter1);
menuParams = (LinearLayout.LayoutParams) menu.getLayoutParams();
// 将menu的宽度设置为屏幕宽度减去menuPadding
menuParams.width = screenWidth - menuPadding;
// 左边缘的值赋值为menu宽度的负数
leftEdge = -menuParams.width;
// menu的leftMargin设置为左边缘的值,这样初始化时menu就变为不可见
menuParams.leftMargin = leftEdge;
// 将content的宽度设置为屏幕宽度
content.getLayoutParams().width = screenWidth;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
createVelocityTracker(event);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 手指按下时,记录按下时的横座标
xDown = event.getRawX();
break;
case MotionEvent.ACTION_MOVE:
// 手指移动时,对比按下时的横座标,计算出移动的距离,来调整menu的leftMargin值,从而显示和隐藏menu
xMove = event.getRawX();
int distanceX = (int) (xMove - xDown);
if (isMenuVisible) {
menuParams.leftMargin = distanceX;
} else {
menuParams.leftMargin = leftEdge + distanceX;
}
if (menuParams.leftMargin < leftEdge) {
menuParams.leftMargin = leftEdge;
} else if (menuParams.leftMargin > rightEdge) {
menuParams.leftMargin = rightEdge;
}
Log.i("Left Margin", menuParams.leftMargin + "");
menu.setLayoutParams(menuParams);
break;
case MotionEvent.ACTION_UP:
// 手指擡起时,进行判断当前手势的意图,从而决定是滚动到menu界面,还是滚动到content界面
xUp = event.getRawX();
if (wantToShowMenu()) {
if (shouldScrollToMenu()) {
scrollToMenu();
} else {
scrollToContent();
}
} else if (wantToShowContent()) {
if (shouldScrollToContent()) {
scrollToContent();
} else {
scrollToMenu();
}
}
recycleVelocityTracker();
break;
}
return true;
}
/**
* 判断当前手势的意图是不是想显示content。如果手指移动的距离是负数,且当前menu是可见的,则认为当前手势是想要显示content。
*
* @return 当前手势想显示content返回true,否则返回false。
*/
private boolean wantToShowContent() {
return xUp - xDown < 0 && isMenuVisible;
}
/**
* 判断当前手势的意图是不是想显示menu。如果手指移动的距离是正数,且当前menu是不可见的,则认为当前手势是想要显示menu。
*
* @return 当前手势想显示menu返回true,否则返回false。
*/
private boolean wantToShowMenu() {
return xUp - xDown > 0 && !isMenuVisible;
}
/**
* 判断是否应该滚动将menu展示出来。如果手指移动距离大于屏幕的1/2,或者手指移动速度大于SNAP_VELOCITY,
* 就认为应该滚动将menu展示出来。
*
* @return 如果应该滚动将menu展示出来返回true,否则返回false。
*/
private boolean shouldScrollToMenu() {
return xUp - xDown > screenWidth / 2
|| getScrollVelocity() > SNAP_VELOCITY;
}
/**
* 判断是否应该滚动将content展示出来。如果手指移动距离加上menuPadding大于屏幕的1/2,
* 或者手指移动速度大于SNAP_VELOCITY, 就认为应该滚动将content展示出来。
*
* @return 如果应该滚动将content展示出来返回true,否则返回false。
*/
private boolean shouldScrollToContent() {
return xDown - xUp + menuPadding > screenWidth / 2
|| getScrollVelocity() > SNAP_VELOCITY;
}
/**
* 将屏幕滚动到menu界面,滚动速度设定为30.
*/
private void scrollToMenu() {
new ScrollTask().execute(30);
}
/**
* 将屏幕滚动到content界面,滚动速度设定为-30.
*/
private void scrollToContent() {
new ScrollTask().execute(-30);
}
/**
* 创建VelocityTracker对象,并将触摸content界面的滑动事件加入到VelocityTracker当中。
*
* @param event
* content界面的滑动事件
*/
private void createVelocityTracker(MotionEvent event) {
if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();
}
mVelocityTracker.addMovement(event);
}
/**
* 获取手指在content界面滑动的速度。
*
* @return 滑动速度,以每秒钟移动了多少像素值为单位。
*/
private int getScrollVelocity() {
mVelocityTracker.computeCurrentVelocity(1000);
int velocity = (int) mVelocityTracker.getXVelocity();
return Math.abs(velocity);
}
/**
* 回收VelocityTracker对象。
*/
private void recycleVelocityTracker() {
mVelocityTracker.recycle();
mVelocityTracker = null;
}
class ScrollTask extends AsyncTask<Integer, Integer, Integer> {
@Override
protected Integer doInBackground(Integer... speed) {
int leftMargin = menuParams.leftMargin;
// 根据传入的速度来滚动界面,当滚动到达左边界或右边界时,跳出循环。
while (true) {
leftMargin = leftMargin + speed[0];
if (leftMargin > rightEdge) {
leftMargin = rightEdge;
break;
}
if (leftMargin < leftEdge) {
leftMargin = leftEdge;
break;
}
publishProgress(leftMargin);
// 为了要有滚动效果产生,每次循环使线程睡眠20毫秒,这样肉眼才能够看到滚动动画。
sleep(20);
}
if (speed[0] > 0) {
isMenuVisible = true;
} else {
isMenuVisible = false;
}
return leftMargin;
}
@Override
protected void onProgressUpdate(Integer... leftMargin) {
menuParams.leftMargin = leftMargin[0];
menu.setLayoutParams(menuParams);
}
@Override
protected void onPostExecute(Integer leftMargin) {
menuParams.leftMargin = leftMargin;
menu.setLayoutParams(menuParams);
}
}
/**
* 使当前线程睡眠指定的毫秒数。
*
* @param millis
* 指定当前线程睡眠多久,以毫秒为单位
*/
private void sleep(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@转载请注明出处http://blog.csdn.net/zjbpku/