我正在创建一个实现 Android 新导航抽屉布局的应用程序。此布局的文档可以在这里和这里找到。
在设计文档以及 Facebook 和 Google Currents 等许多流行应用程序中,抽屉项目前面都有图标。
设计文档本身在“导航抽屉的内容”下提到了它们
导航目标可以有可选的前导图标和尾随计数器。使用计数器通知用户相应视图中数据状态的更改。
不幸的是,开发者文档没有提及如何在此设置中实现图标。它描述了如何实现项目列表,如下所示:
public class MainActivity extends Activity {
private String[] mPlanetTitles;
private ListView mDrawerList;
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPlanetTitles = getResources().getStringArray(R.array.planets_array);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for the list view
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mPlanetTitles));
// Set the list's click listener
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
...
}
}
因为他们的实现使用 getStringArray(),所以我无法想到修改行项的 xml 以包含图标。
这令人困惑,因为许多应用程序似乎在每个文本项之前都实现了图标,而且设计文档甚至提到了添加图标。我觉得必须有一些简单的 API 调用来完成添加这些图标,但迄今为止我发现的唯一实现似乎复杂得可笑。
有谁知道如何向导航抽屉项目添加图标?有没有办法通过简单的 API 调用来做到这一点,或者让所有这些公司(Facebook、Google 等)必须找到解决该问题的方法?
因为他们的实现使用 getStringArray(),所以我无法想到修改行项的 xml 以包含图标。
ListAdapter
的模型数据选择对于该 ListAdapter
创建的行是否可以具有图标没有任何影响。
有人知道如何向导航抽屉项目添加图标吗?
在
ImageView
的行布局中放置一个 ListAdapter
。通过您的 ImageView
填充该 ListAdapter
,例如通过覆盖您的 getView()
上的 ArrayAdapter
。 IOW,您的做法与其他任何ListView
的做法相同,多年来一直这样做。
有没有办法通过简单的 API 调用来做到这一点
这取决于你对“简单”和“API调用”的定义。
创建一个适配器类,例如..
public class AdapterClass extends ArrayAdapter<String> {
Context context;
private ArrayList<String> TextValue = new ArrayList<String>();
public AdapterClass(Context context, ArrayList<String> TextValue) {
super(context, R.layout.row_of_drawer, TextValue);
this.context = context;
this.TextValue= TextValue;
}
@Override
public View getView(int position, View coverView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.row_of_drawer,
parent, false);
TextView text1 = (TextView)rowView.findViewById(R.id.text1);
text1.setText(TextValue.get(position));
return rowView;
}
}
并对您的 MainActivity 创建一些更改。
使用
AdapterClass adClass = new AdapterClass(MainActivity.this, name_of_arrayList);
mDrawerList.setAdapter(adClass);
代替
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mPlanetTitles));
字符串数组不是行的定义布局。您的
R.layout.drawer_list_item
代表列表中每一行的布局。如果您想包含图像(甚至其他复杂的行布局),您可以将图像视图包含到该自定义布局 xml 中,替换 R.layout.drawer_list_item
xml。
至于如何将图像或文本设置到每个不同的行中,这都是在数组适配器的
getView()
方法中完成的。一个粗略的例子是这样的(假设行中有一个textview和imageview):
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.revise_listview, parent,false);
TextView tv = (TextView)convertView.findViewById(R.id.text);
ImageView iv = (ImageView)convertView.findViewById(R.id.image);
tv1.setText(stringArray.get(i));
if(//condition) {
iv.setImageResource(R.drawable.imagexx);
}
return convertView;
}
实现每行视图的更有效方法是使用 viewholder 模式。您可以找到示例的许多地方之一是这里。
为了真正清晰地构建这个结构,我为面向对象的实现编写了几行额外的代码,该实现从
NavigationItem
数组生成所有内容 - 一个普通的 Java 类,其中包含文本、图标和菜单项片段的字段:
在 MainActivity 中,我的导航只是由数组定义:
NavigationAdapter navigationMenuAdapter;
NavigationItem[] navigationMenuItems = {
new NavigationItem(R.string.menu_home, R.drawable.ic_menu_home, new MainFragment()),
new NavigationItem(R.string.menu_statistics, R.drawable.ic_statistics, new StatisticsFragment()),
new NavigationItem(R.string.menu_settings, R.drawable.ic_settings, new SettingsFragment()),
};
protected void onCreate(Bundle savedInstanceState) {
...
navigationMenuAdapter = new NavigationAdapter(this, navigationMenuItems);
mDrawerList.setAdapter(navigationMenuAdapter);
}
...
private void selectItem(int position) {
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, navigationMenuItems[position].fragment)
.commit();
mDrawerList.setItemChecked(position, true);
setTitle(navigationMenuItems[position].stringTitle);
mDrawerLayout.closeDrawer(mDrawerList);
}
NavigationItem
班:
public class NavigationItem
{
/**
*
* @param stringTitle The id of the string resource of the text of the item.
* @param drawableIcon The id of the drawable resource of icon of the item.
* @param fragment The Fragment to be loaded upon selecting the item.
*/
public NavigationItem(int stringTitle, int drawableIcon, Fragment fragment)
{
this.stringTitle = stringTitle;
this.drawableIcon = drawableIcon;
this.fragment = fragment;
}
/**
* The id of the string resource of the text of the item.
*/
public int stringTitle;
/**
* The id of the drawable resource of icon of the item.
*/
public int drawableIcon;
/**
* The Fragment to be loaded upon selecting the item.
*/
public Fragment fragment;
}
还有导航适配器:
public class NavigationAdapter extends BaseAdapter {
private Context context;
private NavigationItem[] navigationItems;
public NavigationAdapter(Context context, NavigationItem[] items) {
this.context = context;
navigationItems = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.list_item_navigation, parent, false);
} else {
row = convertView;
}
TextView tvTitle = (TextView) row.findViewById(R.id.textView);
ImageView ivIcon = (ImageView) row.findViewById(R.id.imageView);
tvTitle.setText(navigationItems[position].stringTitle);
ivIcon.setImageResource(navigationItems[position].drawableIcon);
return row;
}
}
它使用非常基本的 xml 布局
list_item_navigation
,仅包含一个 ImageView
和一个 TextView
。