解决SlidingPaneLayout的滑动冲突

最近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.
 */
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值