Disable TabLayout

前端 未结 11 1859
时光说笑
时光说笑 2020-11-29 02:49

I\'m using the new class provided by the design library : TabLayout. And I want in specific cases that the one I\'m using can\'t change tab anymore.

I manage to disa

11条回答
  •  一向
    一向 (楼主)
    2020-11-29 03:20

    In my use case, if the user is in a tab and presses one of the buttons, a calculation is launched and I don't want the user to be able to switch tabs until after the calculation is completed. So I used the answer by Vladislav Rishe but modified a little as follows...

    In JAVA:

    //'tabLayout' is the variable where you reference your tab layout...
    //A container to save the tabs that were disabled
    private ArrayList touchablesToRestore = new ArrayList();
    
    public void enableTabs() {
        for(View v: touchablesToRestore){
            v.setClickable(true);
        }
        //After you enable them all, clear the container
        touchablesToRestore.clear();
    }
    
    public void disableTabs(){
        for(View v: tabLayout.getTouchables()){
            //Add the tab that is being disabled to the container
            touchablesToRestore.add(v);
            v.setClickable(false);
        }
    }
    

    So when the user taps the button to make a calculation, just before the calculation logic is called, I call disableTabs(), then once the calculation is completed and it has been displayed, I call enableTabs()...

提交回复
热议问题