extends LinearLayout 实现侧滑菜单

来源:互联网 发布:a站ceo莫然离职 知乎 编辑:程序博客网 时间:2024/05/21 09:50
package com.ce.MyLayout;import android.content.Context;import android.util.AttributeSet;import android.widget.LinearLayout;import android.widget.Scroller;public class SlideLinearLayout extends LinearLayout {private boolean ishow = false;private Scroller mScroller = null;private int distance;public SlideLinearLayout(Context context, AttributeSet attrs) {super(context, attrs);mScroller = new Scroller(context);// TODO Auto-generated constructor stub}@Overridepublic void computeScroll() {if (mScroller.computeScrollOffset()) {if (ishow) {scrollTo(mScroller.getCurrX(), 0);} else {scrollTo(mScroller.getCurrX() - distance, 0);}postInvalidate();}}public void beginScroll(Boolean isShow, int odistance,int duration) {distance=odistance;if (!ishow) {mScroller.startScroll(0, 0, -distance, 0, duration);ishow = true;} else {mScroller.startScroll(0, 0, distance, 0, duration);ishow = false;}invalidate();}public boolean Ishow() {return ishow;}}

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="fill_parent"      android:layout_height="fill_parent"      android:orientation="vertical" >        <Button          android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:text="scroll"          android:onClick="scroll" />        <com.wb.SlideLinearLayout         xmlns:android="http://schemas.android.com/apk/res/android"          android:layout_width="fill_parent"          android:layout_height="fill_parent"          android:orientation="vertical" android:id="@+id/myviewGroup">            <TextView              android:layout_width="wrap_content"              android:layout_height="fill_parent"              android:background="#ff0000"              android:text="我在這"/>            </com.wb.SlideLinearLayout>    </FrameLayout>  

要点:

1 FrameLayout布局的特性

2 对以下方法的理解:

2.1 Override computeScroll()

2.2 Scroller



0 0