6

I'm creating a android application with target on 2.2. I still want to use the ViewPager provided in the android.support-v4.jar.i want need to disable Viewpager swipping on button click.i have use following code for disable. but its don't work.

public class ServiesDeail extends FragmentActivity{

    ServiesTitleFragmentAdapter mAdapter;
    ViewPager mPager;
        int position = 0 ;

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mastermenu);

        LinearLayout lnrparent = (LinearLayout) findViewById(R.id.LL_001);
        LayoutInflater inflater = (LayoutInflater)                                                         this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.menu_deatil, null);
        lnrparent.addView(view);

        mAdapter = new ServiesTitleFragmentAdapter(getSupportFragmentManager());

        mPager = (ViewPager) findViewById(R.id.pagerone);
        mPager.setAdapter(mAdapter);


        TitlePageIndicator indicator = (TitlePageIndicator) findViewById(R.id.indicatorone);
        indicator.setViewPager(mPager,position);

            Menu_Button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                mPager.setClickable(false);
            }
        });
      }
}

if u have any idea anyone.. please give me reponse.. i m waiting your response

Akash Singh
  • 5,171
  • 2
  • 26
  • 55

1 Answers1

24

You will have to extend the view pager and create a custom view pager for you which can be used in the xml instead of the view pager that you get from support library and change some properties, see the code below, it worked for me. Use the customviewpager in your code, and the swiping will be disabled.

public class CustomViewPager extends ViewPager{

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

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

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev){
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev){
        return false;
    }
}
Yashwanth Kumar
  • 28,931
  • 15
  • 65
  • 69
  • thanks for response ..that are not possible i have use fragment view there has multiple layout use.. so i have use this method then not able to use multiple task on fragment @yashwanth – Akash Singh Jul 16 '12 at 05:28
  • i couldn't understand your comment, declare a variable in the class to check if swiping has to be disabled, and control that from the activity where you are using viewPager. – Yashwanth Kumar Jul 16 '12 at 05:39
  • i have already extends FragmentActivity in my class .. then how to possible to extends another class Viewpager .. that are problem ? @yashwanth kumar – Akash Singh Jul 16 '12 at 05:43
  • I see that you are declaring viewPager in the xml, so instead of using viewPager use the custom class i have mentioned in the xml, like instead of , i did not ask to extend viewPager for your activity. i will change my answer a bit. – Yashwanth Kumar Jul 16 '12 at 05:46