启用/禁用操作栏中的选项卡

发布于 2024-12-16 00:38:24 字数 384 浏览 1 评论 0原文

可以在 ActionBar 中启用/禁用选项卡吗? 使用 TabHost 这不是问题.. 我这样做:

tabHost.getTabWidget().getChildAt(3).setVisibility(true);

并且一切正常.. 但如果我想对 ActionBar 中的选项卡做同样的事情? Tab 类中不存在 setEnable();

ActionBar bar = getActionBar();
Tab tab =  bar.newTab();
tab.setText("Test");
tab.setEnable(false); /*DON'T EXIST!!*/

我该怎么办?

It's possible to enable/disable Tabs in ActionBar?
With TabHost this is not a problem.. I do:

tabHost.getTabWidget().getChildAt(3).setVisibility(true);

and all works.. but if i want to do the same thing with Tabs in ActionBar??
In Tab class don't exist setEnable();

ActionBar bar = getActionBar();
Tab tab =  bar.newTab();
tab.setText("Test");
tab.setEnable(false); /*DON'T EXIST!!*/

How can I do??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

樱花落人离去 2024-12-23 00:38:24

您可以使用 removeTab( ActionBar.Tab tab ) - ActionBar的方法:

bar.removeTab( tab );

然后使用addTab( ActionBar.Tab tab, int position ) 将其放回原处,前提是您保存了删除的 Tab 的位置:

bar.addTab( tab, savedTabPosition );

You could use the removeTab( ActionBar.Tab tab )-method of ActionBar:

bar.removeTab( tab );

And then use the addTab( ActionBar.Tab tab, int position ) to put it back in, provided that you save the position of the Tab you removed:

bar.addTab( tab, savedTabPosition );
弥繁 2024-12-23 00:38:24

我还没有测试过这个——这将取决于你,但它应该让你对如何处理你的问题有一个总体的了解。

共有三个步骤:

第一步

我们需要一些可以为我们处理启用/禁用操作的东西。为此,我们创建了以下类:

public class TabItem {
  private Tab tab;
  private Fragment fragment;
  private boolean enabled;

  public TabItem( Tab tab, Fragment fragment ) {
    this.tab = tab;
    this.fragment = fragment;
    enabled = true;
  }

  public Tab getTab() {
    return tab;
  }

  public Fragment getFragment() {
    return fragment;
  }

  public void toggleEnabled() {
    enabled = enabled ? false : true;
  }

  public boolean isEnabled() {
    return enabled;
  }
} 

第二步

我们需要一些可以保存这些TabItems的东西以及一种访问它们的简单方法。为此,我们添加以下类:

public class TabHolder {
  private HashMap<Integer, TabItem> tabs;

  public TabHolder() {
    tabs = new HashMap<Integer, TabItem>();
  }

  public void addTab( TabItem tab ) {
    tabs.put( tab.getTab().getPosition(), tab );
  }

  public TabItem getTab( int position ) {
    return tabs.get( position );
  }
}

第三步

我们需要自己处理Tabs的选择,因此我们需要创建一个自定义的TabListener

private class MyTabListener implements TabListener {
  @Override
  public void onTabReselected( Tab tab, FragmentTransaction ft ) {
    //Do nothing - unless you want to do something.
  }

  @Override
  public void onTabSelected( Tab tab, FragmentTransaction ft ) {
    TabItem item = tabHolder.getTab( tab.getPosition() );
    if( item.isEnabled() ) {
      ft.remove( item.getFragment() );
      ft.commit();
    }        
  }

  @Override
  public void onTabUnselected( Tab tab, FragmentTransaction ft ) {
    //Do nothing - unless you want to do something.        
  } 
}

终于

我们现在可以利用我们创建的框架了。为此,我们需要一个 TabHolder

tabHolder = new TabHolder(); //Needs to be declared in the same class as our TabListener

我们需要将我们的 Tab 添加到此:

tabHolder.addTab( new TabItem( tab, fragmentForThisTab ) );

并且我们需要在每个 TabListener 上设置自定义 TabListener code>Tab:

tab.setTabListener( new MyTabListener() );

启用/禁用

要启用或禁用Tab,我们只需调用:

tabHolder.getTab( position ).toggleEnabled();

让我知道它是如何进行的:)

I haven't tested this - that will be up to you, but it should give you an general idea on how you could handle your problem.

There are three steps:

First step

We need something that can handle the enable/disable action for us. For this purpose we create the following class:

public class TabItem {
  private Tab tab;
  private Fragment fragment;
  private boolean enabled;

  public TabItem( Tab tab, Fragment fragment ) {
    this.tab = tab;
    this.fragment = fragment;
    enabled = true;
  }

  public Tab getTab() {
    return tab;
  }

  public Fragment getFragment() {
    return fragment;
  }

  public void toggleEnabled() {
    enabled = enabled ? false : true;
  }

  public boolean isEnabled() {
    return enabled;
  }
} 

Second step

We need something that can hold these TabItems and an easy way to access them. For this purpose we add the following class:

public class TabHolder {
  private HashMap<Integer, TabItem> tabs;

  public TabHolder() {
    tabs = new HashMap<Integer, TabItem>();
  }

  public void addTab( TabItem tab ) {
    tabs.put( tab.getTab().getPosition(), tab );
  }

  public TabItem getTab( int position ) {
    return tabs.get( position );
  }
}

Third step

We need to handle the selection of Tabs ourselves, so we need to create a custom TabListener:

private class MyTabListener implements TabListener {
  @Override
  public void onTabReselected( Tab tab, FragmentTransaction ft ) {
    //Do nothing - unless you want to do something.
  }

  @Override
  public void onTabSelected( Tab tab, FragmentTransaction ft ) {
    TabItem item = tabHolder.getTab( tab.getPosition() );
    if( item.isEnabled() ) {
      ft.remove( item.getFragment() );
      ft.commit();
    }        
  }

  @Override
  public void onTabUnselected( Tab tab, FragmentTransaction ft ) {
    //Do nothing - unless you want to do something.        
  } 
}

Finally

We can now utilize our created framework. To do so, we need a TabHolder:

tabHolder = new TabHolder(); //Needs to be declared in the same class as our TabListener

We need to add our Tabs to this:

tabHolder.addTab( new TabItem( tab, fragmentForThisTab ) );

And we need to set our custom TabListener on each Tab:

tab.setTabListener( new MyTabListener() );

Enable/Disable

To enable or disable a Tab we simply call:

tabHolder.getTab( position ).toggleEnabled();

Let me know how it goes :)

浅听莫相离 2024-12-23 00:38:24

有一种简单的方法可以从操作栏中删除选项卡栏。只需输入:

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);

这将删除所有选项卡栏。

There is a simple way to remove the Tabs-bar from the Actionbar. Just type:

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);

This will remove any tabs-bar.

你怎么这么可爱啊 2024-12-23 00:38:24

你可以覆盖:
public boolean onPrepareOptionsMenu(Menu menu),这里设置选项卡启用/禁用例如:

menu.findItem(R.id.send).setEnabled(mMessageNeedtoSend);

然后你可以设置mMessageNeedtoSend true或false,调用invalidateOptionsMenu( ) 刷新 ActionBar。

you can override:
public boolean onPrepareOptionsMenu(Menu menu), in here set the tab enable/disable for example:

menu.findItem(R.id.send).setEnabled(mMessageNeedtoSend);

and then you can set the mMessageNeedtoSend true or false,call invalidateOptionsMenu() to refresh ActionBar.

绮筵 2024-12-23 00:38:24

回答晚了,但希望这个解决方法可以帮助对此问题感兴趣的其他人。

private Handler mHandler;
private int mLastSelectedTabIndex = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // ...

    mHandler = new Handler();

    // ...

    ActionBar bar = getActionBar();

    Tab tab = bar.newTab().setText("Enabled Tab")
            .setTabListener(mTabListener);
    bar.addTab(tab);

    tab = bar.newTab().setText("Disabled Tab")
            .setTabListener(mTabListener);
    bar.addTab(tab);
}

private TabListener mTabListener = new TabListener() {
    // ...

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        int position = tab.getPosition();
        if (position == 1) {
            // Disabled tabs are selected.

            mHandler.postAtFrontOfQueue(new Runnable() {
                @Override
                public void run() {
                    getActionBar().setSelectedNavigationItem(
                            mLastSelectedTabIndex);
                }
            });
        } else {
            // Enabled tabs are selected. Do something on your own.

            mLastSelectedTabIndex = position;
        }
    }

    // ...
};

如果您使用的是 ViewPager 像往常一样,您可以简单地利用它的位置而不是 mLastSelectedTabIndex,如下所示:

getActionBar().setSelectedNavigationItem(mViewPager.getCurrentItem());

Late answer but hope this workaround might help others who are interested in this issue.

private Handler mHandler;
private int mLastSelectedTabIndex = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // ...

    mHandler = new Handler();

    // ...

    ActionBar bar = getActionBar();

    Tab tab = bar.newTab().setText("Enabled Tab")
            .setTabListener(mTabListener);
    bar.addTab(tab);

    tab = bar.newTab().setText("Disabled Tab")
            .setTabListener(mTabListener);
    bar.addTab(tab);
}

private TabListener mTabListener = new TabListener() {
    // ...

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        int position = tab.getPosition();
        if (position == 1) {
            // Disabled tabs are selected.

            mHandler.postAtFrontOfQueue(new Runnable() {
                @Override
                public void run() {
                    getActionBar().setSelectedNavigationItem(
                            mLastSelectedTabIndex);
                }
            });
        } else {
            // Enabled tabs are selected. Do something on your own.

            mLastSelectedTabIndex = position;
        }
    }

    // ...
};

If you are using ViewPager with it as usual, you can simply utilize its position instead of mLastSelectedTabIndex like this:

getActionBar().setSelectedNavigationItem(mViewPager.getCurrentItem());
℡Ms空城旧梦 2024-12-23 00:38:24

这是通过两个简单的步骤完成的(假设您为选项卡设置自定义视图):

1-禁用功能:避免更改选项卡

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        int position = tab.getPosition();
        if (mModifyMode) {
            mPagerAdapter.changeFragment(position);
        }
    }

2-禁用单击事件:

    public void setModifyMode(boolean modifyMode) {
        mModifyMode = modifyMode;
        for (int i = 0; i < mActionBar.getTabCount(); i++) {
            View parent = (View) mActionBar.getTabAt(i).getCustomView().getParent();
            parent.setEnabled(mModifyMode);
        }
    }

我现在测试了它并且它正在工作;)

This is done by two simple steps(Assuming you set custom view to the tabs):

1- Disable function: Avoid changing tabs

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        int position = tab.getPosition();
        if (mModifyMode) {
            mPagerAdapter.changeFragment(position);
        }
    }

2- Disable click events:

    public void setModifyMode(boolean modifyMode) {
        mModifyMode = modifyMode;
        for (int i = 0; i < mActionBar.getTabCount(); i++) {
            View parent = (View) mActionBar.getTabAt(i).getCustomView().getParent();
            parent.setEnabled(mModifyMode);
        }
    }

I tested it right now and it is working ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文