我可以禁用ActionBar的导航Spinner吗?(Can I disable ActionBar's navigation Spinner?)

编程入门 行业动态 更新时间:2024-10-09 05:18:28
我可以禁用ActionBar的导航Spinner吗?(Can I disable ActionBar's navigation Spinner?)

我想要实现的是禁用ActionBar上的所有项目,除了一个。 我有一个带有Menu的自定义ActionBar ,几个TextViews ,一个Button和一个来自ListNavigation的Spinner 。 由于bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);而创建了Spinner bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); 喜欢这个 :

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; } });

我想禁用Spinner ,因此它处于非活动状态,但在禁用它之前仍然可以看到最后一项。总之我需要像bar.getNavigationSpinner.setEnabled(false)这样的东西。 问题是:是否有某种解决方法? 如果没有,有没有办法禁用整个ActionBar ,但保持可见?

注意:我想在Fragment中实现它。

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; } });

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.

最满意答案

是的,可以禁用ActionBar中列表导航中使用的Spinner 。 但这不是一个简单的解决方案,而是一个黑客。 ActionBar不提供对Spinner视图的直接访问。 不幸的是, Spinner是在私有代码中创建的,没有任何id。

那么如何访问Spinner实例呢? 一种解决方案可能是通过Java反射API访问它,但我不建议这样做。

更好的解决方案是获取当前Activity的根视图,遍历它的子视图(操作栏及其视图层次结构中存在的所有视图)并找到正确的Spinner 。 由于操作栏中的Spinner可能是您自己没有创建的唯一一个,因此您应该能够将其与其他人区分开来。

在这个SO问题中描述了获取根View 。

遍历非常简单,请记住,如果使用ActionBarSherlock ,则必须查找IcsSpinner而不是Spinner ( IcsSpinner不会从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应该以一种能够区分其他微调器的方式实现。 如果您没有使用任何Spinner (或从中派生的任何视图),则返回的列表应该只包含一个项目。

上面的代码描述了如何在Activity获取Spinner 。 当然,从Fragment禁用Spinner不是问题。 片段具有对其活动的引用,因此活动可以通过某个接口将代码暴露给片段。

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.

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

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.

Getting the root View is described in this SO question.

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;
}
 

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.

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,Spinner,电脑培训,计算机培训,IT培训"/> <meta name="description

本文发布于:2023-07-18 07:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1157065.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Spinner   ActionBar   navigation   disable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!