ActionBarCompat Spinner无法正常工作

时间:2013-11-13 19:11:48

标签: android android-actionbar android-support-library

我有一个ActionBar微调器的问题。我正在使用ActionBarCompat。我在姜饼,JB和KK上看到了这个问题。

这是我的活动:

public class WikiActivity extends ActionBarActivity implements ActionBar.OnNavigationListener {
    protected ArrayList<Project> mProjects;
    protected ArrayAdapter<Project> mAdapter;
    public int mCurrentProjectPosition;

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initProjectsSpinner(savedInstanceState);
    }

    @Override
    public boolean onOptionsItemSelected(final MenuItem item) {
        L.d("item" + item);
        switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshProjectsList();
    }

    private void initProjectsSpinner(Bundle savedInstanceState) {
        mCurrentProjectPosition = -1;
        if (savedInstanceState == null) {
            mProjects = new ArrayList<Project>();
            mAdapter = new ProjectsSpinnerAdapter(this, R.layout.main_nav_item, mProjects);
        }
    }

    private void enableListNavigationMode() {
        L.d("current proj pos=" + mCurrentProjectPosition);
        final ActionBar ab = getSupportActionBar();
        ab.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
        ab.setListNavigationCallbacks(mAdapter, this);
        if (mCurrentProjectPosition >= 0) {
            ab.setSelectedNavigationItem(mCurrentProjectPosition);
        }
    }

    public void refreshProjectsList() {
        L.d("");
        if (mProjects != null && mProjects.size() > 0) {
            return;
        }
        L.d("");
        Project p = new Project();
        p.name = "test1";
        mProjects.add(p);
        p = new Project();
        p.name = "test2";
        mProjects.add(p);
        p = new Project();
        p.name = "test3";
        mProjects.add(p);
        p = new Project();
        p.name = "test4";
        mProjects.add(p);

        enableListNavigationMode();
    }

    @Override
    public boolean onNavigationItemSelected(final int itemPosition, final long itemId) {
        L.d("position: " + itemPosition);
        if (mProjects == null || itemPosition < 0 || itemPosition > mProjects.size()) {
            return true;
        }

        mCurrentProjectPosition = itemPosition;
        Project currentProject = mProjects.get(mCurrentProjectPosition);
        L.d("currentProject=" + currentProject);

        return true;
    }
}

这是适配器。我同意它可以改进(例如使用视图持有者),但这不是问题所在。

public class ProjectsSpinnerAdapter extends ArrayAdapter<Project> {
    ArrayList<Project> data;
    LayoutInflater inflater;
    public ProjectsSpinnerAdapter(final Context ctx, final int textViewResourceId, final ArrayList<Project> data) {
        super(ctx, textViewResourceId, data);
        this.data = data;
        inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.main_nav_item_in_actionbar, null);
        }
        final TextView text = (TextView) convertView.findViewById(R.id.main_nav_ab_item_text);
        if (text != null && data != null && position < data.size()) {
            text.setText(data.get(position).name);
        }
        return convertView;
    }

    @Override
    public View getDropDownView(final int position, View convertView, final ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.main_nav_item, null);
        }
        final TextView text = (TextView) convertView.findViewById(R.id.main_nav_item_text);
        final View image = convertView.findViewById(R.id.main_nav_item_icon);
        if (text != null && image != null) {
            text.setText(data.get(position).name);
            image.setVisibility(View.INVISIBLE);
        }
        return convertView;
    }

    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public Project getItem(final int position) {
        return null;
    }

    @Override
    public long getItemId(final int position) {
        return 0;
    }
}

事实上这很简单。我的代码更复杂,但我可以将它减少到这个并且它仍然失败。它曾经与ABS完美配合,而不是我切换到ActionBarCompat它不再工作了。

当我触摸操作栏中的微调器时,没有任何反应。它没有展开。怎么了?

这是日志:

--- ProjectsSpinnerAdapter.java:25 ProjectsSpinnerAdapter::<init>() ---
Context: net.bicou.redmine.app.wiki.WikiActivity@431e35c0, textViewResourceId: 2130903113, data: []
--- WikiActivity.java:65 WikiActivity::refreshProjectsList() ---
--- WikiActivity.java:69 WikiActivity::refreshProjectsList() ---
--- WikiActivity.java:54 WikiActivity::enableListNavigationMode() ---
current proj pos=-1
--- ProjectsSpinnerAdapter.java:66 ProjectsSpinnerAdapter::getCount() ---
--- ProjectsSpinnerAdapter.java:66 ProjectsSpinnerAdapter::getCount() ---
--- ProjectsSpinnerAdapter.java:78 ProjectsSpinnerAdapter::getItemId() ---
position: 0
--- ProjectsSpinnerAdapter.java:78 ProjectsSpinnerAdapter::getItemId() ---
position: 0
--- ProjectsSpinnerAdapter.java:66 ProjectsSpinnerAdapter::getCount() ---
--- ProjectsSpinnerAdapter.java:66 ProjectsSpinnerAdapter::getCount() ---
--- ProjectsSpinnerAdapter.java:78 ProjectsSpinnerAdapter::getItemId() ---
position: 0
--- ProjectsSpinnerAdapter.java:78 ProjectsSpinnerAdapter::getItemId() ---
position: 0
--- WikiActivity.java:88 WikiActivity::onNavigationItemSelected() ---
position: 0
--- WikiActivity.java:95 WikiActivity::onNavigationItemSelected() ---
currentProject=Project { id: 0, name: test1, identifier: null }
--- ProjectsSpinnerAdapter.java:66 ProjectsSpinnerAdapter::getCount() ---
--- ProjectsSpinnerAdapter.java:32 ProjectsSpinnerAdapter::getView() ---
position: 0, convertView: null, parent: android.widget.Spinner{43163858 VFED.... ......I. 0,0-0,0 #1020261 android:id/action_bar_spinner}
--- ProjectsSpinnerAdapter.java:66 ProjectsSpinnerAdapter::getCount() ---
--- ProjectsSpinnerAdapter.java:32 ProjectsSpinnerAdapter::getView() ---
position: 0, convertView: null, parent: android.widget.Spinner{43163858 VFED.... ......I. 0,0-0,0 #1020261 android:id/action_bar_spinner}
--- ProjectsSpinnerAdapter.java:32 ProjectsSpinnerAdapter::getView() ---
position: 1, convertView: android.widget.LinearLayout{42c86858 V.E..... ......I. 0,0-0,0}, parent: android.widget.Spinner{43163858 VFED.... ......I. 0,0-0,
--- ProjectsSpinnerAdapter.java:32 ProjectsSpinnerAdapter::getView() ---
position: 2, convertView: android.widget.LinearLayout{42c86858 V.E..... ......I. 0,0-0,0}, parent: android.widget.Spinner{43163858 VFED.... ......I. 0,0-0,
--- ProjectsSpinnerAdapter.java:32 ProjectsSpinnerAdapter::getView() ---
position: 3, convertView: android.widget.LinearLayout{42c86858 V.E..... ......I. 0,0-0,0}, parent: android.widget.Spinner{43163858 VFED.... ......I. 0,0-0,
--- ProjectsSpinnerAdapter.java:66 ProjectsSpinnerAdapter::getCount() ---
--- ProjectsSpinnerAdapter.java:32 ProjectsSpinnerAdapter::getView() ---
position: 0, convertView: null, parent: android.widget.Spinner{43163858 VFED.... ......I. 0,0-0,0 #1020261 android:id/action_bar_spinner}
--- ProjectsSpinnerAdapter.java:78 ProjectsSpinnerAdapter::getItemId() ---
position: 0
--- ProjectsSpinnerAdapter.java:78 ProjectsSpinnerAdapter::getItemId() ---
position: 0
--- ProjectsSpinnerAdapter.java:32 ProjectsSpinnerAdapter::getView() ---
position: 0, convertView: null, parent: android.widget.Spinner{43163858 VFED.... ......ID 0,0-204,144 #1020261 android:id/action_bar_spinner}
--- ProjectsSpinnerAdapter.java:78 ProjectsSpinnerAdapter::getItemId() ---
position: 0
--- ProjectsSpinnerAdapter.java:66 ProjectsSpinnerAdapter::getCount() ---
--- ProjectsSpinnerAdapter.java:32 ProjectsSpinnerAdapter::getView() ---
position: 0, convertView: null, parent: android.widget.Spinner{43163858 VFED.... ......ID 0,0-204,144 #1020261 android:id/action_bar_spinner}
--- ProjectsSpinnerAdapter.java:66 ProjectsSpinnerAdapter::getCount() ---
--- ProjectsSpinnerAdapter.java:32 ProjectsSpinnerAdapter::getView() ---
position: 0, convertView: null, parent: android.widget.Spinner{43163858 VFED.... ......ID 0,0-204,144 #1020261 android:id/action_bar_spinner}
--- ProjectsSpinnerAdapter.java:32 ProjectsSpinnerAdapter::getView() ---
position: 1, convertView: android.widget.LinearLayout{42d3a630 V.E..... ......I. 0,0-0,0}, parent: android.widget.Spinner{43163858 VFED.... ......ID 0,0-20
er}
--- ProjectsSpinnerAdapter.java:32 ProjectsSpinnerAdapter::getView() ---
position: 2, convertView: android.widget.LinearLayout{42d3a630 V.E..... ......I. 0,0-0,0}, parent: android.widget.Spinner{43163858 VFED.... ......ID 0,0-20
er}
--- ProjectsSpinnerAdapter.java:32 ProjectsSpinnerAdapter::getView() ---
position: 3, convertView: android.widget.LinearLayout{42d3a630 V.E..... ......I. 0,0-0,0}, parent: android.widget.Spinner{43163858 VFED.... ......ID 0,0-20
er}
--- ProjectsSpinnerAdapter.java:78 ProjectsSpinnerAdapter::getItemId() ---
position: 0
--- ProjectsSpinnerAdapter.java:78 ProjectsSpinnerAdapter::getItemId() ---
position: 0

然后我多次敲击旋转器。没有。然后我点击了操作栏中的向上按钮:

--- WikiActivity.java:26 WikiActivity::onOptionsItemSelected() ---
itemandroid.support.v7.internal.view.menu.MenuItemWrapperJB@432778d8

真的,那个旋转器没有任何反应。这是

2 个答案:

答案 0 :(得分:1)

发现了这个问题。这是因为我的主题/风格。

这是应用主题(活动使用的主题):

<item name="actionDropDownStyle">@style/Theme.MyMine.DropDownNav</item>
<item name="android:actionDropDownStyle">@style/Theme.MyMine.DropDownNav</item>

这是样式声明:

<style name="Theme.MyMine.DropDownNav">
    <item name="android:background">@drawable/spinner_background_ab_mymine</item>
    <item name="android:popupBackground">@drawable/menu_dropdown_panel_mymine</item>
    <item name="android:dropDownSelector">@drawable/selectable_background_mymine</item>
</style>

这是固定的:我只需要添加父母。这是HC +设备的修复:

<style name="Theme.MyMine.DropDownNav"
       parent="android:Widget.Holo.Light.Spinner">
    <item name="android:background">@drawable/spinner_background_ab_mymine</item>
    <item name="android:popupBackground">@drawable/menu_dropdown_panel_mymine</item>
    <item name="android:dropDownSelector">@drawable/selectable_background_mymine</item>
</style>

答案 1 :(得分:0)

永远不要使用NAVIGATION_MODE_LIST和onNavigationItemSelected它是不值得的!

@覆盖  public boolean onNavigationItemSelected(int position,long itemId)

您也无法使用菜单执行此操作:

@Override public boolean onCreateOptionsMenu(菜单菜单){getMenuInflater()。inflate(R.menu.activity_main,menu);

由于通货膨胀排序。原因:

(1)它生成一个“隐藏”的微调器,你无法获得id。 (2)你不能自定义这个微调器(3)你保存30行代码,但如果你想添加第二个双向微调器(4),即使在“简单代码”(一个微调器)的特殊情况下也是如此),你输了很多。 (5)你不能使用标签。 (6)为什么你不想要类似的代码?

关键是actionBar.setCustomView(R.layout.action_bar_custom);和spinner.setOnItemSelectedListener(每个微调器的新AdapterView.OnItemSelectedListener()...。相信我,我在每个解决方案中都失去了几个小时。