动作条列表导航宽度 - 包装内容 [英] Actionbar list navigation width - wrap content
问题描述
在默认情况下在所选项目的动作条列表导航宽度一样宽,最宽的项目。我想在选定的项目为WRAP_CONTENT,因为它是在谷歌+,Gmail的,地图Android应用程序。
By default in actionbar list navigation width of selected item is as wide as widest item. I would like to "wrap_content" in selected item as it's in google+,gmail,maps android apps.
是否有人知道热办呢?
我试图重写导航适配器的getView,但它不工作。看来,这种观点是包裹着另一种观点认为它具有宽最宽的项目。我也试过actionbar.setCustom视图微调,但微调的行为是一样的。它采用最宽的项目宽度。
I tried to override getView of navigation adapter but it doesn't work. It looks that this view is wrapped with another view which has width of widest item. I also tried actionbar.setCustom view with spinner but spinner behaviour is same. It uses widest item width.
感谢所有的建议,链接和帮助。
Thanks for all suggestions, links and help.
推荐答案
我遇到了同样的问题。看来Android将调用getView()方法的所有项目,最后设置的宽度最宽的项目的宽度。
I've met the same problem. It seems Android will call getView() method for all the items, and finally set the width to the widest item's width.
因此,这里是我的解决方案:
So here's my solution:
-
存储当前选定的部分(在你的例子,例如部分),说selectedSection,在code。其实你需要做的是在NavigationListener的onNavigationItemSelected()方法。
Store the current selected section(e.g. section in your example), say selectedSection, in your code. Actually you have to do it in the onNavigationItemSelected() method of NavigationListener.
覆盖您SpinnerAdapter的getView()方法,始终将它的内容selectedSection。
Override the getView() method of your SpinnerAdapter, always set it's content to selectedSection.
在NavigationListener的onNavigationItemSelected()方法,尝试之后,您将当前模式设置为selectedSection打电话给你spinnerAdapter的notifyDataSetChanged()方法。
In the onNavigationItemSelected() method of NavigationListener, try to call the notifyDataSetChanged() method of your spinnerAdapter after you set the current mode to selectedSection.
下面的示例code:
final int selectedSection = 0;
final String sectionLabels[] = {"short sec", "long section", "looooonger section"};
final ArrayAdapter<String> arrayAdapter =
new arrayAdapter<String>(this, simple_list_item_1) {
@Override
public int getCount() {
// You need to implement here.
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.you_entry, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.you_text);
// Set the text to the current section.
textView.setText(sectionLabels[selectedSection]);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
// You need to implement here.
}
}
actionBar.setListNavigationCallbacks(navigationAdpater,
new ActionBar.OnNavigationListener() {
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
// Store the current section.
selectedSection = itemPosition;
navigationAdpater.notifyDataSetChanged();
return true;
}
});
这篇关于动作条列表导航宽度 - 包装内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!