2012-07-19 98 views
0

在我的应用程序中,我正在使用列表导航的操作栏。有像HOME,CATEGORIES,MULTIMEDIA这样的可能性......当我在HOME Activity上点击导航下拉菜单,但我想从列表中隐藏HOME项目。我在该屏幕上,因此无法导航到同一屏幕。是否有一些选项可以从下拉菜单中隐藏选定/当前项目?操作栏列表导航 - 从下拉菜单中隐藏当前项目

谢谢

回答

0

它可以完成,这有点棘手。而且你不能使用真实的操作栏导航功能。你需要添加你自己的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; 
    } 
}