【发布时间】:2017-04-04 02:11:58
【问题描述】:
我创建了一个自定义查看器,我希望用户仅从屏幕的一部分滑动和更改页面。
每个页面的底部都有一个带有一些文本的线性布局,我只想从那里滑动,而不是从孔屏幕。
这可能吗?
这是我的自定义 viewpager 类:
public class MyViewPager extends ViewPager {
private boolean mSwipable = true;
public MyViewPager(Context context) {
super(context);
}
public MyViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
return mSwipable ? super.onInterceptTouchEvent(event) : false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return mSwipable ? super.onTouchEvent(event) : false;
}
public boolean isSwipable() {
return mSwipable;
}
public void setSwipable(boolean swipable) {
mSwipable = swipable;
}
}
这是一个页面的视图:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:clipChildren="false"
android:clipToPadding="false"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/fragment_search">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottomLayout"
android:layout_marginBottom="@dimen/bottom_bar_height">
//show some stuff here
</LinearLayout>
<LinearLayout
android:id="@+id/bottomMenu"
android:layout_width="match_parent"
android:layout_height="@dimen/bottom_bar_height"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:orientation="horizontal">
// this is my footer, where i need to change the pages by swipe
</LinearLayout>
</RelativeLayout>
【问题讨论】:
-
让你的viewpager与你的底部线性布局有相同的大小,所以你只能从那里滚动,然后在滑动viewpager时放置一个framelayout并更改片段。我认为您不能处理滑动事件以仅在 de pager 内的某些视图中起作用。