实现侧滑栏的三种形式

来源:互联网 发布:lg电视如何看网络电视 编辑:程序博客网 时间:2024/06/11 15:02

DrawerLayout

  • 介绍:
    1.DrawerLayout是系统定义的一个ViewGroup,包含了两个页面,然后你可以像使用其他的ViewGroup一样使用DrawerLayout.
  • 使用步骤
    1. 主内容区的布局代码要放在侧滑菜单的前面
    2. 侧滑栏部分必须设置layout_gravity属性,它表示侧滑栏在左边还是右边,否则DrawerLayout就不会将其识别为一个滑动的ViewGroup,
      3.在Java代码中将DrawerLayout获取出来,然后使用Button或者其他的事件来触发就可以了
  • 代码实现
    XML代码示例
<android.support.v4.widget.DrawerLayout                android:id="@+id/drawer_layout"                xmlns:tools="http://schemas.android.com/tools"                android:layout_width="match_parent"                android:layout_height="match_parent"                android:orientation="vertical"                tools:context=".MainActivity"                xmlns:android="http://schemas.android.com/apk/res/android">            <!-- The main content view -->                 <!-- 界面主要内容布局区 -->           <TextView               android:layout_width="match_parent"               android:layout_height="match_parent"               android:text="主界面"/>            <!-- The navigation drawer -->              <!-- 左边弹出布局区 -->            <TextView                android:background="@color/colorAccent"                android:layout_gravity="left"                android:layout_width="200dp"                android:layout_height="match_parent"                android:text="DrawerLayout抽屉效果"                />        </android.support.v4.widget.DrawerLayout>
Java代码示例
      public class MainActivity extends Activity{        private DrawerLayout drawerLayout;        @Override        protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initViews();        }        private void initViews() {        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);        }        //这里使用的是调用了触摸事件,然后调用DrawerLayout.openDrawer来对滑动事件做出反应        @Override        public boolean onTouchEvent(MotionEvent event) {        drawerLayout.openDrawer(Gravity.LEFT);        return true;        }        }

SlidingPaneLayout

  • 介绍: 这个更简单,直接在XML中声明出来就可以使用了,都不需要在Java代码中配置,SlidingPaneLayout的第一个控件是菜单控件,第二个控件是内容控件
  • 使用步骤:直接在XML文件中定义
  • 代码实现
<android.support.v4.widget.SlidingPaneLayout   xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/sliding_pane_layout"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView    android:layout_width="240dp"    android:layout_height="match_parent"    android:background="@color/colorAccent"    android:text="面板2"/>    <TextView    android:layout_width="400dp"    android:layout_height="match_parent"    android:layout_weight="1"    android:background="@color/colorPrimary"    android:text="面板1"/></android.support.v4.widget.SlidingPaneLayout>

ViewDragHelper

  • 介绍: ViewDragHelper是上面这两个的一个原理性的应该吧,通常定义在一个ViewGroup里面
  • 使用步骤 & 代码实现
//在加载完成XML后,将子View获取出来,初始化的一些操作        @Override        protected void onFinishInflate() {        super.onFinishInflate();        //将第一个view作为主要显示的内容        mMenuView = (TextView) getChildAt(0);        mMainView = (TextView) getChildAt(1);        }        //1.在ViewGroup的构造方法中初始化一个ViewDragHelper        private void initViewDragHelper(){        viewDragHelper = ViewDragHelper.create(                this,                new ViewDragHelper.Callback() {                //这个方法为系统自动重写方法                    @Override                    public boolean tryCaptureView(View child, int pointerId) {                        //如果当前触摸的是child,那么开始检测                        return mMainView == child;                    }                });        }        //2. 重写onInterceptTouchEvent()方法,来拦截触摸事件        @Override        public boolean onInterceptTouchEvent(MotionEvent ev) {           return viewDragHelper.shouldInterceptTouchEvent(ev);        }        //3. 重写onTouchEvent()方法,来将触摸事件交给ViewDragHelper来处理        @Override        public boolean onTouchEvent(MotionEvent event) {        viewDragHelper.processTouchEvent(event);        return true;        }            //4. 处理滑动,重写computeScroll,这里医生提供了模板代码,直接抄的(*^__^*) 嘻嘻……,这个内容其实是为了onViewReleased(callback 的重写方法)做的准备,因为onViewReleased内部就使用了Scroller        @Override        public void computeScroll() {        super.computeScroll();        if(viewDragHelper.continueSettling(true)){            ViewCompat.postInvalidateOnAnimation(this);        }        }        //5. 处理callback方法        private void initViewDragHelper(){        viewDragHelper = ViewDragHelper.create(                this,                new ViewDragHelper.Callback() {                    @Override                    public boolean tryCaptureView(View child, int pointerId) {                        //如果当前触摸的是child,那么开始检测                        return mMainView == child;                    }                    //水平滑动和垂直滑动可以只选择一个进行重写,那么就只支持这一个方向的滑动                    //水平滑动                    @Override                    public int clampViewPositionHorizontal(View child, int left, int dx) {                        if(left < 0){                            return 0;                        }                        return left;                    }                    //垂直滑动                    @Override                    public int clampViewPositionVertical(View child, int top, int dy) {                        return 0;                    }                    //实现Scroller的效果,当拖动的距离小于某一个值的时候,让它返回原点,否则展开内容                    @Override                    public void onViewReleased(View releasedChild, float xvel, float yvel) {                        super.onViewReleased(releasedChild, xvel, yvel);                        //为了做一些滑动效果                         if(mMainView.getLeft() < 500){                            //关闭菜单                            viewDragHelper.smoothSlideViewTo(mMainView,0,0);                            ViewCompat.postInvalidateOnAnimation(ViewDragHelperViewGroup.this);                        }else{                            viewDragHelper.smoothSlideViewTo(mMainView,300,0);                            ViewCompat.postInvalidateOnAnimation(ViewDragHelperViewGroup.this);                        }                    }                });        }                //上面这些代码写完后,下面看XML代码,因为我们在Java代码中已经规定,第一个View为menu,第二个View为MainView,并且我们继承的ViewGroup是RelativeLayout,所以必须像下面这样写XML
 <com.example.young.test.ViewDragHelperViewGroup android:layout_width="match_parent"    android:layout_height="match_parent"    xmlns:android="http://schemas.android.com/apk/res/android" >    <!--menu视图-->    <TextView    android:background="@color/colorPrimary"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:text="子菜单"/>    <!--内容视图-->    <TextView    android:background="@color/colorAccent"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:text="主菜单"/></com.example.young.test.ViewDragHelperViewGroup>

这个ViewDragHelper实现的效果和上面的两个有所不同,上面的两个实现并不是覆盖的结果,而是直接两个页面的操作,如果你使用的是Android studio 那么在预览页面你也可以看到这个效果。
OK,今天的总结就这些

0 0
原创粉丝点击