在ActionBar / ActionBarSherlock中以编程方式显示下拉列表 [英] Show dropdown programmatically in ActionBar / ActionBarSherlock
问题描述
我有一个使用 ActionBarSherlock
和 ActionBar.NAVIGATION_MODE_LIST
的活动。
I have an activity using ActionBarSherlock
with ActionBar.NAVIGATION_MODE_LIST
.
进入页面时,我希望操作栏中的微调器在填充项目后以编程方式展开,因此用户需要选择一个项目。到目前为止,适配器中的第一项是自动选择的。
When entering the page I want the spinner in the action bar to expand programmatically after it's populated with items so the user needs to pick an item. As of now the first item in the adapter is selected automatically.
我想不出一种以编程方式在操作栏中扩展微调器的好方法。我需要使用自定义视图来实现此行为吗?
I can't figure out a nice way to expand the spinner in the action bar programmatically. Do I need to use a custom view to achieve this behavior?
我已经使用 HierarchyViewer $ c $查看了操作栏c>并且微调器没有设置ID。有任何想法吗?
I've looked on the action bar with the HierarchyViewer
and the spinner does not have an id set. Any ideas?
推荐答案
以下是我如何使用actiobBarSherlock创建自定义操作栏的代码
Here is code how I create custom action bar with 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();
这篇关于在ActionBar / ActionBarSherlock中以编程方式显示下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!