listview与ExpandableTextView结合导致的高度复用问题

如下图,由于复用的时候view的高度没有被重新计算,item8的高度沿用的是item1的高度,固当ExpandableTextView点击放大显示的时候,高度放大,但是item8并不需要拉伸。

listview与ExpandableTextView结合导致的高度复用问题_第1张图片
Paste_Image.png
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder viewHolder;
    if (convertView == null) {
        convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
        viewHolder = new ViewHolder();
        viewHolder.expandableTextView = (ExpandableTextView) convertView.findViewById(R.id.expand_text_view);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    viewHolder.expandableTextView.setText(item.getName[position]);//解决方案在于该句,需要改为下面的注释行
    //viewHolder.expandableTextView.setText(item.getName[position], mCollapsedStatus, position);//主要原因在于该方法会自动计算高度

    return convertView;
}

主要代码见:
https://github.com/Manabu-GT/ExpandableTextView/blob/master/sample/src/main/java/com/ms/square/android/expandabletextview/sample/SampleTextListAdapter.java

且看下开源控件的setText原理:

  public void setText(@Nullable CharSequence text, @NonNull SparseBooleanArray collapsedStatus, int position) {
    mCollapsedStatus = collapsedStatus;
    mPosition = position;
    boolean isCollapsed = collapsedStatus.get(position, true);
    clearAnimation();
    mCollapsed = isCollapsed;
    mButton.setImageDrawable(mCollapsed ? mExpandDrawable : mCollapseDrawable);
    setText(text);
    getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
    requestLayout();
}

你可能感兴趣的:(listview与ExpandableTextView结合导致的高度复用问题)