问题描述
我有一个viewpager刷卡左/ right.In我的第二个选项卡时,卡之间切换,我有有监听捏和拖动,但是当我试着捏或拖累,viewpager开始刷页面的一些自定义视图
一个解决方案,在我脑海是禁用当那些views.Is外界接触接触这些具体的意见,只有刷卡时,刷卡可以吗?
更新:@Asok好心提供的解决方案。但随后更新了code这wouldnt在我的情况下工作,所以我张贴previous一张code的工作对我来说:
公共类CustomViewPager扩展ViewPager {
私人布尔swipeable = TRUE;
公共CustomViewPager(上下文的背景下){
超(上下文);
}
公共CustomViewPager(上下文的背景下,ATTRS的AttributeSet){
超(背景下,ATTRS);
}
//你的移动事件调用此方法时要禁用或启用
//它应该根据需要。
公共无效setSwipeable(布尔swipeable){
this.swipeable = swipeable;
}
@覆盖
公共布尔onInterceptTouchEvent(MotionEvent为arg0){
返程(this.swipeable)? super.onInterceptTouchEvent(arg0中):假的;
}
让我们假设我有一个可拖动的观点,我需要禁用swipping拖动时开始,并重新在拖放完成如此的TouchEvent使我所谓的观点:
@覆盖
公共布尔的onTouchEvent(MotionEvent事件){
开关(event.getAction()){
案例MotionEvent.ACTION_DOWN:
//当触摸按钮禁用刷卡
((ActivityOriginal)getActivity())setSwipeable(假)。
//的code休息...
打破;
案例MotionEvent.ACTION_MOVE:
打破;
案例MotionEvent.ACTION_UP:
//重新启用swipping当触摸停止
//的code休息...
((ActivityOriginal)getActivity())setSwipeable(真)。
打破;
}
返回true;
}
想到的,我是有一个自定义的 ViewPager
这第一件事,当你触摸听众得到通知的特定事件,你可以设置 swipeable
布尔
在 ViewPager 对code>为false,并设置回真哪个方式最适合你的应用程序。
公共类CustomViewPager扩展ViewPager {
私人布尔swipeable = TRUE;
公共CustomViewPager(上下文的背景下){
超(上下文);
}
公共CustomViewPager(上下文的背景下,ATTRS的AttributeSet){
超(背景下,ATTRS);
}
//你的移动事件调用此方法时要禁用或启用
//它应该根据需要。
公共无效setSwipeable(布尔swipeable){
this.swipeable = swipeable;
}
@覆盖
公共布尔onInterceptTouchEvent(MotionEvent为arg0){
返程(this.swipeable)? super.onInterceptTouchEvent(arg0中):假的;
}
}
请一定要改变你的布局文件显示:
< com.your.package.CustomViewPager .. />
相反的:
< android.support.v4.view.ViewPager .. />
编辑2
下面是我的设置(与工作上面 CustomViewPager
):
CustomViewPager mViewPager;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
//设置操作栏。
最后的动作条动作条= getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//创建将返回一个片段为三个的适配器
//应用程序的主要部分。
mSectionsPagerAdapter =新SectionsPagerAdapter(getSupportFragmentManager());
//设置的CustomViewPager与部分适配器。
mViewPager =(CustomViewPager)findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(新ViewPager.SimpleOnPageChangeListener(){
@覆盖
公共无效onPageSelected(INT位置){
actionBar.setSelectedNavigationItem(位置);
}
});
//对于每个应用程序的章节中,添加一个标签,操作栏。
的for(int i = 0; I< mSectionsPagerAdapter.getCount();我++){
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(I))
.setTabListener(本));
}
}
公共无效swipeOn(视图v){
mViewPager.setSwipeable(真正的);
}
公共无效swipeOff(视图v){
mViewPager.setSwipeable(假);
}
上面显示的onCreate
在我的 MainActivity
类,它扩展了 FragmentActivity
和农具 ActionBar.TabListener
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?
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;
}
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;
}
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 .. />
Instead of:
<android.support.v4.view.ViewPager .. />
Edit 2
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);
}
The above shown onCreate
is in my MainActivity
class which extends FragmentActivity
and implements ActionBar.TabListener
这篇关于如何禁用viewpager适配器上摸具体看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!