【发布时间】:2017-07-29 19:34:07
【问题描述】:
我的应用使用以下ViewPager:
ViewPager 与显示的宽度相同。
我在 xml 中为 ViewPager 添加了边距,以便我可以看到左(蓝色)和右(灰色)页面。 结果一切正常,但我只能触摸绿色区域来移动页面。
我想将触摸区域扩展到整个屏幕,以便我可以触摸任何地方来移动ViewPager。
我尝试添加TouchDelegate:
binding.pagerContainer.post {
val rect = Rect()
binding.touchArea.getHitRect(rect)
binding.pagerContainer.touchDelegate = TouchDelegate(rect, binding.viewPager)
}
在xml中:
<FrameLayout
android:id="@+id/pager_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:clipChildren="false">
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginBottom="150dp"
android:layout_marginLeft="70dp"
android:layout_marginRight="70dp"
android:layout_marginTop="150dp"
android:clipChildren="false"/>
<View
android:id="@+id/touchArea"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0">
</View>
</FrameLayout>
Rect 的大小正确。
我发布了Runnable 以确保视图布局。
我阅读了TouchDelegate 的文档。
我不知道为什么它不起作用。
【问题讨论】:
标签: android android-layout android-viewpager touch android-view