Android 在ActionBar/ActionBarSherlock中以编程方式显示下拉列表

Android 在ActionBar/ActionBarSherlock中以编程方式显示下拉列表,android,spinner,android-actionbar,actionbarsherlock,Android,Spinner,Android Actionbar,Actionbarsherlock,我有一个活动使用ActionBar sherlock和ActionBar.NAVIGATION\u MODE\u LIST 进入页面时,我希望操作栏中的微调器在填充项目后以编程方式展开,以便用户需要选择项目。到目前为止,适配器中的第一项将自动选择 我想不出一个好方法来以编程方式扩展动作栏中的微调器。我是否需要使用自定义视图来实现此行为 我用层次查看器查看了操作栏,微调器没有设置id。有什么想法吗?下面是我如何使用ActivobBarSherlock创建自定义操作栏的代码 private voi

我有一个活动使用
ActionBar sherlock
ActionBar.NAVIGATION\u MODE\u LIST

进入页面时,我希望操作栏中的微调器在填充项目后以编程方式展开,以便用户需要选择项目。到目前为止,适配器中的第一项将自动选择

我想不出一个好方法来以编程方式扩展动作栏中的微调器。我是否需要使用自定义视图来实现此行为


我用
层次查看器查看了操作栏,微调器没有设置id。有什么想法吗?

下面是我如何使用ActivobBarSherlock创建自定义操作栏的代码

 private void createCustomActionBar() {

    List<SiteLink> links = new ArrayList<SiteLink>();
    links.add(...)  
    LinksAdapter linkAdapter = new LinksAdapter(this, R.layout.external_link, links);

    View customNav = LayoutInflater.from(this).inflate(R.layout.custom_show_action_bar, null);
    IcsSpinner spinner = (IcsSpinner)customNav.findViewById(R.id.spinner);
    spinner.setAdapter(linkAdapter);


    ImageView refresh = (ImageView) customNav.findViewById(R.id.refresh);
    refresh.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ...
        }
    });

    ImageView settings = (ImageView) customNav.findViewById(R.id.settings);
    settings.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ...
        }
    });

    getSupportActionBar().setCustomView(customNav, new ActionBar.LayoutParams(Gravity.RIGHT));
    getSupportActionBar().setDisplayShowCustomEnabled(true);

}

如果您正在使用StringAdapter,那么通过扩展StringAdapter是实现此功能的最佳且简单的方法。否则,您可以使用任何您正在使用的适配器

步骤:

  • 创建一个适配器

       class ListNavigationAdapter extends ArrayAdapter<String>{
    
    public Spinner mSpinner; // This is the spinner which is used in actionbar
    
    public ListNavigationAdapter(Context context,
            int resource,
            int textViewResourceId, 
            String[] objects) {
        super(context, resource, 
                textViewResourceId, objects);
        // TODO Auto-generated constructor stub
    }
    
    @Override
    public View getView(int position, 
            View convertView, 
            ViewGroup parent) {
        // TODO Auto-generated method stub
    
        mSpinner = (Spinner) parent;
    
        return super.getView(position, convertView, parent);
    }
    
    • 如果您想以屏幕打开的方式打开它,只需使用下面提到的代码
    @凌驾 公共布尔onCreateOptions菜单(菜单){


    试试这个。它可以工作(Y)

    此外,您可能需要在适配器上使用一个接口,以便在加载数据和填充微调器后获得回调。然后您可以执行
    performClick()
    @Georgy Gobozov。谢谢,这似乎是您必须要做的。尽管我想避免使用自定义视图。您好,谢谢您的回答,但是如果我尝试运行您提供的示例,它在这里不起作用,这是完整的代码。请帮助我错误在哪里。我想要这个问题的确切行为
    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                   android:gravity="right"
            >
    
    
        <com.actionbarsherlock.internal.widget.IcsSpinner
            android:id="@+id/spinner"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:paddingRight="20dp"
            android:layout_gravity="center"
                />
    
         <ImageView  android:layout_width="fill_parent"
                     android:layout_height="fill_parent"
                     android:src="@drawable/ic_navigation_refresh"
                     android:paddingRight="20dp"
                     android:paddingLeft="10dp"
                     android:layout_gravity="center"
                     android:background="@drawable/action_buttons_background"
                     android:id="@+id/refresh"/>
    
    
        <ImageView  android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:src="@drawable/ic_action_settings"
                    android:paddingRight="20dp"
                    android:background="@drawable/action_buttons_background"
                    android:layout_gravity="center"
                    android:id="@+id/settings"/>
    
    </LinearLayout>
    
     (getSupportActionBar().getCustomView().findViewById(R.id.spinner)).performClick();
    
       class ListNavigationAdapter extends ArrayAdapter<String>{
    
    public Spinner mSpinner; // This is the spinner which is used in actionbar
    
    public ListNavigationAdapter(Context context,
            int resource,
            int textViewResourceId, 
            String[] objects) {
        super(context, resource, 
                textViewResourceId, objects);
        // TODO Auto-generated constructor stub
    }
    
    @Override
    public View getView(int position, 
            View convertView, 
            ViewGroup parent) {
        // TODO Auto-generated method stub
    
        mSpinner = (Spinner) parent;
    
        return super.getView(position, convertView, parent);
    }
    
    // Set up the action bar to show a dropdown list.
        actionBar = getSupportActionBar();
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    
        // Set up the dropdown list navigation in the action bar.
        actionBar.setListNavigationCallbacks(
                // Specify a SpinnerAdapter to populate the dropdown list.
              mAdapter = new ListNavigationAdapter(
                        actionBar.getThemedContext(),
                        android.R.layout.simple_list_item_1,
                        android.R.id.text1,
                        new String[] {
                                getString(R.string.title_1),
                                getString(R.string.title_2)
                        }),
                this);
    
        // Inflate the menu; this adds items to the action bar if it is present.
        ...........
    
        if(mAdapter.mSpinner != null){
          mAdapter.mSpinner.performClick();
        }
        return true;
    }