1

我对 ActionBar 微调器有疑问。我正在使用 ActionBarCompat。我在 Gingerbread、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

真的,那个微调器上什么也没有发生。它死了

4

2 回答 2

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>
于 2013-11-16T16:23:48.987 回答
0

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

@Override public boolean onNavigationItemSelected(int position, long itemId)

您也不能使用菜单来执行此操作:

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

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

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

关键是 actionBar.setCustomView(R.layout.action_bar_custom); 和 spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()... 对于每个微调器。相信我,我在尝试每个解决方案时都浪费了几个小时。

于 2015-08-12T00:33:58.753 回答