HorizontalVariableListView icon indicating copy to clipboard operation
HorizontalVariableListView copied to clipboard

List was blank when call notifyDatasetChanged in Adapter

Open ducn opened this issue 12 years ago • 4 comments

Hi, When list item changed in adapter, I called notifyDatasetChanged to ask the list redraw, but it's empty then, is it a bug ? or I have to use another method ?

ducn avatar Dec 27 '12 03:12 ducn

I faced the same issue, I would say even more, sometimes demo app also doesn't display horizontal list after start, but next time list appears. And again after notifyDatasetChanged list is empty too

EDIT: Probably I've found potential problem, it looks like omMeasure doesn't work fine

After notifyDatasetChanged value of mChildHeight becomes 0 and after remeasure final height is 0, this is why list is invisible, if I hard code height to some value (ignoring 0) then it works fine, still continue to investigate this issue but any feedback is appriciated

endryha avatar Feb 21 '13 15:02 endryha

Currently I solved it by rewriting onMeasure method to allow it to calculate child height on the fly while measurement:

@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int parentWidth = MeasureSpec.getSize(widthMeasureSpec); int parentHeight = MeasureSpec.getSize(heightMeasureSpec); if (mChildHeight == 0) { if (mAdapter != null && mAdapter.getCount() > 0) { int viewType = mAdapter.getItemViewType(0); View child = mAdapter.getView(0, mRecycleBin.get(viewType).poll(), this);

        LayoutParams params = child.getLayoutParams();

        if (params == null) {
           params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        }

        int childHeightSpec = ViewGroup.getChildMeasureSpec(mHeightMeasureSpec, getPaddingTop() + getPaddingBottom(), params.height);
        int childWidthSpec = ViewGroup.getChildMeasureSpec(mWidthMeasureSpec, getPaddingLeft() + getPaddingRight(), params.width);
        child.measure(childWidthSpec, childHeightSpec);

        mChildHeight = child.getMeasuredHeight();

        setMeasuredDimension(parentWidth, mChildHeight);
     }
     else {
        setMeasuredDimension(parentWidth, parentHeight);
     }
  }
  else {
     setMeasuredDimension(parentWidth, mChildHeight);
  }

  mHeightMeasureSpec = heightMeasureSpec;
  mWidthMeasureSpec = widthMeasureSpec;

}

endryha avatar Feb 21 '13 17:02 endryha

@endryha your fix works like a charm, you should send a pull request

0xjams avatar Feb 28 '13 17:02 0xjams

Confirm, fix is great! @endryha please, create a pull request ;-)

almozavr avatar Mar 14 '13 17:03 almozavr