gpt4 book ai didi

c# - 绑定(bind)后如何计算MvxListView的高度?

转载 作者:行者123 更新时间:2023-11-30 01:53:54 25 4
gpt4 key购买 nike

我想调整 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。

关于c# - 绑定(bind)后如何计算MvxListView的高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32571282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com