Java代码
- 下面看看我们如何使用它,达到我们想要的效果
- public class MainActivity extends Activity {
-
-
- private SliderMenu SliderMenu;
-
-
- private ListView contentList;
-
-
- private ArrayAdapter<String> contentListAdapter;
-
-
- private String[] contentItems = { "Content Item 1", "Content Item 2", "Content Item 3",
- "Content Item 4", "Content Item 5", "Content Item 6", "Content Item 7",
- "Content Item 8", "Content Item 9", "Content Item 10", "Content Item 11",
- "Content Item 12", "Content Item 13", "Content Item 14", "Content Item 15",
- "Content Item 16" };
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- SliderMenu = (SliderMenu) findViewById(R.id.bidir_sliding_layout);
- contentList = (ListView) findViewById(R.id.contentList);
- contentListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
- contentItems);
- contentList.setAdapter(contentListAdapter);
- SliderMenu.setScrollEvent(contentList);
- }
-
- }
Java代码
- 这个类就是实现双向滑动菜单功能的核心类
-
- public class SliderMenu extends RelativeLayout implements OnTouchListener {
-
-
- public static final int SNAP_VELOCITY = 200;
-
-
- public static final int DO_NOTHING = 0;
-
-
- public static final int SHOW_LEFT_MENU = 1;
-
-
- public static final int SHOW_RIGHT_MENU = 2;
-
-
- public static final int HIDE_LEFT_MENU = 3;
-
-
- public static final int HIDE_RIGHT_MENU = 4;
-
-
- private int slideState;
-
-
- private int screenWidth;
-
-
- private int touchSlop;
-
-
- private float xDown;
-
-
- private float yDown;
-
-
- private float xMove;
-
-
- private float yMove;
-
-
- private float xUp;
-
-
- private boolean isLeftMenuVisible;
-
-
- private boolean isRightMenuVisible;
-
-
- private boolean isSliding;
-
-
- private View leftMenuLayout;
-
-
- private View rightMenuLayout;
-
-
- private View contentLayout;
-
-
- private View mBindView;
-
-
- private MarginLayoutParams leftMenuLayoutParams;
-
-
- private MarginLayoutParams rightMenuLayoutParams;
-
-
- private RelativeLayout.LayoutParams contentLayoutParams;
-
-
- private VelocityTracker mVelocityTracker;
-
-
- public SliderMenu(Context context, AttributeSet attrs) {
- super(context, attrs);
- WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
- screenWidth = wm.getDefaultDisplay().getWidth();
- touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
- }
-
-
- public void setScrollEvent(View bindView) {
- mBindView = bindView;
- mBindView.setOnTouchListener(this);
- }
-
-
- public void scrollToLeftMenu() {
- new LeftMenuScrollTask().execute(-50);
- }
-
-
- public void scrollToRightMenu() {
- new RightMenuScrollTask().execute(-50);
- }
-
-
- public void scrollToContentFromLeftMenu() {
- new LeftMenuScrollTask().execute(50);
- }
-
-
- public void scrollToContentFromRightMenu() {
- new RightMenuScrollTask().execute(50);
- }
-
-
- public boolean isLeftLayoutVisible() {
- return isLeftMenuVisible;
- }
-
-
- public boolean isRightLayoutVisible() {
- return isRightMenuVisible;
- }
-
-
- @Override
- protected void onLayout(boolean changed, int l, int t, int r, int b) {
- super.onLayout(changed, l, t, r, b);
- if (changed) {
-
- leftMenuLayout = getChildAt(0);
- leftMenuLayoutParams = (MarginLayoutParams) leftMenuLayout.getLayoutParams();
-
- rightMenuLayout = getChildAt(1);
- rightMenuLayoutParams = (MarginLayoutParams) rightMenuLayout.getLayoutParams();
-
- contentLayout = getChildAt(2);
- contentLayoutParams = (RelativeLayout.LayoutParams) contentLayout.getLayoutParams();
- contentLayoutParams.width = screenWidth;
- contentLayout.setLayoutParams(contentLayoutParams);
- }
- }
-
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- createVelocityTracker(event);
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
-
- xDown = event.getRawX();
- yDown = event.getRawY();
-
- slideState = DO_NOTHING;
- break;
- case MotionEvent.ACTION_MOVE:
- xMove = event.getRawX();
- yMove = event.getRawY();
-
- int moveDistanceX = (int) (xMove - xDown);
- int moveDistanceY = (int) (yMove - yDown);
-
- checkSlideState(moveDistanceX, moveDistanceY);
-
- switch (slideState) {
- case SHOW_LEFT_MENU:
- contentLayoutParams.rightMargin = -moveDistanceX;
- checkLeftMenuBorder();
- contentLayout.setLayoutParams(contentLayoutParams);
- break;
- case HIDE_LEFT_MENU:
- contentLayoutParams.rightMargin = -leftMenuLayoutParams.width - moveDistanceX;
- checkLeftMenuBorder();
- contentLayout.setLayoutParams(contentLayoutParams);
- case SHOW_RIGHT_MENU:
- contentLayoutParams.leftMargin = moveDistanceX;
- checkRightMenuBorder();
- contentLayout.setLayoutParams(contentLayoutParams);
- break;
- case HIDE_RIGHT_MENU:
- contentLayoutParams.leftMargin = -rightMenuLayoutParams.width + moveDistanceX;
- checkRightMenuBorder();
- contentLayout.setLayoutParams(contentLayoutParams);
- default:
- break;
- }
- break;
- case MotionEvent.ACTION_UP:
- xUp = event.getRawX();
- int upDistanceX = (int) (xUp - xDown);
- if (isSliding) {
-
- switch (slideState) {
- case SHOW_LEFT_MENU:
- if (shouldScrollToLeftMenu()) {
- scrollToLeftMenu();
- } else {
- scrollToContentFromLeftMenu();
- }
- break;
- case HIDE_LEFT_MENU:
- if (shouldScrollToContentFromLeftMenu()) {
- scrollToContentFromLeftMenu();
- } else {
- scrollToLeftMenu();
- }
- break;
- case SHOW_RIGHT_MENU:
- if (shouldScrollToRightMenu()) {
- scrollToRightMenu();
- } else {
- scrollToContentFromRightMenu();
- }
- break;
- case HIDE_RIGHT_MENU:
- if (shouldScrollToContentFromRightMenu()) {
- scrollToContentFromRightMenu();
- } else {
- scrollToRightMenu();
- }
- break;
- default:
- break;
- }
- } else if (upDistanceX < touchSlop && isLeftMenuVisible) {
-
- scrollToContentFromLeftMenu();
- } else if (upDistanceX < touchSlop && isRightMenuVisible) {
-
- scrollToContentFromRightMenu();
- }
- recycleVelocityTracker();
- break;
- }
- if (v.isEnabled()) {
- if (isSliding) {
-
- unFocusBindView();
- return true;
- }
- if (isLeftMenuVisible || isRightMenuVisible) {
-
- return true;
- }
- return false;
- }
- return true;
- }
-
-
- private void checkSlideState(int moveDistanceX, int moveDistanceY) {
- if (isLeftMenuVisible) {
- if (!isSliding && Math.abs(moveDistanceX) >= touchSlop && moveDistanceX < 0) {
- isSliding = true;
- slideState = HIDE_LEFT_MENU;
- }
- } else if (isRightMenuVisible) {
- if (!isSliding && Math.abs(moveDistanceX) >= touchSlop && moveDistanceX > 0) {
- isSliding = true;
- slideState = HIDE_RIGHT_MENU;
- }
- } else {
- if (!isSliding && Math.abs(moveDistanceX) >= touchSlop && moveDistanceX > 0
- && Math.abs(moveDistanceY) < touchSlop) {
- isSliding = true;
- slideState = SHOW_LEFT_MENU;
- contentLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
- contentLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
- contentLayout.setLayoutParams(contentLayoutParams);
-
- leftMenuLayout.setVisibility(View.VISIBLE);
- rightMenuLayout.setVisibility(View.GONE);
- } else if (!isSliding && Math.abs(moveDistanceX) >= touchSlop && moveDistanceX < 0
- && Math.abs(moveDistanceY) < touchSlop) {
- isSliding = true;
- slideState = SHOW_RIGHT_MENU;
- contentLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
- contentLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
- contentLayout.setLayoutParams(contentLayoutParams);
-
- rightMenuLayout.setVisibility(View.VISIBLE);
- leftMenuLayout.setVisibility(View.GONE);
- }
- }
- }
-
-
- private void checkLeftMenuBorder() {
- if (contentLayoutParams.rightMargin > 0) {
- contentLayoutParams.rightMargin = 0;
- } else if (contentLayoutParams.rightMargin < -leftMenuLayoutParams.width) {
- contentLayoutParams.rightMargin = -leftMenuLayoutParams.width;
- }
- }
-
-
- private void checkRightMenuBorder() {
- if (contentLayoutParams.leftMargin > 0) {
- contentLayoutParams.leftMargin = 0;
- } else if (contentLayoutParams.leftMargin < -rightMenuLayoutParams.width) {
- contentLayoutParams.leftMargin = -rightMenuLayoutParams.width;
- }
- }
-
-
- private boolean shouldScrollToLeftMenu() {
- return xUp - xDown > leftMenuLayoutParams.width / 2 || getScrollVelocity() > SNAP_VELOCITY;
- }
-
-
- private boolean shouldScrollToRightMenu() {
- return xDown - xUp > rightMenuLayoutParams.width / 2 || getScrollVelocity() > SNAP_VELOCITY;
- }
-
-
- private boolean shouldScrollToContentFromLeftMenu() {
- return xDown - xUp > leftMenuLayoutParams.width / 2 || getScrollVelocity() > SNAP_VELOCITY;
- }
-
-
- private boolean shouldScrollToContentFromRightMenu() {
- return xUp - xDown > rightMenuLayoutParams.width / 2 || getScrollVelocity() > SNAP_VELOCITY;
- }
-
-
- private void createVelocityTracker(MotionEvent event) {
- if (mVelocityTracker == null) {
- mVelocityTracker = VelocityTracker.obtain();
- }
- mVelocityTracker.addMovement(event);
- }
-
-
- private int getScrollVelocity() {
- mVelocityTracker.computeCurrentVelocity(1000);
- int velocity = (int) mVelocityTracker.getXVelocity();
- return Math.abs(velocity);
- }
-
-
- private void recycleVelocityTracker() {
- mVelocityTracker.recycle();
- mVelocityTracker = null;
- }
-
-
- private void unFocusBindView() {
- if (mBindView != null) {
- mBindView.setPressed(false);
- mBindView.setFocusable(false);
- mBindView.setFocusableInTouchMode(false);
- }
- }
-
- class LeftMenuScrollTask extends AsyncTask<Integer, Integer, Integer> {
-
- @Override
- protected Integer doInBackground(Integer... speed) {
- int rightMargin = contentLayoutParams.rightMargin;
-
- while (true) {
- rightMargin = rightMargin + speed[0];
- if (rightMargin < -leftMenuLayoutParams.width) {
- rightMargin = -leftMenuLayoutParams.width;
- break;
- }
- if (rightMargin > 0) {
- rightMargin = 0;
- break;
- }
- publishProgress(rightMargin);
-
- sleep(15);
- }
- if (speed[0] > 0) {
- isLeftMenuVisible = false;
- } else {
- isLeftMenuVisible = true;
- }
- isSliding = false;
- return rightMargin;
- }
-
- @Override
- protected void onProgressUpdate(Integer... rightMargin) {
- contentLayoutParams.rightMargin = rightMargin[0];
- contentLayout.setLayoutParams(contentLayoutParams);
- unFocusBindView();
- }
-
- @Override
- protected void onPostExecute(Integer rightMargin) {
- contentLayoutParams.rightMargin = rightMargin;
- contentLayout.setLayoutParams(contentLayoutParams);
- }
- }
-
- class RightMenuScrollTask extends AsyncTask<Integer, Integer, Integer> {
-
- @Override
- protected Integer doInBackground(Integer... speed) {
- int leftMargin = contentLayoutParams.leftMargin;
-
- while (true) {
- leftMargin = leftMargin + speed[0];
- if (leftMargin < -rightMenuLayoutParams.width) {
- leftMargin = -rightMenuLayoutParams.width;
- break;
- }
- if (leftMargin > 0) {
- leftMargin = 0;
- break;
- }
- publishProgress(leftMargin);
-
- sleep(15);
- }
- if (speed[0] > 0) {
- isRightMenuVisible = false;
- } else {
- isRightMenuVisible = true;
- }
- isSliding = false;
- return leftMargin;
- }
-
- @Override
- protected void onProgressUpdate(Integer... leftMargin) {
- contentLayoutParams.leftMargin = leftMargin[0];
- contentLayout.setLayoutParams(contentLayoutParams);
- unFocusBindView();
- }
-
- @Override
- protected void onPostExecute(Integer leftMargin) {
- contentLayoutParams.leftMargin = leftMargin;
- contentLayout.setLayoutParams(contentLayoutParams);
- }
- }
-
-
- private void sleep(long millis) {
- try {
- Thread.sleep(millis);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- 复制代码
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- <com.example.slidermenu.SliderMenu
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/bidir_sliding_layout"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" >
-
- <RelativeLayout
- android:id="@+id/left_menu"
- android:layout_width="270dip"
- android:layout_height="fill_parent"
- android:layout_alignParentLeft="true"
- android:background="#00ccff"
- android:visibility="invisible" >
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:text="This is left menu"
- android:textColor="#000000"
- android:textSize="28sp" />
- </RelativeLayout>
-
- <RelativeLayout
- android:id="@+id/right_menu"
- android:layout_width="270dip"
- android:layout_height="fill_parent"
- android:layout_alignParentRight="true"
- android:background="#00ffcc"
- android:visibility="invisible" >
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:text="This is right menu"
- android:textColor="#000000"
- android:textSize="28sp" />
- </RelativeLayout>
-
- <LinearLayout
- android:id="@+id/content"
- android:layout_width="320dip"
- android:layout_height="fill_parent"
- android:background="#e9e9e9" >
-
- <ListView
- android:id="@+id/contentList"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:scrollbars="none"
- android:cacheColorHint="#00000000" >
- </ListView>
- </LinearLayout>
-
- </com.example.slidermenu.SliderMenu>
可以看到,我们使用了自定义的SliderMenu作为根布局,然后依次加入了三个子布局分别作为左侧菜单、右侧菜单和内容的布局。左侧菜单和右侧菜单中都只是简单地放入了一个TextView用于显示一段文字,内容布局中放入了一个ListView。注意要让左侧菜单和父布局左边缘对齐,右侧菜单和父布局右边缘对齐。