我想调整MvxListView的大小,使其显示ScrollView中的所有元素 (我实际上想要使用Header和CustomAdapter的MvxListView,但这太难实现了。)
我尝试了几种方法,使用OnMeasure
protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// Calculate entire height by providing a very large height hint.
// View.MeasuredSizeMask represents the largest height possible.
var expandSpec = MeasureSpec.MakeMeasureSpec(MeasuredSizeMask, MeasureSpecMode.AtMost);
base.OnMeasure(widthMeasureSpec, expandSpec);
SetMeasuredDimension(MeasuredWidth, (int)2 * MeasuredHeight);
}
使用OnDraw:
protected override void OnDraw(Canvas canvas)
{
if (Count != _oldCount)
{
_oldCount = Count;
_params = LayoutParameters;
_params.Height = CalculateHeight();
LayoutParameters = _params;
}
base.OnDraw(canvas);
}
其中CalculateHeight为:
private int CalculateHeight()
{
var height = 0;
for (var i = 0; i < ChildCount; i++)
{
height += GetChildAt(i).MeasuredHeight;
height += DividerHeight;
}
return height;
}
甚至:
private int CalculateHeight()
{
var mAdapter = Adapter;
int listviewElementsheight = 0;
for (int i = 0; i < Adapter.Count; i++)
{
var mView = mAdapter.GetView(i, null, this);
mView.Measure(MeasureSpec.MakeMeasureSpec(0, MeasureSpecMode.Unspecified),
MeasureSpec.MakeMeasureSpec(0, MeasureSpecMode.Unspecified));
listviewElementsheight += mView.MeasuredHeight;
listviewElementsheight += DividerHeight;
}
return listviewElementsheight;
}
但我注意到元素的高度都是一样的。元素有这种布局
<TextView
android:text="Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/List.Secondary"
local:MvxBind="Text Title; TextColor FineInfoColor(IsHighlighted)" />
<TextView
android:text="Content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/List.Primary"
local:MvxBind="Text Content" />
和第二个TextView可以有一到十行文本。 我想这是问题 - 它根据布局计算高度而不是实际的行高。我对吗?我该如何解决?
答案 0 :(得分:1)
使用MvxLinearLayout而不是MvxListView。