Disable swipe in fragmentPagerAdapter? - android

南楼画角 提交于 2020-08-24 06:38:34

问题


Hey guys i have a fragmentPagerAdapter with 3 fragments in it. How would i disable the swiping between the 3 fragments so the user only uses the tabview to go between fragments?

Cheers!

private class ViewPagerAdapter extends FragmentPagerAdapter
{
    ArrayList<Fragment> fragments = new ArrayList<>();
    ArrayList<String> tabTitles = new ArrayList<>();

    public ViewPagerAdapter(android.support.v4.app.FragmentManager fragmentManager)
    {
        super(fragmentManager);
    }
    @Override
    public Fragment getItem(int position)
    {
        return fragments.get(position);
    }
    @Override
    public int getCount()
    {
        return fragments.size();
    }
    @Override
    public CharSequence getPageTitle(int position)
    {
        return tabTitles.get(position);
    }
    public void addFragments(Fragment fragment, String titles)
    {
        this.fragments.add(fragment);
        this.tabTitles.add(titles);
    }
}

回答1:


You can setup a custom view pager like this :-

public class LockableViewPager extends ViewPager {
    private boolean swipeable;

    public LockableViewPager(Context context) {
        super(context);    
    }    

    public LockableViewPager(Context context, AttributeSet attrs) {    
        super(context, attrs);    
        this.swipeable = true;    
    }

    @Override    
    public boolean onTouchEvent(MotionEvent event) {    
        if (this.swipeable) {    
            return super.onTouchEvent(event);    
        }    
        return false;    
    }

    @Override

    public boolean onInterceptTouchEvent(MotionEvent event) {
        if (this.swipeable) {
            return super.onInterceptTouchEvent(event);
        }
        return false;
    }

    public void setSwipeable(boolean swipeable) {
        this.swipeable = swipeable;
    }
}

And then in the layout you can set it as :-

<mypackage.lockableviewpager 
    android:id="@+id/photosViewPager" 
    android:layout_height="match_parent"
    android:layout_width="match_parent"/>

And then just call

setSwipeable(false)

Hope it helps.




回答2:


Here's a little more elaboration on the LockableViewPager class example...

First instead of marking up a ViewPager like this

<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</android.support.v4.view.ViewPager>

you want to make a custom class that inherits VP like this

public class LockableViewPager extends ViewPager {
  private boolean swipeable;

  public LockableViewPager(Context context) {
      super(context);
  }

  public LockableViewPager(Context context, AttributeSet attrs) {
      super(context, attrs);
      this.swipeable = true;
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {
      if (this.swipeable) {
          return super.onTouchEvent(event);
      }
      return false;
  }

  @Override
  public boolean onInterceptTouchEvent(MotionEvent event) {
      if (this.swipeable) {
          return super.onInterceptTouchEvent(event);
      }
      return false;
  }

  public void setSwipeable(boolean swipeable) {
      this.swipeable = swipeable;
  }
}

Pay attention to this method

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
  if (this.swipeable) {
      return super.onInterceptTouchEvent(event);
  }
  return false;
}

then you can mark up your custom class that already acts as ViewPager plus intercepts touch events

<com.YourPackage.LockableViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</com.YourPackage.LockableViewPager>

In your activity's java class that contains LockableViewPager set

LockableViewPager viewPager;
OnCreate() {
  viewPager = (LockableVewPager) findViewById(R.id.*id of pager*);
  viewPager.setSwipable(false);
}

Extendng the class you want (i.e. ViewPager) is the CORRECT way to do this unless you are making a totally different class that doesn't behave the same way ;* good luck



来源:https://stackoverflow.com/questions/41656902/disable-swipe-in-fragmentpageradapter-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!