简体   繁体   中英

Android - ListView with different heights in each row / getView()

I want to build a dynamic ListView (like a chat list with different layouts/bubbles). My problem is, that each row has an individual height. My code below works, but every time I scroll down or receive a new message, the row with a different height gets heigher.

private class dialogAdapter extends BaseAdapter {
private LayoutInflater mInflater;

public dialogAdapter(Context context) {
    mInflater = LayoutInflater.from(context);
}

@Override
public boolean hasStableIds() {
    return true;
}

public int getCount() {
    return dialog.size();
}

public int getViewTypeCount() {
    return 999999;
}

public Object getItem(int position) {
    return dialog.get(position);
}

public int getItemViewType(int position) {
    return position;
}

public String getType(int position) {
    return dialogType.get(position);
}

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

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    Log.w("DRAGII", "POS: "+position);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.bubble, null);
        holder = new ViewHolder();
        holder.text = (TextView) convertView.findViewById(R.id.text);
        holder.parser = new URLImageParser(holder.text);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    if (position <= dialogCache.size())
        dialogCache.add(position, Html.fromHtml((String)getItem(position),
                holder.parser, null));
    holder.text.setText(dialogCache.get(position));
    holder.type = getType(position);
    int bubble = R.drawable.bubble;
    if (holder.type.equals("R")) bubble = R.drawable.bubble_right;
    else if (holder.type.equals("L")) bubble = R.drawable.bubble_left;
    holder.text.setBackgroundResource(bubble);

    return convertView;
}

class ViewHolder {
    TextView text;
    String type = "B";
    URLImageParser parser;
}
}

What should I do?

通过使用TableLayout而不是ListView解决了此问题。

if u are adding tableRow programmatically to tableLayout, you will have performance issues. Think it again and find a way by using listView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM