这是在CSDN人家实现好的一个Demo,源代码中有点小bug,滑动的时候界面会花掉,残留滑动前的图片,所以我在每次滑动后刷新了ListView。天天动听现在里面就有这么一个滑动菜单。
滑动前:
滑动后:
实现过程如下:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/layout_right"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="50dp"
android:orientation="vertical" >
<AbsoluteLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/grey21"
android:padding="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="设置"
android:textColor="@android:color/background_light"
android:textSize="20sp" />
</AbsoluteLayout>
<ListView
android:id="@+id/lv_set"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
</ListView>
</LinearLayout>
<LinearLayout
android:id="@+id/layout_left"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/nav_bg" >
<ImageView
android:id="@+id/iv_set"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/nav_setting" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="我"
android:textColor="@android:color/background_light"
android:textSize="20sp" />
</RelativeLayout>
<ImageView
android:id="@+id/iv_set"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:src="@drawable/bg_guide_5" />
</LinearLayout>
</RelativeLayout>
MainActivity
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.view.Window;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class MainActivity extends Activity implements OnTouchListener,
GestureDetector.OnGestureListener {
private boolean hasMeasured = false;// 是否Measured.
private LinearLayout layout_left;
private LinearLayout layout_right;
private ImageView imageView;
private ListView listView;
/** 每次自动展开/收缩的范围 */
private int MAX_WIDTH = 0;
/** 每次自动展开/收缩的速度 */
private final static int SPEED = 1200;
private GestureDetector mGestureDetector;
private boolean isScrolling = false;
// 滑块滑动距离
private float mScrollX;
// 屏幕的宽度
private int window_width;
private String title[] = { "待发送队列", "ͬ同步分享设置", "编辑我的资料", "找朋友", "告诉朋友",
"节省流量", "推送设置", "版本更新", "意见反馈", "积分兑换", "精品应用", "常见问题", "退出当前帐号" };
void InitView() {
layout_left = (LinearLayout) findViewById(R.id.layout_left);
layout_right = (LinearLayout) findViewById(R.id.layout_right);
imageView = (ImageView) findViewById(R.id.iv_set);
listView = (ListView) findViewById(R.id.lv_set);
listView.setAdapter(new ArrayAdapter<String>(this, R.layout.item,
R.id.tv_item, title));
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, title[position], 1).show();
}
});
layout_left.setOnTouchListener(this);
imageView.setOnTouchListener(this);
mGestureDetector = new GestureDetector(this);
mGestureDetector.setIsLongpressEnabled(false);
getMAX_WIDTH();
}
/***
* 获取移动距离 移动的距离其实就是layout_right的宽度
*/
void getMAX_WIDTH() {
ViewTreeObserver viewTreeObserver = layout_left.getViewTreeObserver();
viewTreeObserver.addOnPreDrawListener(new OnPreDrawListener() {
public boolean onPreDraw() {
if (!hasMeasured) {
window_width = getWindowManager().getDefaultDisplay()
.getWidth();
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) layout_left
.getLayoutParams();
//左边的布局宽度等于屏幕宽度,即只显示左边部分
layoutParams.width = window_width;
layout_left.setLayoutParams(layoutParams);
MAX_WIDTH = layout_right.getWidth();
Log.v(TAG, "MAX_WIDTH=" + MAX_WIDTH + "width="
+ window_width);
hasMeasured = true;
}
return true;
}
});
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
InitView();
}
public boolean onTouch(View v, MotionEvent event) {
// 松开的时候要判断,如果不到半屏幕位子则缩回去
if (MotionEvent.ACTION_UP == event.getAction() && isScrolling == true) {
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) layout_left
.getLayoutParams();
if (layoutParams.leftMargin < -window_width / 2) {
new AsynMove().execute(-SPEED);
} else {
new AsynMove().execute(SPEED);
}
}
return mGestureDetector.onTouchEvent(event);
}
public boolean onDown(MotionEvent e) {
mScrollX = 0;
isScrolling = false;
// 将之改为true,不然事件不会向下传递
return true;
}
public void onShowPress(MotionEvent e) {
}
public boolean onSingleTapUp(MotionEvent e) {
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) layout_left
.getLayoutParams();
if (layoutParams.leftMargin >= 0) {
new AsynMove().execute(-SPEED);
} else {
new AsynMove().execute(SPEED);
}
return true;
}
/***
* 滑动监听 就是一个点移动到另外一个点. distanceX=后面点x-前面点x,如果大于0,说明后面点在前面点的右边及向右滑动
*/
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
isScrolling = true;
mScrollX += distanceX;
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) layout_left
.getLayoutParams();
layoutParams.leftMargin -= mScrollX;
if (layoutParams.leftMargin >= 0) {
isScrolling = false;
layoutParams.leftMargin = 0;
} else if (layoutParams.leftMargin <= -MAX_WIDTH) {
// 拖过头了不需要再执行AsynMove了
isScrolling = false;
layoutParams.leftMargin = -MAX_WIDTH;
}
layout_left.setLayoutParams(layoutParams);
listView.postInvalidate();
return false;
}
public void onLongPress(MotionEvent e) {
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
return false;
}
class AsynMove extends AsyncTask<Integer, Integer, Void> {
@Override
protected Void doInBackground(Integer... params) {
int times = 0;
if (MAX_WIDTH % Math.abs(params[0]) == 0)
times = MAX_WIDTH / Math.abs(params[0]);
else
times = MAX_WIDTH / Math.abs(params[0]) + 1;
for (int i = 0; i < times; i++) {
publishProgress(params[0]);
try {
Thread.sleep(Math.abs(params[0]));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
/**
* update UI
*/
@Override
protected void onProgressUpdate(Integer... values) {
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) layout_left
.getLayoutParams();
if (values[0] > 0) {
layoutParams.leftMargin = Math.min(layoutParams.leftMargin
+ values[0], 0);
} else {
layoutParams.leftMargin = Math.max(layoutParams.leftMargin
+ values[0], -MAX_WIDTH);
}
layout_left.setLayoutParams(layoutParams);
listView.postInvalidate();
}
}
}
参考链接:http://blog.csdn.net/jj120522/article/details/8075249