我正在尝试将自定义适配器用于导航抽屉,但我不知道如何添加它以便它可以使用它。
任何指导都将不胜感激,谢谢。
这是我的自定义适配器:
private class SampleItem {
public String tag;
public int iconRes;
public SampleItem(String tag, int iconRes) {
this.tag = tag;
this.iconRes = iconRes;
}
}
public class SampleAdapter extends ArrayAdapter<SampleItem> {
public SampleAdapter(Context context) {
super(context, 0);
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.row, null);
}
ImageView icon = (ImageView) convertView
.findViewById(R.id.ImageNavDrawer);
icon.setImageResource(getItem(position).iconRes);
TextView title = (TextView) convertView
.findViewById(R.id.NavListTextview);
title.setText(getItem(position).tag);
return convertView;
}
}
如何设置抽屉:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mDrawerListView = (ListView) inflater.inflate(
R.layout.fragment_navigation_drawer, container, false);
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
});
mDrawerListView.setAdapter(new ArrayAdapter<String>(
getActionBar().getThemedContext(),
R.layout.row,
R.id.NavListTextview,
new String[]{
getString(R.string.1),
getString(R.string.2),
getString(R.string.3),
}));
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
return mDrawerListView;
}
如何在其他项目中使用适配器:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.list, null);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
SampleAdapter adapter = new SampleAdapter(getActivity());
{
adapter.add(new SampleItem("Archietecture",
R.drawable.archietecture));
adapter.add(new SampleItem("Art", R.drawable.art));
adapter.add(new SampleItem("Cinema", R.drawable.cinema));
adapter.add(new SampleItem("Discoveries", R.drawable.discoveries));
adapter.add(new SampleItem("Events", R.drawable.events));
adapter.add(new SampleItem("Literature", R.drawable.literature));
adapter.add(new SampleItem("Music", R.drawable.music));
adapter.add(new SampleItem("Nature", R.drawable.nature));
adapter.add(new SampleItem("Politics", R.drawable.politics));
adapter.add(new SampleItem("Science", R.drawable.science));
adapter.add(new SampleItem("Sports", R.drawable.sports));
adapter.add(new SampleItem("Technology", R.drawable.technology));
}
setListAdapter(adapter);
}
private class SampleItem {
public String tag;
public int iconRes;
public SampleItem(String tag, int iconRes) {
this.tag = tag;
this.iconRes = iconRes;
}
}
public class SampleAdapter extends ArrayAdapter<SampleItem> {
public SampleAdapter(Context context) {
super(context, 0);
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.row, null);
}
ImageView icon = (ImageView) convertView
.findViewById(R.id.row_icon);
icon.setImageResource(getItem(position).iconRes);
TextView title = (TextView) convertView
.findViewById(R.id.row_title);
title.setText(getItem(position).tag);
return convertView;
}
}
答案 0 :(得分:2)
如果您想使用自定义适配器,则必须将其设置为ListView
,而不是使用普通ArrayAdapter
。所以不要这样:
mDrawerListView.setAdapter(new ArrayAdapter<String>(getActionBar().getThemedContext(), R.layout.row, R.id.NavListTextview, new String[]{getString(R.string.1), getString(R.string.2), getString(R.string.3)}));
这样做:
mDrawerListView.setAdapter(new SampleAdapter(context));
但您的自定义SampleAdapter
似乎尚未完成。好像你需要延长一点点。