侧滑菜单HorizontalScrollView,类中如何使用intent跳转

来源:互联网 发布:linux运行级别介绍 编辑:程序博客网 时间:2024/05/21 13:21

布局文件main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <MyHorizontalScrollView        android:id="@+id/layout"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:scrollbars="none">        <LinearLayout            android:layout_width="match_parent"            android:layout_height="match_parent"            android:orientation="horizontal" >            <include layout="@layout/horizontalscrollview"/>            <ListView                 android:id="@+id/listView_msg"                 android:layout_width="match_parent"                 android:layout_height="match_parent"                 android:layout_marginBottom="65dp"></ListView>        </LinearLayout>    </MyHorizontalScrollView></LinearLayout>

侧滑的布局myhorizontalscrollView.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <Button        android:id="@+id/quit_button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/quit"/></LinearLayout>

HorizontalScrollView类

public class MyHorizontalScrollView extends HorizontalScrollView {    // 在HorizontalScrollView有个LinearLayout    private LinearLayout linearLayout;    // 菜单,内容页    private ViewGroup myMenu;    private ViewGroup myContent;    // 菜单宽度    private int myMenuWidth;    // 屏幕宽度    private int screenWidth;    // 菜单与屏幕右侧的距离(dp)    private int myMenuPaddingRight = 50;    // 避免多次调用onMeasure的标志    private boolean once = false;    public MyHorizontalScrollView(Context context, AttributeSet attrs) {        super(context, attrs);        // 获取屏幕宽度        WindowManager windowManager = (WindowManager) context                .getSystemService(Context.WINDOW_SERVICE);        DisplayMetrics outMetrics = new DisplayMetrics();        windowManager.getDefaultDisplay().getMetrics(outMetrics);        screenWidth = outMetrics.widthPixels;// 屏幕宽度        // 将dp转换px        myMenuPaddingRight = (int) TypedValue.applyDimension(                TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources()                        .getDisplayMetrics());    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        if (!once) {// 使其只调用一次            // this指的是HorizontalScrollView,获取各个元素            linearLayout = (LinearLayout) this.getChildAt(0);// 第一个子元素            myMenu = (ViewGroup) linearLayout.getChildAt(0);// HorizontalScrollView下LinearLayout的第一个子元素            //退出登录            Button quitButton = (Button) myMenu.findViewById(R.id.quit_button);            quitButton.setOnClickListener(new OnClickListener() {                @Override                public void onClick(View v) {                    Log.i("MyHorizontalScrollView", "quit");                    //退出登录                    EMClient.getInstance().logout(true);                    //跳转到登录界面(activity是context的子类)                    Intent intent = new Intent(getContext(), LoginActivity.class);                    getContext().startActivity(intent);                    //activity是context的子类,可以转换                    ((Activity)getContext()).finish();                 }            });            myContent = (ViewGroup) linearLayout.getChildAt(1);// HorizontalScrollView下LinearLayout的第二个子元素            // 设置子View的宽高,高于屏幕一致            myMenuWidth = myMenu.getLayoutParams().width = screenWidth                    - myMenuPaddingRight;// 菜单的宽度=屏幕宽度-右边距            myContent.getLayoutParams().width = screenWidth;// 内容宽度=屏幕宽度            // 决定自身View的宽高,高于屏幕一致            // 由于这里的LinearLayout里只包含了Menu和Content所以就不需要额外的去指定自身的宽            once = true;        }    }    @Override    protected void onLayout(boolean changed, int l, int t, int r, int b) {        super.onLayout(changed, l, t, r, b);        if (changed) {            this.scrollTo(myMenuWidth, 0); // 没有动画效果的隐藏        }    }    @Override    public boolean onTouchEvent(MotionEvent ev) {        int action = ev.getAction();        switch (action) {            case MotionEvent.ACTION_UP:                int scrollX = this.getScrollX();// 滑动的距离scrollTo方法里,也就是onMeasure方法里的向左滑动那部分                if (scrollX >= myMenuWidth / 2) {                    this.smoothScrollTo(myMenuWidth, 0);// 向左滑动展示内容                } else {                    this.smoothScrollTo(0, 0);                }                return true;        }        return super.onTouchEvent(ev);    }}
0 0