导航抽屉列表视图有2种类型

时间:2015-02-28 21:34:15

标签: android android-listview navigation-drawer listviewitem

我有一个导航抽屉,带有ListView和标题。当我将抽屉向下舀起时,物品是混合的,物品的类型。为什么?

我的DrawerAdapter.java:

公共类DrawerAdapter扩展了BaseAdapter {

private Context context;
private List<DrawerItem> navDrawerItems;
DrawerItemClickListener drawerItemClickListener;


public DrawerAdapter(Context context, ArrayList<DrawerItem> navDrawerItems,DrawerItemClickListener drawerItemClickListener) {
    this.context = context;
    this.navDrawerItems = navDrawerItems;
    this.drawerItemClickListener = drawerItemClickListener;
}

@Override
public int getCount() {
    return navDrawerItems.size();
}

@Override
public Object getItem(int position) {
    return navDrawerItems.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public boolean isEnabled(int position) {
    return false;
}


static class ViewHolder {
ImageView imvIcon;
TextView textView;
LinearLayout layout;
}


@Override
public int getViewTypeCount() {
    return 2;
}

@Override
public int getItemViewType(int position) {
    return super.getItemViewType(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;
    final DrawerItem mItem = navDrawerItems.get(position);

    if(mItem.getType().equals(DrawerItem.TYPE_CLICKABLE))
    {
        if (convertView == null)
        {
            holder = new ViewHolder();

            LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);

            convertView = inflater.inflate(R.layout.drawer_list_item, parent, false);
            holder.imvIcon = (ImageView) convertView.findViewById(R.id.item_icon);
            holder.textView = (TextView) convertView.findViewById(R.id.item_text);
            holder.layout = (LinearLayout) convertView.findViewById(R.id.drawer_one_layout);

            convertView.setTag(holder);

        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }

        if(holder.imvIcon != null)
            Picasso.with(parent.getContext()).load(mItem.getIcon()).into(holder.imvIcon);
        if(holder.textView != null)
            holder.textView.setText(mItem.getTitle());

        if(holder.layout != null)
            holder.layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    drawerItemClickListener.DrawerItemClicked(mItem);
                }
            });

    }
    else
    {

        if (convertView == null)
        {
            holder = new ViewHolder();

            LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);

                convertView = inflater.inflate(R.layout.drawer_list_item_two, parent, false);
                holder.textView = (TextView) convertView.findViewById(R.id.item_text);

            convertView.setTag(holder);

        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }

            holder.textView.setText(mItem.getTitle());

    }

    return convertView;
}}

抽屉物品:

公共类DrawerItem {

public final static String TYPE_CLICKABLE = "clikc";

public final static String TYPE_HEADER = "header";

private String title, type;
private int icon;

public DrawerItem(String title, int icon, String type) {
    this.title = title;
    this.icon = icon;
    this.type = type;

}

public String getTitle() {
    return title;
}

public int getIcon() {
    return icon;
}

public String getType() {return this.type;}}

请帮帮我。感谢。

1 个答案:

答案 0 :(得分:1)

您没有明确说明当前视图的项目类型,因此适配器不知道视图类型并且没有正确显示项目(例如,它们混合了它们)。

在你的getItemViewType()中,您应该清楚当前项目的viewtype,并执行以下操作:

@Override
public int getItemViewType(int position) {
    if(navDrawerItems.get(position).getType().equals(DrawerItem.TYPE_CLICKABLE))
        return 0;
    return 1;
}