我有一个 viewpager,它在向左/向右滑动时在选项卡之间切换。在我的第二个选项卡中,我有一些自定义 View ,它们具有用于捏合和拖动的监听器,但是当我尝试捏合或拖动时,viewpager 开始滑动页面.
我想到的一个解决方案是在触摸这些特定 View 时禁用滑动,仅在触摸这些 View 之外时才滑动。这可能吗?
更新: @Asok 好心地提供了解决方案。但后来更新了在我的情况下不起作用的代码,所以我发布了上一段对我有用的代码:
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;
}
假设我有一个可拖动的 View ,我需要在拖动开始时禁用滑动,并在拖动完成时重新启用,所以在我所谓的 View 的 TouchEvent 中:
@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
,当你的触摸监听器收到特定事件的通知时,你可以设置 swipeable
ViewPager
中的 boolean
为 false,然后以最适合您的应用程序的方式将其设置回 true。
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;
}
}
确保更改布局文件以显示:
<com.your.package.CustomViewPager .. />
代替:
<android.support.v4.view.ViewPager .. />
编辑 2
这是我的设置(使用上面的 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
关于android - 如何在触摸特定 View 时禁用 viewpager 适配器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16342630/