在getView
方法中,当视图类型为vTypeItem
时,它应返回我的数组列表中的所有项,但它返回除第一个之外的所有项,因此显示ListView
除第一个之外的所有项目
public class DrawerAdapter extends ArrayAdapter<DrawerCustomList> {
private static final int vTypeHeader = 0;
private static final int vTypeItem = 1;
ArrayList<DrawerCustomList> mList = new ArrayList<>();
public DrawerAdapter(Context context, ArrayList<DrawerCustomList> list) {
super(context,0,list);
mList = list;
}
@Override
public int getItemViewType(int position) {
return (position==0)?vTypeHeader:vTypeItem;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getCount() {
return mList.size();
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
View v = convertView;
MyViewHolder mViewHolder = null;
if (v==null){
if(type==vTypeItem){
v = LayoutInflater.from(getContext()).inflate(R.layout.drawer_single_item,parent,false);
mViewHolder= new MyViewHolder(v,type);
DrawerCustomList val = mList.get(position);
mViewHolder.itemImage.setImageResource(val.getmListImage());
mViewHolder.itemName.setText(val.getmListName());
}else {
v = LayoutInflater.from(getContext()).inflate(R.layout.drawer_header_layout,parent,false);
mViewHolder= new MyViewHolder(v,type);
}
}
v.setTag(mViewHolder);
return v;
}
public static class MyViewHolder{
TextView itemName,headerName,headerEmail;
CircleImageView headerPicture;
ImageView itemImage;
public MyViewHolder(View v,int type){
if(type==vTypeHeader){
headerName = (TextView)v.findViewById(R.id.drawerHeaderName);
headerEmail = (TextView)v.findViewById(R.id.drawerHeaderEmail);
headerPicture = (CircleImageView)v.findViewById(R.id.drawerCircleImageView);
}else {
itemName = (TextView) v.findViewById(R.id.drawerItemName);
itemImage = (ImageView)v.findViewById(R.id.drawerItemImage);
}
}
}
}
答案 0 :(得分:0)
好像您没有考虑header
方法中的getCount()
视图。
首先将后者改为:
@Override
public int getCount() {
return mList.size() + 1; // accounting for the header
}
现在请记住,在getView()
方法的当前版本中,如果position == 0
您将显示header
,这是正确的。但是如果position == 1
,你会做
....
if(type==vTypeItem){
v = LayoutInflater.from(getContext()).inflate(R.layout.drawer_single_item,parent,false);
mViewHolder= new MyViewHolder(v,type);
DrawerCustomList val = mList.get(position);
注意mList.get(position)
如何实际获取列表中的第二项(索引为1),而不是第一项,这就是您所需要的。因此,将此行更改为...mList.get(position - 1)
,您的问题应该得到解决。
答案 1 :(得分:0)
我如下更改了您的代码,似乎工作正常。在这里,我在drawer_header_layout中添加了drawer_single_item布局的抽屉项名称,drawerItemImage控件。 在getView方法中进行以下更改。
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
View v = convertView;
MyViewHolder mViewHolder = null;
if(type==vTypeItem){
v = LayoutInflater.from(getContext()).inflate(R.layout.drawer_single_item,parent,false);
mViewHolder= new MyViewHolder(v,type);
DrawerCustomList val = mList.get(position);
mViewHolder.itemImage.setImageResource(val.getmListImage());
mViewHolder.itemName.setText(val.getmListName());
}else {
v = LayoutInflater.from(getContext()).inflate(R.layout.drawer_header_layout,parent,false);
mViewHolder= new MyViewHolder(v,type);
DrawerCustomList val = mList.get(position);
mViewHolder.itemImage.setImageResource(val.getmListImage());
mViewHolder.itemName.setText(val.getmListName());
}
v.setTag(mViewHolder);
return v;
}
将MyViewHolder更改为此:
public static class MyViewHolder{
TextView itemName,headerName,headerEmail;
CircleImageView headerPicture;
ImageView itemImage;
public MyViewHolder(View v,int type){
if(type==vTypeHeader){
headerName = (TextView)v.findViewById(R.id.drawerHeaderName);
headerEmail = (TextView)v.findViewById(R.id.drawerHeaderEmail);
headerPicture = (CircleImageView)v.findViewById(R.id.drawerCircleImageView);
itemName = (TextView) v.findViewById(R.id.drawerItemName);
itemImage = (ImageView)v.findViewById(R.id.drawerItemImage);
}else {
itemName = (TextView) v.findViewById(R.id.drawerItemName);
itemImage = (ImageView)v.findViewById(R.id.drawerItemImage);
}
}
}
希望这会有所帮助。