问题
I am trying to achieve NAVIGATION_MODE_LIST like gmail Android Application. My main issue is to hide the currently selected item from spinner list. so for example as shown here in
if you select Sent then it will only have other elements shown in the spinner.My Understanding says that it is a custom ActionView rather than using NAVIGATION_MODE_LIST with custom adapter.
回答1:
If someone else is looking for the solution to this problem here it is,
Here is sample code which is written with the help of link
use following code to create your adapter and join it to ActionBar List Navigation
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
itemArr = getResources().getStringArray(R.array.array_spinner_items);
items = toArrayList(itemArr, null);
navigationAdapter = new CustomAdapter(this, R.layout.navigation_item_layout, items);
actionBar.setListNavigationCallbacks(navigationAdapter, this);
actionBar.setDisplayShowTitleEnabled(false);
Extend BaseAdapter
or ArrayAdapter
and implement SpinnerAdapter
In your adapter override getDropdownView which is responsible for individual item view in dropdown and override getView which is responsible for the view appearing in the ActionBar
`public class CustomAdapter extends ArrayAdapter implements SpinnerAdapter {
Context context;
int textViewResourceId;
ArrayList<String> arrayList;
public CustomAdapter(Context context, int textViewResourceId, ArrayList<String> arrayList) {
super(context, textViewResourceId, arrayList);
this.context = context;
this.textViewResourceId = textViewResourceId;
this.arrayList = arrayList;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent){
if (convertView == null)
{
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//convertView = vi.inflate(android.R.layout.simple_spinner_dropdown_item, null);
convertView = vi.inflate(R.layout.navigation_item_layout, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.navigation_item);
textView.setText(arrayList.get(position).toString());//after changing from ArrayList<String> to ArrayList<Object>
if (position == curitem) {
textView.setHeight(0);
}
else{
textView.setHeight(60);
}
return convertView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.navigation_item_layout, null);
}
TextView textview = (TextView) convertView.findViewById(R.id.navigation_item);
textview.setText(itemArr[position].toUpperCase());
textview.setTextColor(Color.RED);
return convertView;
}
}`
Here is the layout file for spinner item navigation_tem_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/navigation_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:padding="10dp" />
回答2:
You may be able to not provide the current entry in the navigation list, and just display it in the custom SpinnerAdapter in getView() method.
I just wrote a post with full source code, but my example uses static typed-arrays - you could change it to use a custom NavigationListItem class (or whatever you want to call it) and build a dynamic list per activity so that doesn't include the current one. You will need to be careful though as the spinner will try to select the first entry upon start, but you can display what you want in getView() as opposed to using the entry provided by position.
dandar3.blogspot.com/2013/03/actionbarsherlock-custom-list-navigation.html
来源:https://stackoverflow.com/questions/13756713/gmail-android-app-like-actionview-spinner-navigation-mode-list