Android SlidingMenu 显示侧滑页

释放双眼,带上耳机,听听看~!

项目当中用到了 侧滑功能 所以在网上找了一些Demo研究一下,但是 我的侧滑页是显示在ViewPager 的某个Fragment 的页面上。而Demo都是直接写在Activity上的,苦逼的我就自己琢磨。把Sliding的控制代码写到我的Fragment上面。结果 当然是不行的。正确的做法是 将控制代码写在MainActivity上,然后让对应的Fragment进行调用。还有如果你也用ViewPage 而且也需要Sliding的滑动展示
,那么两个滑动事件会冲突。我项目暂时没遇到 我就先不研究了。。。。。。

直接上代码。所有应该注意的东西都在注释上写着 不在赘述

activity 代码

package com.ingkoo.SlidingMenu;
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.ingkoo.R;
public class SlidingActivity extends Activity implements OnClickListener{
SlidingMenu mSlidingMenu;
// private ListView list_view,list_rightView;
// private ListAdapter adapter,adapter1;;
// public String[] info={"你好","�ջ�","�ٺϻ�","õ��","���","޹�²�","���ӻ�","��õ��"};
// public String[] info1={"逗你玩","�������","�����б�����","��������","��������","�˳�"};
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.main);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
mSlidingMenu = (SlidingMenu) findViewById(R.id.slidingMenu1);
mSlidingMenu.setAlignScreenWidth((dm.widthPixels / 5) * 2);
View leftView=getLayoutInflater().inflate(R.layout.left_menu, null);
View rightView=getLayoutInflater().inflate(R.layout.right_menu, null);
View centerView=getLayoutInflater().inflate(R.layout.center, null);
//    list_view=(ListView)centerView.findViewById(R.id.main_listview);
//    ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
//    for(String s:info)
//       adapter.add(s);
//    list_view.setAdapter(adapter);
//
//    list_rightView=(ListView)rightView.findViewById(R.id.right_listview);
//    ArrayAdapter<String> adapter1=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,info1);
//    list_rightView.setAdapter(adapter1);
mSlidingMenu.setLeftView(leftView);
mSlidingMenu.setRightView(rightView);
mSlidingMenu.setCenterView(centerView);
Button showLeftMenu=(Button)centerView.findViewById(R.id.btn_Left);
showLeftMenu.setOnClickListener(this);
Button showRightMenu=(Button)centerView.findViewById(R.id.btn_Righ);
showRightMenu.setOnClickListener(this);
}
//左右侧滑 这里没有写死 暂时禁止左侧View
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_Left:
//       mSlidingMenu.showLeftView();
break;
        case R.id.btn_Righ:
mSlidingMenu.showRightView();
         break;
      default:
break;
}
}
}

SlidingMenu代码

package com.ingkoo.SlidingMenu;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
public class SlidingMenu extends RelativeLayout {
private SlidingView mSlidingView;
   private View mLeftView;
   private View mRightView;
// menu width
private int alignScreenWidth;
   public SlidingMenu(Context context) {
super(context);
}
public SlidingMenu(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SlidingMenu(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setAlignScreenWidth(int alignScreenWidth) {
this.alignScreenWidth = alignScreenWidth;
}
public void setLeftView(View view) {
LayoutParams behindParams = new LayoutParams(alignScreenWidth,
LayoutParams.MATCH_PARENT);
addView(view, behindParams);
mLeftView = view;
}
public void setRightView(View view) {
LayoutParams behindParams = new LayoutParams(alignScreenWidth,
LayoutParams.MATCH_PARENT);
behindParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
addView(view, behindParams);
mRightView = view;
}
public void setCenterView(View view) {
LayoutParams aboveParams = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
mSlidingView = new SlidingView(getContext());
addView(mSlidingView, aboveParams);
mSlidingView.setView(view);
mSlidingView.invalidate();
mSlidingView.setLeftView(mLeftView);
mSlidingView.setRightView(mRightView);
}
//左侧视图
public void showLeftView() {
mSlidingView.showLeftView();
}
//右侧视图
public void showRightView() {
mSlidingView.showRightView();
}
//默认的中间视图
public void showCenterView() {
mSlidingView.showCenterView();
}
}

SlidingVeiw代码

package com.ingkoo.SlidingMenu;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.Scroller;
public class SlidingView extends ViewGroup {
private FrameLayout mContainer;
   private Scroller mScroller;
   private VelocityTracker mVelocityTracker;
   private int mTouchSlop;
   private float mLastMotionX;
   private float mLastMotionY;
   private static final int SNAP_VELOCITY = 1000;
   private View mLeftView;
   private View mRightView;
   public SlidingView(Context context) {
super(context);
init();
}
public SlidingView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public SlidingView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mContainer.measure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int width = r - l;
      final int height = b - t;
mContainer.layout(0, 0, width, height);
}
private void init() {
mContainer = new FrameLayout(getContext());
mContainer.setBackgroundColor(0xff000000);
mScroller = new Scroller(getContext());
mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
      super.addView(mContainer);
}
public void setView(View v) {
if (mContainer.getChildCount() > 0) {
mContainer.removeAllViews();
}
mContainer.addView(v);
}
@Override
public void scrollTo(int x, int y) {
super.scrollTo(x, y);
postInvalidate();
}
@Override
public void computeScroll() {
if (!mScroller.isFinished()) {
if (mScroller.computeScrollOffset()) {
int oldX = getScrollX();
            int oldY = getScrollY();
            int x = mScroller.getCurrX();
            int y = mScroller.getCurrY();
            if (oldX != x || oldY != y) {
scrollTo(x, y);
}
// Keep on drawing until the animation has finished.
invalidate();
} else {
clearChildrenCache();
}
} else {
clearChildrenCache();
}
}
private boolean mIsBeingDragged;
/**
     * ontuch 事件 这里暂时先禁止掉(项目需求点击显示即可 哈哈)
     */
// @Override
// public boolean onInterceptTouchEvent(MotionEvent ev) {
//
//    final int action = ev.getAction();
//    final float x = ev.getX();
//    final float y = ev.getY();
//
//    switch (action) {
//    case MotionEvent.ACTION_DOWN:
//       mLastMotionX = x;
//       mLastMotionY = y;
//       mIsBeingDragged = false;
//       break;
//
//    case MotionEvent.ACTION_MOVE:
//       final float dx = x - mLastMotionX;
//       final float xDiff = Math.abs(dx);
//       final float yDiff = Math.abs(y - mLastMotionY);
//       if (xDiff > mTouchSlop && xDiff > yDiff) {
//          mIsBeingDragged = true;
//          mLastMotionX = x;
//       }
//       Log.d("Sliding", "SlidingView_Touch:"+x+"|"+y);
//       Log.d("Sliding", "SlidingView_Touch:"+xDiff+"|"+mTouchSlop+"|"+yDiff+"|"+mLastMotionY);
//       Log.d("Sliding", "SlidingView_Touch:"+mIsBeingDragged);
//       break;
//
//    }
//    return mIsBeingDragged;
// }
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();
}
mVelocityTracker.addMovement(ev);
      final int action = ev.getAction();
      final float x = ev.getX();
      final float y = ev.getY();
      switch (action) {
case MotionEvent.ACTION_DOWN:
if (!mScroller.isFinished()) {
mScroller.abortAnimation();
}
mLastMotionX = x;
mLastMotionY = y;
         if (getScrollX() == -getLeftMenuWidth()
&& mLastMotionX < getLeftMenuWidth()) {
return false;
}
if (getScrollX() == getRightMenuWidth()
&& mLastMotionX > getLeftMenuWidth()) {
return false;
}
break;
      case MotionEvent.ACTION_MOVE:
if (mIsBeingDragged) {
enableChildrenCache();
            final float deltaX = mLastMotionX - x;
mLastMotionX = x;
            float oldScrollX = getScrollX();
            float scrollX = oldScrollX + deltaX;
            if (deltaX < 0 && oldScrollX < 0) { // left view
final float leftBound = 0;
               final float rightBound = -getLeftMenuWidth();
               if (scrollX > leftBound) {
scrollX = leftBound;
} else if (scrollX < rightBound) {
scrollX = rightBound;
}
} else if (deltaX > 0 && oldScrollX > 0) { // right view
final float rightBound = getRightMenuWidth();
               final float leftBound = 0;
               if (scrollX < leftBound) {
scrollX = leftBound;
} else if (scrollX > rightBound) {
scrollX = rightBound;
}
}
scrollTo((int) scrollX, getScrollY());
            if (scrollX > 0) {
mLeftView.setVisibility(View.GONE);
mLeftView.clearFocus();
mRightView.setVisibility(View.VISIBLE);
mRightView.requestFocus();
} else {
mLeftView.setVisibility(View.VISIBLE);
mLeftView.requestFocus();
mRightView.setVisibility(View.GONE);
mRightView.clearFocus();
}
}
break;
      case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
if (mIsBeingDragged) {
final VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(1000);
            int velocityX = (int) velocityTracker.getXVelocity();
velocityX = 0;
            int oldScrollX = getScrollX();
            int dx = 0;
            if (oldScrollX < 0) {
// ���
if (oldScrollX < -getLeftMenuWidth() / 2
|| velocityX > SNAP_VELOCITY) {
// ���ҳ�滮��
dx = -getLeftMenuWidth() - oldScrollX;
} else if (oldScrollX >= -getLeftMenuWidth() / 2
|| velocityX < -SNAP_VELOCITY) {
// ���ҳ��ر�
dx = -oldScrollX;
}
} else {
// �ұ�
if (oldScrollX > getRightMenuWidth() / 2
|| velocityX < -SNAP_VELOCITY) {
// �Ҳ�ҳ�滮��
dx = getRightMenuWidth() - oldScrollX;
} else if (oldScrollX <= getRightMenuWidth() / 2
|| velocityX > SNAP_VELOCITY) {
// �Ҳ�ҳ��ر�
dx = -oldScrollX;
}
}
smoothScrollTo(dx);
clearChildrenCache();
}
break;
}
if (mVelocityTracker != null) {
mVelocityTracker.recycle();
mVelocityTracker = null;
}
return true;
}
private int getLeftMenuWidth() {
if (mLeftView == null) {
return 0;
}
return mLeftView.getWidth();
}
private int getRightMenuWidth() {
if (mRightView == null) {
return 0;
}
return mRightView.getWidth();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
public View getRightView() {
return mRightView;
}
public void setRightView(View mRightView) {
this.mRightView = mRightView;
}
public View getMenuView() {
return mLeftView;
}
public void setLeftView(View mLeftView) {
this.mLeftView = mLeftView;
}
void toggle() {
int menuWidth = mLeftView.getWidth();
      int oldScrollX = getScrollX();
      if (oldScrollX == 0) {
smoothScrollTo(-menuWidth);
} else if (oldScrollX == -menuWidth) {
smoothScrollTo(menuWidth);
}
}
/**
    * 

人已赞赏
Android文章

Android URLEncoder 转译中文

2020-3-28 5:33:13

Android文章

AndroidStudio 导入第三方库 Manifest 覆盖

2020-3-28 6:48:07

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索