在我的應用程序中,我正在使用列表導航的操作欄。有像HOME,CATEGORIES,MULTIMEDIA這樣的可能性......當我在HOME Activity上點擊導航下拉菜單,但我想從列表中隱藏HOME項目。我在該屏幕上,因此無法導航到同一屏幕。是否有一些選項可以從下拉菜單中隱藏選定/當前項目?操作欄列表導航 - 從下拉菜單中隱藏當前項目
謝謝
在我的應用程序中,我正在使用列表導航的操作欄。有像HOME,CATEGORIES,MULTIMEDIA這樣的可能性......當我在HOME Activity上點擊導航下拉菜單,但我想從列表中隱藏HOME項目。我在該屏幕上,因此無法導航到同一屏幕。是否有一些選項可以從下拉菜單中隱藏選定/當前項目?操作欄列表導航 - 從下拉菜單中隱藏當前項目
謝謝
它可以完成,這有點棘手。而且你不能使用真實的操作欄導航功能。你需要添加你自己的MenuItem和一個自定義的ActionView並擴展Spinner類(參見Spinner : onItemSelected not called when selected item remains the same,因爲下面的代碼會欺騙當前的選擇,並且當你在下拉菜單中選擇'real'選項時,它不會觸發onItemSelected事件)
關鍵是下拉可以不同於所選的顯示。對於這一個更好的解釋看這個答案Difference between getView & getDropDownView in SpinnerAdapter
下面的代碼是,只有不斷從getView()返回標題項目,但作爲預期的getDropDownView()(即返回該位置的視圖)
工作適配器每次做出新選擇時,您都必須重新創建適配器,但它會從下拉列表中刪除當前活動。
class NavigationAdapter extends BaseAdapter implements SpinnerAdapter{
ArrayList<NavigationItem> items;
NavigationItem titleItem;
public NavigationAdapter(ArrayList<NavigationItem> items, NavigationItem titleItem){
this.items = items;
this.titleItem = titleItem;
//make sure the title item isn't also in our list
Iterator<NavigationItem> iterator = items.iterator();
while(iterator.hasNext()){
NavigationItem item = iterator.next();
if(item.getId() == titleItem.getId()){
iterator.remove();
}
}
}
@Override
public int getCount() {
return items.size();
}
@Override
public NavigationItem getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//only ever return the title item from this method
if(convertView == null){
convertView = LayoutInflater.from(Main.this).inflate(R.layout.listrow_single_line_with_image, null);
}
NavigationItem item = titleItem;
TextView tv = (TextView) convertView;
tv.setText(item.getName());
Drawable d = getResources().getDrawable(item.getDrawableId());
d.setBounds(0, 0, 56, 56);
tv.setCompoundDrawables(d, null, null, null);
return convertView;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = LayoutInflater.from(Main.this).inflate(R.layout.listrow_single_line_with_image, null);
}
NavigationItem item = getItem(position);
TextView tv = (TextView) convertView;
tv.setText(item.getName());
Drawable d = getResources().getDrawable(item.getDrawableId());
d.setBounds(0, 0, 56, 56);
tv.setCompoundDrawables(d, null, null, null);
return convertView;
}
}
的NavigationItem在上面只是一個包裝類,在這裏它的完整性
class NavigationItem{
int drawableId;
String name;
int id;
public NavigationItem(int id, String name, int drawableId) {
super();
this.drawableId = drawableId;
this.name = name;
this.id = id;
}
public int getDrawableId() {
return drawableId;
}
public void setDrawableId(int drawableId) {
this.drawableId = drawableId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}