HorizontalVariableListView
HorizontalVariableListView copied to clipboard
List was blank when call notifyDatasetChanged in Adapter
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 ?
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
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 your fix works like a charm, you should send a pull request
Confirm, fix is great! @endryha please, create a pull request ;-)