Android Actionbar列表导航-从下拉列表中隐藏当前项

Android Actionbar列表导航-从下拉列表中隐藏当前项,android,android-actionbar,actionbarsherlock,Android,Android Actionbar,Actionbarsherlock,在我的应用程序中,我使用带有列表导航的actionbar。有可能像家庭,分类,多媒体。。。当我在家庭活动中时,我点击导航下拉列表,但我想从列表中隐藏家庭项目。我在那个屏幕上,所以导航到同一个屏幕是没有意义的。是否有从下拉列表中隐藏选定/当前项目的选项 谢谢这是可以做到的,有点棘手。你不能使用真正的动作栏导航功能。您需要使用自定义ActionView添加自己的MenuItem并扩展微调器类(请参阅,因为下面的代码伪造了当前选择,并且当您在下拉菜单中选择“real”选项时,它不会触发onItemSe

在我的应用程序中,我使用带有列表导航的actionbar。有可能像家庭,分类,多媒体。。。当我在家庭活动中时,我点击导航下拉列表,但我想从列表中隐藏家庭项目。我在那个屏幕上,所以导航到同一个屏幕是没有意义的。是否有从下拉列表中隐藏选定/当前项目的选项


谢谢

这是可以做到的,有点棘手。你不能使用真正的动作栏导航功能。您需要使用自定义ActionView添加自己的MenuItem并扩展微调器类(请参阅,因为下面的代码伪造了当前选择,并且当您在下拉菜单中选择“real”选项时,它不会触发onItemSelected事件)

关键是下拉列表可能与显示的所选内容不同。要更好地解释这一点,请参见此答案

下面的代码是一个适配器,它只从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;

    }

}
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;
    }
}