问题描述
我正在尝试做什么:我正在尝试在程序运行时以编程方式启用/禁用在寻呼机中滑动
What i am trying to do: I am trying to Enable/disable swiping in pager programatically when the program is running
例如: 在流程中,如果我检查条件并且如果它返回 true
启用刷卡,并且如果条件返回 false
禁用滑动.
Ex: When on the flow if i check for a condition and if it returns true
enable swiping, and if condition returns false
disable swiping.
public class CustomViewPager extends ViewPager {
private boolean enabled;
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
this.enabled = true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (this.enabled) {
return super.onTouchEvent(event);
}
return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (this.enabled) {
return super.onInterceptTouchEvent(event);
}
return false;
}
public void setPagingEnabled(boolean enabled) {
this.enabled = enabled;
} }
然后选择这个而不是XML中的内置viewpager
Then select this instead of the builtin viewpager in XML
<mypackage.CustomViewPager
android:id="@+id/myViewPager"
android:layout_height="match_parent"
android:layout_width="match_parent" />
您只需调用setPagingEnabled"方法,设置为false",用户将无法滑动分页.
You just need to call the "setPagingEnabled" method with "false" and users won't be able to swipe to paginate.
上述方法的问题: 我无法在流上设置属性,我:e .... 我可以启用刷卡或禁用刷卡.但我不能根据条件这样做
Problem with above methodology: i cannot set the property on the flow, I:e .... I either can enable swiping or disable swiping. But i cannot do this based on condition
- 我可以通过其他方式实现我的目标吗?如果可以,那是什么?
- 或者这不可能吗?
推荐答案
以编程方式禁用滑动-
final View touchView = findViewById(R.id.Pager);
touchView.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
return true;
}
});
并使用它来手动滑动
touchView.setCurrentItem(int index);
这篇关于如何在android中禁用或启用viewpager滑动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!