0

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

谢谢

4

1 回答 1

0

可以的,就是有点麻烦。而且您不能使用真正的操作栏导航功能。您需要使用自定义 ActionView 添加自己的 MenuItem 并扩展 Spinner 类(请参阅Spinner : onItemSelected not called when selected item保持不变,因为下面的代码伪造了当前选择,当您在下拉菜单中选择“真实”时选项它不会触发 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;
    }
}
于 2013-08-02T10:23:24.853 回答