SlideLayout.java
public class SlideLayout extends FrameLayout{
private View menuView;
private int menuItemHeight,menuItemWidth;
private Scroller scroller;
private boolean isOpen= false;
public SlideLayout(Context context) {
super(context);
}
public SlideLayout(Context context, AttributeSet attrs) {
super(context, attrs);
scroller = new Scroller(context);
}
public SlideLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
menuView = getChildAt(1);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
menuItemHeight = menuView.getMeasuredHeight();
menuItemWidth = menuView.getMeasuredWidth();
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
menuView.layout(-menuItemWidth, 0, 0, menuItemHeight);
}
private int lastX=0;
@Override
public boolean onTouchEvent(MotionEvent event) {
int moveX ;
int action = event.getAction();
int eventX = (int) event.getRawX();
switch (action){
case MotionEvent.ACTION_DOWN:
lastX = eventX;
break;
case MotionEvent.ACTION_MOVE:
int dx = eventX-lastX;
moveX = getScrollX()-dx;
if(moveX<-menuItemWidth){
moveX = -menuItemWidth;
}else if(moveX>0){
moveX = 0;
}
scrollTo(moveX,getScrollY());
lastX = eventX;
break;
case MotionEvent.ACTION_UP:
moveX = getScrollX();
if(moveX <= -menuItemWidth/2){
openMenu();
}else{
closeMenu();
}
break;
}
return true;
}
private void closeMenu() {
isOpen = false;
scroller.startScroll(getScrollX(),getScrollY(),-getScrollX(),-getScrollY(),500);
invalidate();
}
private void openMenu() {
isOpen = true;
scroller.startScroll(getScrollX(), getScrollY(), -menuItemWidth - getScrollX(), getScrollY(), 500);
invalidate();
}
@Override
public void computeScroll() {
if(scroller.computeScrollOffset()){
scrollTo(scroller.getCurrX(),scroller.getCurrY());
invalidate();
}
}
public void toggleMenu() {
if(isOpen){
closeMenu();
}else{
openMenu();
}
}
}
item_content.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/top_bar_bg"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:drawableLeft="@drawable/main_back"
android:gravity="center"
android:textColor="#fff"
android:textSize="30sp"
android:text="新闻"
android:clickable="true"
android:id="@+id/tv_back"/>
</LinearLayout>
</LinearLayout>
item_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView"
android:layout_width="150dp"
android:layout_height="match_parent"
android:background="@drawable/menu_bg"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
style="@style/myTextStyle"
android:text="焦点"
android:drawableLeft="@drawable/tab_focus"
/>
<TextView
style="@style/myTextStyle"
android:text="本地"
android:drawableLeft="@drawable/tab_local"
/>
<TextView
style="@style/myTextStyle"
android:text="新闻"
android:drawableLeft="@drawable/tab_news"
/>
<TextView
style="@style/myTextStyle"
android:text="图片"
android:drawableLeft="@drawable/tab_pics"
/>
<TextView
style="@style/myTextStyle"
android:text="阅读"
android:drawableLeft="@drawable/tab_read"
/>
<TextView
style="@style/myTextStyle"
android:text="热帖"
android:drawableLeft="@drawable/tab_ties"
/>
<TextView
style="@style/myTextStyle"
android:text="点评"
android:drawableLeft="@drawable/tab_ugc"
/>
<TextView
style="@style/myTextStyle"
android:text="投票"
android:drawableLeft="@drawable/tab_vote"
/>
</LinearLayout>
</ScrollView>
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<com.xhly.sdv.slidemenu.view.SlideLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/slide_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/item_content" />
<include layout="@layout/item_menu"/>
</com.xhly.sdv.slidemenu.view.SlideLayout>