问题描述
我有一个 viewpager,它在向左/向右滑动时在选项卡之间切换.在我的第二个选项卡中,我有一些自定义视图,它们具有用于捏合和拖动的侦听器,但是当我尝试捏合或拖动时,viewpager 开始滑动页面.
I have a viewpager which switches between tabs when swiping left/right.In my second tab, i have some custom views which have listeners for pinching and dragging but when i try to pinch or drag, the viewpager starts to swipe the page.
我想到的一个解决方案是在触摸这些特定视图时禁用滑动,仅在触摸这些视图之外时才滑动.这可能吗?
A solution comes to my mind is to disable swiping when touching those specific views and only swipe when touching outside those views.Is this possible?
更新:@Asok 好心地提供了解决方案.但后来更新了在我的情况下不起作用的代码,所以我发布了上一段对我有用的代码:
Updated: @Asok kindly provided the solution. But then updated the code which wouldnt work in my case so i post the previous piece of code which worked for me:
public class CustomViewPager extends ViewPager {
private boolean swipeable = true;
public CustomViewPager(Context context) {
super(context);
}
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
// Call this method in your motion events when you want to disable or enable
// It should work as desired.
public void setSwipeable(boolean swipeable) {
this.swipeable = swipeable;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent arg0) {
return (this.swipeable) ? super.onInterceptTouchEvent(arg0) : false;
}
假设我有一个可拖动的视图,我需要在拖动开始时禁用滑动,并在拖动完成时重新启用,所以在我所谓的视图的 TouchEvent 中:
Lets suppose i have a draggable view and i need to disable swipping when dragging start and re enable when dragging finished so in TouchEvent of my so called view:
@Override
public boolean onTouchEvent(MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
//disable swiping when the button is touched
((ActivityOriginal) getActivity()).setSwipeable(false);
//the rest of the code...
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
//re enable swipping when the touch is stopped
//the rest of the code...
((ActivityOriginal) getActivity()).setSwipeable(true);
break;
}
return true;
}
推荐答案
我首先想到的是有一个自定义的 ViewPager
,当你的触摸监听器收到通知时特定事件,您可以将 ViewPager
中的 swipeable
boolean
设置为 false,然后以最适合您的应用程序的方式将其设置回 true.
This first thing that comes to mind for me is to have a custom ViewPager
in which, when your touch listeners get notified of a specific event you could set the swipeable
boolean
in ViewPager
to false and set it back to true whichever way best fits your application.
public class CustomViewPager extends ViewPager {
private boolean swipeable = true;
public CustomViewPager(Context context) {
super(context);
}
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
// Call this method in your motion events when you want to disable or enable
// It should work as desired.
public void setSwipeable(boolean swipeable) {
this.swipeable = swipeable;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent arg0) {
return (this.swipeable) ? super.onInterceptTouchEvent(arg0) : false;
}
}
<小时>
确保更改布局文件以显示:
Make sure to change your layout file to show:
<com.your.package.CustomViewPager .. />
代替:
<android.support.v4.view.ViewPager .. />
<小时>
编辑 2
这是我的设置(使用上面的 CustomViewPager
):
Here is my setup (Working with the above CustomViewPager
):
CustomViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the CustomViewPager with the sections adapter.
mViewPager = (CustomViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
public void swipeOn(View v) {
mViewPager.setSwipeable(true);
}
public void swipeOff(View v) {
mViewPager.setSwipeable(false);
}
上面显示的 onCreate
在我的 MainActivity
类中,它扩展了 FragmentActivity
并实现了 ActionBar.TabListener
The above shown onCreate
is in my MainActivity
class which extends FragmentActivity
and implements ActionBar.TabListener
这篇关于如何在触摸特定视图时禁用 viewpager 适配器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!