emilsjolander / StickyListHeaders

An android library for section headers that stick to the top
Apache License 2.0
5.51k stars 1.52k forks source link

ExpandableStickyListHeadersListView has only one child #484

Closed eltgm closed 6 years ago

eltgm commented 6 years ago

public class StickyListAdapter extends BaseAdapter implements StickyListHeadersAdapter{

private List<String> childs = new ArrayList<>();
private List<ExpandedMenuModel> head = new ArrayList<>();
private LayoutInflater inflater;
private Context context;

public StickyListAdapter(Context context) {
    inflater = LayoutInflater.from(context);
    this.context = context;
}

public void setData(List<ExpandedMenuModel> head, List<String> childs){
    this.head = head;
    this.childs = childs;
    notifyDataSetChanged();
}

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

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

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

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

    if (convertView == null) {
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.menu_artist, parent, false);
        holder.text = convertView.findViewById(R.id.submenu);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.text.setText(childs.get(position));

    return convertView;
}

@Override
public View getHeaderView(int position, View convertView, ViewGroup parent) {
    HeaderViewHolder holder;
    if (convertView == null) {
        holder = new HeaderViewHolder();
        convertView = inflater.inflate(R.layout.menu_artist_header, parent, false);
        holder.text = convertView.findViewById(R.id.submenu);
        holder.imageView = convertView.findViewById(R.id.iconimage);
        convertView.setTag(holder);
    } else {
        holder = (HeaderViewHolder) convertView.getTag();
    }
    //set header text as first char in name

    holder.text.setText(head.get(position).getIconName());
    holder.imageView.setImageDrawable(context.getDrawable(head.get(position).getIconImg()));
    return convertView;
}

@Override
public long getHeaderId(int position) {
    //return the first character of the country as ID because this is what headers are based upon
    return head.get(position).getIconName().subSequence(0, 1).charAt(0);
}

class HeaderViewHolder {
    TextView text;
    ImageView imageView;
}

class ViewHolder {
    TextView text;
}

} ... expandableList = findViewById(R.id.navigationmenu); mMenuAdapter = new StickyListAdapter(this);

    // setting list adapter
    expandableList.setAdapter(mMenuAdapter);

... mMenuAdapter.setData(Arrays.asList(item1), heading1);

returns only one child(but heading1 has 7 elements.

Scienticious commented 5 years ago

same issue, my list has 4 child on 1st item, but only return 1 child. How to fix this issue?