Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lock page fragment in ViewPager android

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;
    }
}
like image 517
yuva ツ Avatar asked Feb 14 '23 21:02

yuva ツ


2 Answers

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?

like image 170
vipul mittal Avatar answered Feb 16 '23 11:02

vipul mittal


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.

like image 35
Boldijar Paul Avatar answered Feb 16 '23 09:02

Boldijar Paul