简体   繁体   English

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

[英]Actionbar List Navigation - hide current item from dropdown

In my application I am using actionbar with list navigation. 在我的应用程序中,我将动作栏与列表导航一起使用。 There are possibilities like HOME, CATEGORIES, MULTIMEDIA... When I am on HOME Activity I tap on navigation dropdown, but I want to hide HOME item from list. 可能有诸如HOME,类别,多媒体...之类的功能。当我进行HOME活动时,我点击导航下拉菜单,但我想从列表中隐藏HOME项目。 I am on that screen so it has no sense to navigate to the same screen. 我在那个屏幕上,所以导航到同一屏幕上没有任何意义。 Is there some option to hide selected/current item from dropdown? 是否有一些选项可以从下拉菜单中隐藏所选/当前项目?

Thanks 谢谢

It can be done, it's a little tricky. 可以做到,有点棘手。 And you can't use the real Action Bar Navigation feature. 而且您不能使用真正的操作栏导航功能。 You need to add your own MenuItem with a custom ActionView and extend the Spinner class (see Spinner : onItemSelected not called when selected item remains the same since the code below fakes the current selection and when you select the in the drop down the 'real' option it wont trigger the onItemSelected event) 您需要添加带有自定义ActionView的自己的MenuItem并扩展Spinner类(请参见Spinner:当选定的项目保持不变时,不会调用onItemSelected,因为下面的代码伪造了当前选择,并且在下拉菜单中选择了“ real”选项,它将不会触发onItemSelected事件)

The key is the drop down can be different from the selected one displayed. 关键是下拉菜单可能与显示的所选菜单不同。 For a better explanation of this see this answer Difference between getView & getDropDownView in SpinnerAdapter 有关此内容的更好说明,请参见此答案SpinnerAdapter中的getView和getDropDownView之间的区别

The code below is an adapter that only ever returns the title item from getView() but works as expected for the getDropDownView() (ie return a view for that position) 下面的代码是一个适配器,它只能从getView()返回标题项,但可以对getDropDownView()正常工作(即返回该位置的视图)

You will have to recreate the adapter each time a new selection is made but it will remove the current activity from the drop down. 每次进行新选择时,都必须重新创建适配器,但是它将从下拉列表中删除当前活动。

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;

    }

}

The NavigationItem in the above is just a wrapper class, here it is for completeness 上面的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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM