One side ViewPager swiping only(一侧 ViewPager 仅滑动)
问题描述
我的应用程序中有一个 ViewPager,我想随时禁用/允许向右滑动.viewpager 中的每个视图都包含 ListView.我怎样才能做到这一点?我正在尝试以这种方式做到这一点:
I have a ViewPager in my application and I would like to disable/allow swipe to right side in any time. Every view in the viewpager contains ListView. How can I do that? I am trying to do that in this way:
private int oldX;
private int deltaX = 0;
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_MOVE || motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
int newX = (int) motionEvent.getX();
deltaX = oldX - newX;
oldX = newX;
}
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
oldX = 0;
}
return deltaX < 0;
}
但是视图仍然会向右一点.有人在解决同样的问题吗?
But view is still going to right side a little bit. Anyone was solving the same issue?
推荐答案
通过将 ViewPager 的代码放入我的应用程序并添加以下内容,我能够在 ViewPager 中实现单侧滚动
I was able to achieve one-side scrolling in ViewPager by taking its code to my application and adding the following
private boolean performDrag(float x) {
boolean needsInvalidate = false;
final float deltaX = mLastMotionX - x;
mLastMotionX = x;
// MY CODE STARTS
if (deltaX < 0) {
return false;
}
// MY CODE ENDS
float oldScrollX = getScrollX();
...
它似乎工作正常.您可能需要的唯一东西 - 获取适配器代码或提供您自己的适配器,因为 ViewPager
在其代码中使用的某些方法不是从 PagerAdapter
公开的.
It seems to work fine. The only thing You might need - take adapter code or provide Your own adapter, because some methods ViewPager
uses in its code are not public from PagerAdapter
.
这篇关于一侧 ViewPager 仅滑动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:一侧 ViewPager 仅滑动
基础教程推荐
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01