客户端的侧滑效果,简单实用,附如下代码:
其中Scroller的代码就不沾了,和系统提供的Scroller是差不多的,可参照源码.
private View content; private View menu; private boolean isMenuShow = false; public MySlideView(Context context, AttributeSet attrs) { super(context, attrs); scroller = new Scroller(context); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); menu = getChildAt(0); content = getChildAt(1); menu.measure(MeasureSpec.makeMeasureSpec(menu.getLayoutParams().width, MeasureSpec.EXACTLY) , heightMeasureSpec); content.measure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { System.out.println("layout:::"+getChildCount()); menu.layout(0-menu.getMeasuredWidth(), 0, 0, b); content.layout(0, 0, r, b); } private int firstX; private int lastX; @Override public boolean onTouchEvent(MotionEvent event) { super.onTouchEvent(event); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: firstX = lastX = (int) event.getX(); break; case MotionEvent.ACTION_MOVE: int disX = (int) (lastX - event.getX()); lastX = (int) event.getX(); int nextScrollX = getScrollX()+disX; // 可能的,下一个 mScrollX 的值 if( nextScrollX >= -menu.getWidth() && nextScrollX <=0){ scrollBy(disX, 0); } break; case MotionEvent.ACTION_UP: int curScrollX = getScrollX(); if(curScrollX > -menu.getWidth()/2){ isMenuShow = false; }else{ isMenuShow = true; } flushState(); break; } return true; } private void flushState() { int distance = 0; if(!isMenuShow){ // scrollTo(0,0); distance = 0-getScrollX(); }else{ // scrollTo(-menu.getWidth(),0); distance = -menu.getWidth()-getScrollX(); } scroller.startScroll(getScrollX(), 0, distance, 0); invalidate(); } @Override public void computeScroll() { if(scroller.computeScrollOffset()){ scrollTo(scroller.getCurrX(),0); invalidate(); } } private Scroller scroller ; public void changeState() { isMenuShow = !isMenuShow; flushState(); }
还有在activity中处理点击事件,附如下代码:
private MySlideView msv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
msv = (MySlideView) findViewById(R.id.msv);
findViewById(R.id.main_back).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
msv.changeState();
}
});
}
public void onClick(View v){
TextView tv = (TextView) v;
Toast.makeText(this,tv.getText(), 0).show();
}