Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

How to disable swipes in a view pager in android

I am new to android. I am creating an app which consists of a view pager with a swipe tab layout. When I click the button it does some calculations inside of the view pager. While it's doing this I want the switching between the tabs to be disabled. So no swiping between the pages while it's calculating. After the calculations are completed I want the switching to be enabled again. Can anyone tell me how to do this please? Thanks in advance. I tried this. Please have a look at it:

    public class Auto extends FragmentActivity{
    Button tests;
    private ViewPager pager;
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.auto);
                tests = (PaperButton)findViewById(R.id.action_button_tests);
                tests.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if(pager.getCurrentItem()==0){
                            tests.setTag(1);
                            tests.setText("START TEST");
                        final int status = (Integer)v.getTag();
                        if(status==1){
                            tests.setText("STOP TEST");
                            v.setTag(0);
                            int i;
                            Login.Communication_Ok=false;
                            for( i=0;(i<5 && Login.Communication_Ok!=true);i++)
                            Login.Send_Commands_To_Micro_Controller(1);
                            if(Login.Communication_Ok==true)
                            {
                                Video_Status=false;
                                Login.Bucket_Status = false;
                                Login.Auto_Mode_Bfr_Fuse =false;
                                for( i=0;(i<5 && Login.Auto_Mode_Bfr_Fuse!=true);i++)
                                Login.Send_Commands_To_Micro_Controller(3);
                                if(Login.Auto_Mode_Bfr_Fuse==true)
                                Toast.makeText(Auto.this, "Test started", Toast.LENGTH_LONG).show();
                                else
                                {
                                Toast.makeText(Auto.this, "Communication Failure in Before Fuse connection Segment", Toast.LENGTH_LONG).show();
                                tests.setText("START TEST");
                                v.setTag(1);
                                return;
                                }
                                Test_Completed=false;
                                if(!Auto_Bucket_Tests_Thread.isAlive())
                                Auto_Bucket_Tests_Thread.start();


                            }

                        }
                        else{
                            tests.setText("START TEST");

                            v.setTag(1);
                        }
                        }


                        if(pager.getCurrentItem()==1){
                            tests.setTag(1);
                            tests.setText("START TEST");

                            final int status = (Integer)v.getTag();
                            if(status==1){
                                tests.setText("STOP TEST");
                                v.setTag(0);
                                int i;
                                Login.Communication_Ok=false;
                                for( i=0;(i<5 && Login.Communication_Ok!=true);i++)
                                Login.Send_Commands_To_Micro_Controller(1);
                                if(Login.Communication_Ok==true)
                                {
                                    Video_Status=false;
                                    Login.Bucket_Status = false;
                                    Login.Automode_After_connecting_fuse =false;
                                    for( i=0;(i<5 && Login.Automode_After_connecting_fuse!=true);i++)
                                    Login.Send_Commands_To_Micro_Controller(4);
                                    if(Login.Automode_After_connecting_fuse==true){
                                    Toast.makeText(Auto.this, "Test started", Toast.LENGTH_LONG).show();



                                    **/*For Disabling swipe between tabs*/**
                                    pager.setOnTouchListener(new OnTouchListener() {

                                        @Override
                                        public boolean onTouch(View v, MotionEvent event) {
                                            pager.getCurrentItem();
                                            return false;
                                        }
                                    });
                                    pager.setEnabled(false);

                                    }
                                    else
                                    {
                                    Toast.makeText(Auto.this, "Communication Failure in After Fuse connection Segment", Toast.LENGTH_LONG).show();
                                    tests.setText("START TEST");
                                    v.setTag(1);
                                    return;
                                    }
                                    Test_Completed=false;

                                }


                            }
                            else{
                                tests.setText("START TEST");

                                v.setTag(1);
                                }

                    }
}   }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
public class CustomViewPager extends ViewPager {

private boolean enabled;

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

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

return false;
}

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

return false;
}

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

You just need to call the "setPagingEnabled" method with "false" and users won't be able to swipe to paginate.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...