最近2天在写自己的小项目,系图书馆管理系统。打开APP后的界面的布局是左边一个侧拉菜单,右边主界面的内容为一个ViewPager,可以经行滑动和点击选项卡切换。
最后的截图如图:
so,xml的布局代码大致如下:
activity_main.xml
<views.PagerEnableSlidingPaneLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/main_menu"/>
<include layout="@layout/content_main"/>
</views.PagerEnableSlidingPaneLayout>
这里的PagerEnableSlidingPaneLayout就是重写过的SlidingPaneLayout,因为如果直接使用SlidingPaneLayout的话,右侧区域的主视图里面的ViewPager会和它发生滑动冲突。手指从右往左滑动没有问题,但是手指从左向右滑动的时候,会将侧拉菜单拉出,ViewPager的切页效果消失。所以,为了消除滑动冲突,我们需要重写SlindingPaneLayout这个类。
由于自己的实力有限,所以参考了Stack Overflow上面的一个相同问题的回答。他对SlindingPaneLayout的重写如下:
PagerEnabledSlidingPaneLayout.java
import android.content.Context;
import android.support.v4.view.MotionEventCompat;
import android.support.v4.widget.SlidingPaneLayout;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
/**
* SlidingPaneLayout that, if closed, checks if children can scroll before it intercepts
* touch events. This allows it to contain horizontally scrollable children without
* intercepting all of their touches.
*
* To handle cases where the user is scrolled very far to the right, but should still be
* able to open the pane without the need to scroll all the way back to the start, this
* view also adds edge touch detection, so it will intercept edge swipes to open the pane.
*/