【问题标题】:Actionbar List Navigation - hide current item from dropdown操作栏列表导航 - 从下拉列表中隐藏当前项目
【发布时间】:2012-07-19 11:23:26
【问题描述】:

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

谢谢

【问题讨论】:

    标签: android android-actionbar actionbarsherlock


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2018-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多