4

How can I lock paging in fragment activity of viewpager for fragment swipe.I'm performing some operations using progressbar in one fragment.While progressing progressbar fragment gets changes because of swipe action.so while progessing progressbar I want to stop swiping.How to do this?is there any solution??

activity.xml file-

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"   
    tools:context=".MainFragmentActivity" >

    <android.support.v4.view.PagerTitleStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="@drawable/title"
        android:paddingBottom="4dp"
        android:paddingTop="4dp"
        android:textColor="#fff" />

</android.support.v4.view.ViewPager>

fragment activity-

public class MainFragmentActivity extends FragmentActivity {

    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;
    private Context mContext;
    private PagerTitleStrip pt;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity);

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(),mContext);
        mContext=this;
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        pt=(PagerTitleStrip) findViewById(R.id.pager_title_strip);


    }

SectionsPagerAdapter-

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    Context mContext;
    Fragment fragment;

    public SectionsPagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        mContext=context;
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a DummySectionFragment (defined as a static inner class
        // below) with the page number as its lone argument.
        if(position==0){
            fragment = new SelectItem();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, position + 1);
            fragment.setArguments(args);
        }
        if(position==1){
            fragment = new culation();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, position + 1);
            fragment.setArguments(args);
        }
        if(position==2){
                        fragment = new GraphDisplay();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, position + 1);
            fragment.setArguments(args);
        }
        return fragment;
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
        case 0 :
            return "Select item";
        case 1:
            return "Calculation";
        case 2:
            return "Graph";
        }
        return null;
    }
}
yuva ツ
  • 3,707
  • 9
  • 50
  • 78

4 Answers4

4

Try setting clickable to false:

mViewPager.setClickable(false);

If that works what you can do is create a function in activity to disable swipe event on view pager and call that function in fragment in event of progress bar show and a function to enable it.

Subclass ViewPager and override onTouch event for example:

EDIT

public class NonSwipeableViewPager extends ViewPager {
    private boolean lock;
    public void lock(){
        lock=true;
    }    
    public void unlock(){
        lock=false;
    }
    public NonSwipeableViewPager(Context context) {
        super(context);
    }

    public NonSwipeableViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

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

reference: How do disable paging by swiping with finger in ViewPager but still be able to swipe programmatically?

Community
  • 1
  • 1
vipul mittal
  • 17,343
  • 3
  • 41
  • 44
4

I found a simple way to do this, with its adapter.

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

/**
 * Created by paul.boldijar on 2/24/2015.
 */
public class SimpleFragmentAdapter extends FragmentPagerAdapter {

    private Fragment[] fragments;

    private boolean locked = false;
    private int lockedIndex;


    public void setLocked(boolean locked, int page) {
        this.locked = locked;
        lockedIndex = page;
        notifyDataSetChanged();
    }

    public SimpleFragmentAdapter(FragmentManager fm, Fragment[] fragments) {
        super(fm);
        this.fragments = fragments;
    }

    @Override
    public Fragment getItem(int position) {
        if (locked) return fragments[lockedIndex];
        return fragments[position];

    }
    @Override
    public int getCount() {
        if (locked) return 1;
        return fragments.length;
    }
}

If you want to lock your fragment to a page, just call fragment.setLocked(true,page); Where page is the index of the page you want to lock.

Boldijar Paul
  • 5,405
  • 9
  • 46
  • 94
  • this should be the most upvoted answer, instead of making a custom viewpager , this is so much more elegant and simple... – PirateApp Dec 10 '15 at 03:01
  • 1
    this changes the count and, as I see it, it would yield a very bad user experience when combined with a page indicator. otherwise this is very nice – leonardkraemer Mar 13 '18 at 23:52
1

Based on the solution by Paul, I modified it to support displaying titles as well

public static class MainPagerAdapter extends FragmentStatePagerAdapter {
    private Fragment[] fragments;
    private String[] titles;
    private boolean locked = false;
    private int lockedIndex;

    public MainPagerAdapter(FragmentManager fm, Fragment[] fragments, String[] titles) {
        super(fm);
        this.fragments = fragments;
        this.titles = titles;
    }

    public void setLocked(boolean locked, int page) {
        this.locked = locked;
        lockedIndex = page;
        notifyDataSetChanged();
    }

    @Override
    public Fragment getItem(int position) {
        if (locked) return fragments[lockedIndex];
        return fragments[position];
    }

    @Override
    public int getCount() {
        if (locked) return 1;
        return fragments.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        if (locked) return titles[lockedIndex];
        return titles[position];
    }
}

The way I use it is to say

Fragment[] fragments = {//Create fragment objects here...};
    String[] titles = getResources().getStringArray(R.array.tabs);
    MainPagerAdapter adapter = new MainPagerAdapter(getSupportFragmentManager(), fragments, titles);
    adapter.setLocked(true, 0);
    mPager.setAdapter(adapter);
PirateApp
  • 5,433
  • 4
  • 57
  • 90
0

You can use this View:
https://github.com/mr-sarsarabi/lockableViewPager

It lets you lock the swipe gesture with a method: viewPager.setSwipeLocked(true);

Til
  • 5,150
  • 13
  • 26
  • 34
  • It's better to add an example and explanation, that will benefit OP and future readers. – Til Mar 08 '19 at 07:33