0

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. 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

pcu
  • 2,745
  • 4
  • 18
  • 22

1 Answers1

0

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)

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

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)

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

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;
    }
}
Community
  • 1
  • 1
triggs
  • 5,890
  • 3
  • 32
  • 31