我会自定义导航抽屉。我使用了一个在另一个线程中的 stackoverflow 上找到的示例。该代码是复制和粘贴的,但是在 logcat 中运行应用程序时会出现异常:“ResourceNotFoundException”。下面贴一段logcat
08-02 20:06:26.367: E/AndroidRuntime(6084): android.content.res.Resources$NotFoundException: String resource ID #0x0
08-02 20:06:26.367: E/AndroidRuntime(6084): at android.content.res.Resources.getText(Resources.java:239)
08-02 20:06:26.367: E/AndroidRuntime(6084): at android.widget.TextView.setText(TextView.java:3837)
08-02 20:06:26.367: E/AndroidRuntime(6084): at it.ustation.DrawerAdapter.getView(DrawerAdapter.java:81)
08-02 20:06:26.367: E/AndroidRuntime(6084): at android.widget.AbsListView.obtainView(AbsListView.java:2177)
....
我的适配器的代码是
public class DrawerAdapter extends ArrayAdapter<ListNavigationItemModel>{
public DrawerAdapter(Context context) {
super(context, 0);
}
public void addHeader(int title) {
add(new ListNavigationItemModel(title, -1, true));
}
public void addItem(int title, int icon) {
add(new ListNavigationItemModel(title, icon, false));
}
public void addItem(ListNavigationItemModel itemModel) {
add(itemModel);
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
return getItem(position).isHeader ? 0 : 1;
}
@Override
public boolean isEnabled(int position) {
return !getItem(position).isHeader;
}
public static class ViewHolder {
public final TextView textHolder;
public final ImageView imageHolder;
public ViewHolder(TextView text1, ImageView image1) {
this.textHolder = text1;
this.imageHolder = image1;
}
}
public View getView(int position, View convertView, ViewGroup parent) {
ListNavigationItemModel item = getItem(position);
ViewHolder holder = null;
View view = convertView;
if (view == null) {
int layout = R.layout.list_item_drawer;
if (item.isHeader)
layout = R.layout.ns_menu_row_header;
view = LayoutInflater.from(getContext()).inflate(layout, null);
TextView text1 = (TextView) view.findViewById(R.id.nd_title);
ImageView image1 = (ImageView) view.findViewById(R.id.nd_thumb_image);
view.setTag(new ViewHolder(text1, image1));
}
if (holder == null && view != null) {
Object tag = view.getTag();
if (tag instanceof ViewHolder) {
holder = (ViewHolder) tag;
}
}
if (item != null && holder != null) {
if (holder.textHolder != null)
holder.textHolder.setText(item.title);
if (holder.imageHolder != null) {
if (item.iconRes > 0) {
holder.imageHolder.setVisibility(View.VISIBLE);
holder.imageHolder.setImageResource(item.iconRes);
} else {
holder.imageHolder.setVisibility(View.GONE);
}
}
}
return view;
}
}
模型是
public class ListNavigationItemModel {
public int title;
public int iconRes;
public boolean isHeader;
public ListNavigationItemModel(int title, int iconRes, boolean header) {
this.title = title;
this.iconRes = iconRes;
this.isHeader = header;
}
public ListNavigationItemModel(int title, int iconRes) {
this(title, iconRes, false);
}
}
和xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp" >
<ImageView
android:id="@+id/nd_thumb_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:contentDescription="@string/thumbnail_desc"
android:scaleType="fitCenter"
android:src="@drawable/ic_save" />
<TextView
android:id="@+id/nd_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/nd_thumb_image"
android:text="@string/c_desc"
android:textColor="#fff"
android:textSize="16sp" />
这是在适配器上添加每个项目的代码。mPlanetTitles 是一个数组字符串。
for (String item : mPlanetTitles) {
int id_menu_one = getResources().getIdentifier(item, "string", this.getPackageName());
int id_menu_one_icons = getResources().getIdentifier(menuOneIcons[oneIcons], "drawable", this.getPackageName());
ListNavigationItemModel mItem = new ListNavigationItemModel(id_menu_one, id_menu_one_icons);
mAdapter.addItem(mItem);
oneIcons++;
}