我对 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
真的,那个微调器上什么也没有发生。它死了。