看资料仿照做了一个
MainActivity.java:
package com.example.sildingmenudemo;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout leftViewGroup = createLeftView();
LinearLayout rightViewGroup = createRightView();
final MySildingMenu mSlidingMenu = new MySildingMenu(this);
mSlidingMenu.addLeftView(leftViewGroup);
mSlidingMenu.addRightView(rightViewGroup);
setContentView(mSlidingMenu);
}
private LinearLayout createRightView() {
LinearLayout rightViewGroup = new LinearLayout(this);
rightViewGroup.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
rightViewGroup.setBackgroundColor(Color.BLUE);
TextView rightView = new TextView(this);
rightView.setText("点击当前蓝色区域");
rightViewGroup.addView(rightView);
return rightViewGroup;
}
private LinearLayout createLeftView() {
LinearLayout leftViewGroup = new LinearLayout(this);
leftViewGroup.setLayoutParams(new LayoutParams(300, LayoutParams.FILL_PARENT));
leftViewGroup.setBackgroundColor(Color.GREEN);
TextView leftView = new TextView(this);
leftView.setText("Left View");
leftViewGroup.addView(leftView);
return leftViewGroup;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
以上注意:
leftViewGroup 和 rightViewGroup 其实相当于rightViewGroup 盖在了leftViewGroup之上
MySildingMenu:
package com.example.sildingmenudemo;
import android.content.Context;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;
public class MySildingMenu extends ViewGroup {
private View mRightView;
private View mLeftView;
private int mLastX;
private int mMotionX;
private ScrollRunnable mScrollRunnable;
public MySildingMenu(Context context) {
super(context);
}
public MySildingMenu(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MySildingMenu(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void addLeftView(View leftView) {
mLeftView = leftView;
addView(leftView);
}
/**
* 提供右侧显示视图
*
* @param rightView
*/
public void addRightView(View rightView) {
mRightView = rightView;
addView(rightView);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
measureChildren(widthMeasureSpec, heightMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(widthSize, heightSize);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View childView = getChildAt(i);
int measureWidth = childView.getMeasuredWidth();
int measureHeight = childView.getMeasuredHeight();
childView.layout(l, 0, l + measureWidth, measureHeight);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
final int x = (int) event.getX();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mLastX = x;
mMotionX = x;
if (mScrollRunnable != null) {
mScrollRunnable.endScroll();
mScrollRunnable = null;
}
boolean inRegion = canSliding(event);
return inRegion;
case MotionEvent.ACTION_MOVE:
scrollIfNeed(x);
return true;
case MotionEvent.ACTION_UP:
autoScrollifNeed(x);
break;
default:
break;
}
return super.onTouchEvent(event);
}
private boolean canSliding(MotionEvent event) {
int[] location = new int[2];
mRightView.getLocationOnScreen(location);
RectF region = new RectF();
region.set(location[0], location[1], location[0] + mRightView.getWidth(), location[1]
+ mRightView.getHeight());
boolean isRegion = region.contains(event.getRawX(), event.getRawY());
return isRegion;
}
private void scrollIfNeed(final int x) {
int deltaX = x - mLastX;
if (x != mLastX) {
int l = mRightView.getLeft();
int t = mRightView.getTop();
int b = mRightView.getBottom();
int rightViewleft = Math.max(mLeftView.getLeft(), l + deltaX);
rightViewleft = Math.min(mLeftView.getRight(), rightViewleft);
mRightView.layout(rightViewleft, t, rightViewleft + mRightView.getWidth(), b);
}
mLastX = x;
}
private void autoScrollifNeed(final int x) {
mScrollRunnable = new ScrollRunnable();
final int deltaX = x - mMotionX;
boolean moveLeft = deltaX <= 0;
final int distance = Math.abs(deltaX);
if (distance < mLeftView.getWidth() / 2) {
moveLeft = !moveLeft;
}
mScrollRunnable.startScroll(moveLeft);
}
private class ScrollRunnable implements Runnable {
private Scroller mScroller = new Scroller(getContext());
@Override
public void run() {
// TODO Auto-generated method stub
final Scroller scroller = mScroller;
boolean more = scroller.computeScrollOffset();
final int x = scroller.getCurrX();
if (more) {
scrollIfNeed(x);
post(this);
} else {
endScroll();
}
}
private void startScroll(boolean moveLeft) {
mLastX = mRightView.getLeft();
int dx = 0;
if (moveLeft) {
dx = mLeftView.getLeft() - mRightView.getLeft();
} else {
dx = mLeftView.getRight() - mRightView.getLeft();
}
mScroller.startScroll(mRightView.getLeft(), 0, dx, 0, 300);
post(this);
}
private void endScroll() {
removeCallbacks(this);
}
}
}