我想调整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可以包含一到十行文本。
我想这就是问题所在-它根据布局而不是实际的行高来计算高度。我对吗?我该如何解决?
最佳答案
使用MvxLinearLayout而不是MvxListView。