简体   繁体   English

启用/禁用Android ActionBar.Tab

[英]Enable/Disable Android ActionBar.Tab

I'm developing an application that uses the new Tabs system (ActionBar.addTab()), I will have 3 tabs. 我正在开发使用新的Tabs系统(ActionBar.addTab())的应用程序,我将有3个选项卡。 During a determined process I would like to lock (disable) two of them (not remove, only do not accept clicks and not change the 'selector'. 在确定的过程中,我想锁定(禁用)它们中的两个(不删除,仅不接受点击并且不更改“选择器”。

I was able to do not change the content (by implementing the TabListener and not changing the fragment, etc) but the selector changes. 我无法更改内容(通过实现TabListener而不更改片段等),但是选择器发生了变化。

Is there anyway to only disable/enable the tabs? 无论如何,仅禁用/启用选项卡? without have to remove and add again? 无需删除并再次添加?

Thanks in advance! 提前致谢! BR, Danilo BR,达尼洛

Sorry, there is no "enable" or "disable" with tabs in the action bar. 抱歉,操作栏中的选项卡没有“启用”或“禁用”功能。 As you note, you can remove and re-add them -- that is the closest you can get AFAIK. 如您所见,您可以删除并重新添加它们-这是您可以获得AFAIK的最接近的。

I got into the exact same issue. 我遇到了完全相同的问题。 Finally was able to solve it. 终于能够解决它。 As said already, there is no way to disable/enable a tab. 如前所述,无法禁用/启用选项卡。 There is only one way out - and that is to go back to the previous tab using 只有一种方法-使用以下方法返回上一个选项卡

getActionBar().setSelectedNavigationItem(tab);

Note however, that this should NOT be done from within the 但是请注意,这应该被从内完成

onTabSelected(ActionBar.Tab tab,FragmentTransaction fragmentTransaction)

method, because then it gets stuck in recursive loop. 方法,因为这样会陷入递归循环。 So within the onTabSelected() - send a messsage to the Activity Handler to do the setSelectedNavigation(). 因此,在onTabSelected()中-将消息发送到活动处理程序以执行setSelectedNavigation()。

One catch here is (you will know when you implement): what "tab" value to pass in the setSelectedNavigation. 这里有一个问题(您将在实现时知道):在setSelectedNavigation中传递什么“ tab”值。 For this, we have to keep track of whether it is a user pressed tab switch or forced switch to the last tab via the code. 为此,我们必须跟踪是否是用户按下的选项卡开关或通过代码强制切换到最后一个选项卡。

For this: 为了这:

public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

    if (forcedTabSwitch)
        forcedTabSwitch = false;
    else 
        mTabToBeSwitchedTo = tab.getPosition();
}

and in the Handler, 在处理程序中

switch (msg.what) {
            case SWITCH_TO_TAB :
                forcedTabSwitch = true;
                            break;
}

I also faced same problem.My application has three tabs and each tab has buttons to move onto next and previous tab. 我也遇到了同样的问题。我的应用程序有三个选项卡,每个选项卡都有按钮可移至下一个和上一个选项卡。 I don't find any method for enabling or disabling tab. 我找不到用于启用或禁用标签页的任何方法。

I used DataSetter class to hold previously selected tab position and boolean value to determine tab selection. 我使用DataSetter类来保存先前选择的选项卡位置和布尔值以确定选项卡的选择。 If boolean flag is true then user has pressed next/previous button. 如果布尔标志为true,则用户按下了下一个/上一个按钮。 Otherwise user is trying to select tab by pressing them which we don't allow. 否则,用户将尝试按我们不允许的选项卡来选择选项卡。
I used handler for tab selection.Inside fragment when user clicks next button I broadcast that request. 我使用处理程序来选择选项卡。当用户单击下一步按钮时,在片段内部广播了该请求。 Broadcast request has integer parameter which is position of next or previous tab that we want to set. 广播请求具有整数参数,该整数参数是我们要设置的下一个或上一个选项卡的位置。 Code inside fragment is: 片段内的代码是:

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 
    {
      View rootView = inflater.inflate(R.layout.first_tab_fragment, container, false); 
      next = (Button)rootView.findViewById(R.id.nextbutton);
      next.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent intent = new Intent("TAB_CLICKED");
            Bundle bundle = new Bundle();
            bundle.putInt(MainActivity.POSITION,1);
            intent.putExtras(bundle);
            getActivity().sendBroadcast(intent);
        }
     });
    return rootView;
    }

I have used CustomViewPager instead of ViewPager to prevent tab swiping. 我使用CustomViewPager而不是ViewPager来防止选项卡滑动。 CustomViewPager class is : CustomViewPager类为:

            public class CustomViewPager extends ViewPager {            

            private boolean enabled;            
            public CustomViewPager(Context context) {
                super(context);
            }

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

            @Override
            public boolean onTouchEvent(MotionEvent event) {
                // TODO Auto-generated method stub
                if (this.enabled) {
                    return super.onTouchEvent(event);
                }

                return false;
            }

            @Override
            public boolean onInterceptTouchEvent(MotionEvent event) {
                // TODO Auto-generated method stub
                 if (this.enabled) {
                        return super.onInterceptTouchEvent(event);
                    }

                    return false;
            }

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

Inside oncreate method of MainActivity.java add this two lines: 在MainActivity.java的oncreate方法内,添加以下两行:

     viewPager = (CustomViewPager) findViewById(R.id.pager);
     viewPager.setPagingEnabled(false);

Below is remaining code from MainActivity.java: 以下是MainActivity.java的其余代码:

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

    Log.d("MainActivity","onTabSelected");  
    if(setter.getFlag() == true)
    {
        viewPager.setCurrentItem(setter.getposition());
    }           
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub
    Log.d("MainActivity","onTabUnselected");    
    //handler.sendEmptyMessage(0);
    Message message = new Message();
    message.arg1 = setter.getposition();
    handler.sendMessage(message);       
}

Handler handler = new Handler()
{
    public void handleMessage(android.os.Message msg)
    {
        int arg1 = msg.arg1;            
        Log.d("arg1",arg1+"");
        //viewPager.setCurrentItem(arg1);
        mActionBar.setSelectedNavigationItem(arg1);
        Log.d("handler","Inside handler");
    }
};

BroadcastReceiver receiver =  new BroadcastReceiver(){

    public void onReceive(Context context, android.content.Intent intent) {

        String action = intent.getAction();
        Bundle bundle = intent.getExtras();
        int position = bundle.getInt(MainActivity.POSITION);
        Log.d("Action",action);
        Log.d("position",position+"");
        setter.setposition(position);
        setter.setflag(true);
        Message message = new Message();
        message.arg1 = position;
        handler.sendMessage(message);
    };
   };
 }

Finally DataSetter.java : 最后是DataSetter.java:

    public class DataSetter 
    {
        int position;
        boolean flag;   
        void setposition(int value)
        {
            position = value;
        }   
        void setflag(boolean value)
        {
            flag = value;
        }   
        int getposition()
        {
            return position;
        }   
        boolean getFlag()
        {
            return flag;
        }
    }

More you can find on: http://suhas1989.wordpress.com/2014/10/13/enable-or-disable-actionbar-tab-in-android/ 您可以在以下网址找到更多信息: http : //suhas1989.wordpress.com/2014/10/13/enable-or-disable-actionbar-tab-in-android/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM