我可以禁用ActionBar的导航微调? [英] Can I disable ActionBar's navigation Spinner?

查看:115
本文介绍了我可以禁用ActionBar的导航微调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现的是禁止动作条上的所有项目除了一个什么。我有一个自定义的动作条菜单,几个 TextViews ,一个按钮微调从ListNavigation。 微调是因为bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST)的创建; 是这样的:

What I want to achieve is disabling all items on ActionBar except one. I have a custom ActionBar with Menu,several TextViews, one Button and a Spinner from ListNavigation. Spinner is created because of bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); like this :

    SpinnerAdapter spinnerAdapter = new ArrayAdapter<String>(this.getActivity(), R.layout.action_bar_spinner, names);
    // "listener" for spinner
    bar.setListNavigationCallbacks(spinnerAdapter, new OnNavigationListener() {
        @Override
        public boolean onNavigationItemSelected(int position, long itemId) {
            // do some stuff

            return true;
        }
    });

我要禁用微调,使其处于非活动状态,但仍可见与去年项目中选择禁用it.In总之我需要像<$ C $前C> bar.getNavigationSpinner.setEnabled(假),如果它的存在。问题是:是否有某种解决办法吗?如果没有,有没有一种方法来禁用整个动作条,但保持可见?

I want to disable the Spinner, so that it is inactive, but still visible with last item selected before disabling it.In short I need something like bar.getNavigationSpinner.setEnabled(false) if it existed. Question is: Is there some kind of workaround ? If not, is there a way to disable whole ActionBar,but keep it visible ?

注:我想实现在一个片段

Note: I want to achieve it in a Fragment.

推荐答案

是的,这是可以禁用微调用于列表导航动作条。但是,岂不等于不是一个简单的解决方案,而不是黑客。动作条不提供直接访问微调视图。不幸的是,微调在一个私人code没有任何标识被创建。

Yes, it is possible to disable the Spinner used in list navigation in ActionBar. But is't not a straightforward solution, rather a hack. ActionBar doesn't provide a direct access to the Spinner view. Unfortunately the Spinner is created in a private code without any id.

那么如何访问微调实例?一种解决方案可能是通过Java反射API来访问它,但我不会建议。

So how to access the Spinner instance? One solution could be to access it via Java reflection API, but I wouldn't recommend that.

一个更好的解决办法是让根视图当前活动,遍历它的子视图(操作栏和所有它的观点是present在视图层次),并找到合适的微调。由于在行动微调酒吧是presumably您还没有创建自己唯一的一个,你应该能够从其他人区分开来。

A better solution is to get the root view for the current Activity, traverse it's child views (action bar and all it's views are present in the view hierarchy) and find the proper Spinner. Since the Spinner in action bar is presumably the only one you haven't created yourself, you should be able to distinguish it from the others.

在获取的查看 =htt​​p://stackoverflow.com/questions/4486034/get-root-view-from-current-活动>这太问题

Getting the root View is described in this SO question.

遍历很简单,记住只是承受,如果您使用的是 ActionBarSherlock ,你必须寻找 IcsSpinner 而不是微调(<$ C C $> IcsSpinner 不从微调延长)。

The traversal is rather simple, just bear in mind that if you are using ActionBarSherlock, you have to look for IcsSpinner instead of Spinner (IcsSpinner does not extend from Spinner).

private View findActionBarSpinner() {
    View rootView = findViewById(android.R.id.content).getRootView();
    List<View> spinners = traverseViewChildren( (ViewGroup) rootView );
    return findListNavigationSpinner(spinners); // implement according to your layouts
}

private List<View> traverseViewChildren(ViewGroup parent) {
    List<View> spinners = new ArrayList<View>();
    for (int i = 0; i < parent.getChildCount(); i++) {
        View child = parent.getChildAt(i);
        if (child instanceof Spinner) {
            spinners.add( child );
        } else if (child instanceof IcsSpinner) { // add this if you are using ActionBarSherlock
            spinners.add( child );
        } else if (child instanceof ViewGroup) {
            spinners.addAll( traverseViewChildren( (ViewGroup) child ) );
        }
    }
    return spinners;
}

功能 findListNavigationSpinner 应的方式,你能分辨其他纱厂实施。如果您没有使用任何微调(或者从它派生的任何视图),返回的列表应该只包含一个项目。

The function findListNavigationSpinner should be implemented in a way that you are able to distinguish the other spinners. If you are not using any Spinner (or any view derived from it), the returned list should contain just one item.

在code以上介绍了如何获得微调活动。当然,这不是一个问题,禁用微调片段中。该片段的引用,它的活性,使该活动可以通过一些接口暴露code的片段。

The code above describes how to get the Spinner in an Activity. Naturally, it is not a problem to disable the Spinner from within a Fragment. The fragment has a reference to it's activity, so the activity can expose the code to the fragment via some interface.

这篇关于我可以禁用ActionBar的导航微调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆