bgShade.removeAllViews();
bgShade.addView(bgShadeContent, bgParams);
if (getChildCount() > 1) {
removeViewAt(1);
removeViewAt(1);
}
addView(bgShade, bgParams);
LayoutParams aboveParams = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
addView(view, aboveParams);
mSlidView = view;
mSlidView.bringToFront();
System.out.println(getChildCount());
}
/**
* @return 获得菜单的宽度
*/
private int getMenuViewWidth() {
if (mMenuView == null) {
return 0;
}
return mMenuView.getWidth();
}
/**
* @param velocity
* 判断菜单的打开或关闭
*/
private void showVelocityMenuView(int velocity) {
if (velocity > 0) {
if (velocity > SNAP_VELOCITY) {
smoothScrollTo(-(menuWidth + mSlidView.getScrollX()));
setMenuState();
} else {
if (mSlidView.getScrollX() > -ConstantQuantity.screenWidth / 2) {
smoothScrollTo(-mSlidView.getScrollX());
} else {
smoothScrollTo(-(menuWidth + mSlidView.getScrollX()));
setMenuState();
}
}
}
if (velocity < 0) {
if (velocity < -SNAP_VELOCITY) {
smoothScrollTo(-mSlidView.getScrollX());
setMenuState();
} else {
if (mSlidView.getScrollX() > -ConstantQuantity.screenWidth / 2) {
smoothScrollTo(-mSlidView.getScrollX());
} else {
smoothScrollTo(-(menuWidth + mSlidView.getScrollX()));
setMenuState();
}
}
}
if (velocity == 0) {
if (mSlidView.getScrollX() > -(ConstantQuantity.screenWidth - ConstantQuantity.screenDensity * 50) / 2) {
smoothScrollTo(-mSlidView.getScrollX());
} else {
smoothScrollTo(-(menuWidth + mSlidView.getScrollX()));
setMenuState();
}
}
}
/**
* 去打开或者关闭菜单界面
*/
public void openMenuView() {
menuWidth = getMenuViewWidth();
if (mSlidView.getScrollX() == 0) {
// mMenuView.setVisibility(View.VISIBLE);
smoothScrollTo(-menuWidth);
setMenuState();
} else if (mSlidView.getScrollX() == -menuWidth) {
smoothScrollTo(menuWidth);
setMenuState();
}
}
/**
* 真正的实现打开与关闭的滑动效果
* @param distanceX
*/
private void smoothScrollTo(int distanceX) {
mScroller.startScroll(mSlidView.getScrollX(), 0,
distanceX, 0, ConstantQuantity.DURATION_TIME);
invalidate();
}
/**
* 设置菜单打开或关闭状态
*/
private void setMenuState() {
if (menuState == ConstantQuantity.MENU_STATE_CLOSE) {
menuState = ConstantQuantity.MENU_STATE_OPEN;
// mSlidView.setEnabled(false);
System.out.println("菜单将打开");
} else {
menuState = ConstantQuantity.MENU_STATE_CLOSE;
// mSlidView.setEnabled(true);
System.out.println("菜单将关闭");
}
}
}
本文DEMO源码下载
------------------------------------------分割线------------------------------------------
具体下载目录在 /2015年资料/2月/21日/Android自定义侧滑菜单slidmenu/
------------------------------------------分割线------------------------------------------