Lock page fragment in android viewpager

How to block paging in working with fragments of a viewpager for a scipe fragment. I am doing some operations using progressbar in one fragment. If the progress of the progress fragment gets a change due to the scrolling action.so while progressing the progressbar, I want to stop scrolling. How to do it? 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;
    }
}
+3
source share
3 answers

Try setting clickable to false:

mViewPager.setClickable(false);

, , swipe .

ViewPager onTouch, :

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);
        }
    }
}

: , ViewPager, ?

+2

.

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;
    }
}

, .setLocked(true, page); - , .

+2

Paul, , .

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];
    }
}

I use it by saying

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);
+1
source

All Articles