操作栏列表导航 - 从下拉列表中隐藏当前项目

时间:2012-07-19 11:23:26

标签: android android-actionbar actionbarsherlock

在我的应用程序中,我使用带有列表导航的操作栏。有家庭,类别,多媒体等可能性...当我在HOME活动时我点击导航下拉菜单,但我想从列表中隐藏HOME项目。我在那个屏幕上,所以无法导航到同一个屏幕。是否有一些选项可以从下拉列表中隐藏所选/当前项目?

由于

1 个答案:

答案 0 :(得分:0)

可以做到,这有点棘手。而且您无法使用真正的操作栏导航功能。您需要使用自定义ActionView添加自己的MenuItem并扩展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;
    }
}