10

我有一个使用ActionBarSherlockwith的活动ActionBar.NAVIGATION_MODE_LIST

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

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

我查看了带有 的操作栏,HierarchyViewer并且微调器没有设置 id。有任何想法吗?

4

2 回答 2

14

这是我如何使用 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();
于 2012-08-03T10:43:41.663 回答
0

如果您正在使用它,通过扩展 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);
    }
    

    }

  • 将适配器设置为操作栏

// 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);
  • 如果你想在屏幕打开时打开它,只需使用下面提到的代码

@Override public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    ...........

    if(mAdapter.mSpinner != null){
      mAdapter.mSpinner.performClick();
    }
    return true;
}

试试这个 。有效(Y)

于 2014-09-04T20:26:36.153 回答