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

标签 android spinner android-actionbar actionbarsherlock

我有一项使用 ActionBarSherlockActionBar.NAVIGATION_MODE_LIST 的 Activity 。

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

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

我查看了带有 HierarchyViewer 的操作栏,但微调器没有设置 id。有什么想法吗?

最佳答案

这是我如何使用 actiobBarSherlock 创建自定义操作栏的代码

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

}

适配器

private static class LinksAdapter extends ArrayAdapter<SiteLink> {

    private List<SiteLink> strings;
    private Context context;

    private LinksAdapter(Context context, int textViewResourceId, List<SiteLink> objects) {
        super(context, textViewResourceId, objects);
        this.strings = objects;
        this.context = context;
    }

    @Override
    public int getCount() {
        if (strings == null) return 0;
        return strings.size();
    }

    @Override
    public SiteLink getItem(int position) {
        return super.getItem(position);
    }


    // return views of drop down items
    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {

        final SiteLink siteLink = strings.get(position);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // at 0 position show only icon

        TextView site = (TextView) inflater.inflate(R.layout.external_link, null);
        site.setText(siteLink.getName());

        site.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(siteLink.getUrl()));
                context.startActivity(i);
            }
        });
        return site;


    }


    // return header view of drop down
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        return inflater.inflate(R.layout.icon, null);
    }
}

布局

<?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();

关于android - 在 ActionBar/ActionBarSherlock 中以编程方式显示下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11737500/

相关文章:

visual-studio-2005 - MFC:为什么我的旋转控制向后工作

Android加载联系人太慢

android - (Android Studio) 自定义操作栏布局未填满整个操作栏

android - 在 ActionBar 中为 MenuItem 使用选择器

Android:NoSuchFieldError 指的是 appcompat_v7 缺少 R$styleable.Theme_windowActionBar

android - 以编程方式切换 GPS Android 4.4

android - 如何在 webview 中使用微调器?

android - 如何清除BackStack?

android - LinearLayout 中的 RelativeLayout?

在您滚动微调器之前,Android Spinner Radiobutton View 不会响应