Search code examples
androiddrop-down-menuactionbarsherlock

Android: Sherlock action bar drop down menu with custom view


perhaps this is a duplicate question like: Android: Sherlock action bar drop down, but the answer didn't solve my problem

I want to implement a drop down menu on sherlock actionbar with a custom initial view like this: enter image description here

My code can successfully create the list as I wanted, but when I clicked on the item, the "onNavigationItemSelected" function didn't receive anything

My code:

public void someFunction(){             
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setIcon(R.drawable.edit_done);
    getSupportActionBar().setNavigationMode(com.actionbarsherlock.app.ActionBar.NAVIGATION_MODE_LIST);
    final String[] choices = {"Select all"};
    CustomSpinnerAdapter spinnerAdapter = new CustomSpinnerAdapter(MainActivity.getInstance(), android.R.layout.simple_spinner_dropdown_item, choices);
    getSupportActionBar().setListNavigationCallbacks(spinnerAdapter, this);
    spinnerAdapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
}
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
    Log.d("OUTPUT", "selected menu position:" + itemPosition);

    return true;
}


private class CustomSpinnerAdapter extends ArrayAdapter<String> implements SpinnerAdapter {
    private LayoutInflater layoutInflater;

    public CustomSpinnerAdapter(Context context, int textViewResourceId, String[] values) {
        super(context, textViewResourceId, values);
        this.layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return imageview1;
    }

    @Override
    public View getDropDownView(int position, View convertView,
            ViewGroup parent) {
        convertView = layoutInflater.inflate(R.layout.sherlock_spinner_dropdown_item, parent, false);
        ((TextView)convertView).setText("Select all");
        return convertView;
    }

}

as you can see, the log in "onNavigationItemSelected" only appears once when "somefunction" is called, after that, you wouldn't see any log when you click on list items

hopes someone can solve my problem


Solution

  • The problem is because I have only one item in my list, this only one item is selected when the list is created and onNavigationItemSelected will not responde when you click on the item that had been selected, I guess.

    I added another item in the list and the problem solved.