0

I am unable to understand properly what these functions are used for in my FragmentActivity class.

    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int pos) {
            // on changing the page
            // make respected tab selected
            position = pos;
            actionBar.setSelectedNavigationItem(pos);

            new Getquestions().execute();
        }



        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {

}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // on tab selected
    // show respected fragment view
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {

}

I am using a view pager for page sliding, and what I want to do is, if a condition is false, for example I have three edit texts in fragment one, and if a user wants to slide to fragment two without filling up the texts then the must not slide.

I tried to find any such example but I am unable to find it. Can anyone help? Understanding these functions will help me implement my thing I guess

EdmDroid
  • 1,350
  • 1
  • 11
  • 25
Hassaan Rabbani
  • 2,469
  • 5
  • 30
  • 55

2 Answers2

0

Documented advantage of fragments, is that they can be added or removed to the activity on a run-time.

What you want is have a single fragment initially, and once the requirements are fulfilled - add a next fragment to your activity.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
alandarev
  • 8,349
  • 2
  • 34
  • 43
  • no actually i wish to have multiple fragments initially, but i want to restrict the user to initial fragment unless he completes all the fields. – Hassaan Rabbani Apr 01 '14 at 08:05
  • @HassaanRabbani So you want a fragment to be there, but scrolling to it disabled? I afraid in that case you have to implement your own animation classes. That is way too inefficient – alandarev Apr 01 '14 at 08:23
0

I have got an answer from another forum

public class CustomViewPager extends ViewPager {

private boolean isPagingEnabled;

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

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

    return false;
}

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

    return false;
}

public void setPagingEnabled(boolean b) {
    this.isPagingEnabled = b;
}

}

Now when you want to disable swiping just setPagingEnable = false;

and in the layout file replace any <com.android.support.V4.ViewPager> tags with <com.yourpackage.CustomViewPager> tags.

Hassaan Rabbani
  • 2,469
  • 5
  • 30
  • 55