自定义SlideMenu

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>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值