actionbar spinner-用法实例
今天需要更改一个actionbar上的spinner的字体颜色,结果试了好长时间都没有解决,最后才发现,原来他是在代码下增加的一个textview,然后使用adapter加载的,并不是直接用framework下的布局。现在总结下spinner的使用方法,直接见下面的代码
1)首先要有一个spinneradapter来跟spinner进行数据加载
private BaseAdapter mStackAdapter = new BaseAdapter() { @Override public int getCount() { return mState.stack.size(); } @Override public DocumentInfo getItem(int position) { return mState.stack.get(mState.stack.size() - position - 1); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.item_title, parent, false); } final TextView title = (TextView) convertView.findViewById(android.R.id.title); final DocumentInfo doc = getItem(position); if (position == 0) { final RootInfo root = getCurrentRoot(); title.setText(root.title); } else { title.setText(doc.displayName); } // No padding when shown in actionbar convertView.setPadding(0, 0, 0, 0); return convertView; } @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.item_title, parent, false); } final ImageView subdir = (ImageView) convertView.findViewById(R.id.subdir); final TextView title = (TextView) convertView.findViewById(android.R.id.title); final DocumentInfo doc = getItem(position); if (position == 0) { final RootInfo root = getCurrentRoot(); title.setText(root.title); subdir.setVisibility(View.GONE); } else { title.setText(doc.displayName); subdir.setVisibility(View.VISIBLE); } return convertView; } };
上面有两个比较重要的方法
一个是getview:这个方法是加载提示prompt用的
一个是getDropView:这个是加载下拉框用的
2)添加dropview点击监听事件
private OnNavigationListener mStackListener = new OnNavigationListener() { @Override public boolean onNavigationItemSelected(int itemPosition, long itemId) { if (mIgnoreNextNavigation) { mIgnoreNextNavigation = false; return false; } while (mState.stack.size() > itemPosition + 1) { mState.stackTouched = true; mState.stack.pop(); } onCurrentDirectoryChanged(ANIM_UP); return true; } };
3)添加spinneradapter跟dropview
actionBar.setListNavigationCallbacks(mStackAdapter, mStackListener);
actionBar.setSelectedNavigationItem(mStackAdapter.getCount() - 1);