有什么方法可以调整listview的高度,因为它在xamarin.forms中的内容高度?我可以成功地为ios做这个但是对于android,我应用了一个导致布局呈现缓慢的解决方案。
code
public class CustomListViewRenderer : ListViewRenderer
{
...
protected async override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
if(Element == null || ((CustomListView)Element).IsScrollable)
{
return;
}
var view = (CustomListView)Element;
if(!view.IsScrollable)
{
var mAdapter = nativeList.Adapter;
int totalHeight = 0;
int listWidth = nativeList.MeasuredWidth;
int listHeight = nativeList.MeasuredHeight;
if(totalCount == nativeList.Count)
{
//return;
}
for (int i = 0; i < mAdapter.Count; i++)
{
global::Android.Views.View mView = mAdapter.GetView(i, null, nativeList);
mView.Measure(MeasureSpec.MakeMeasureSpec(listWidth, MeasureSpecMode.Exactly),
MeasureSpec.MakeMeasureSpec(0, MeasureSpecMode.Unspecified));
totalHeight += (int)(mView.MeasuredHeight / Resources.DisplayMetrics.Density);
totalCount = i + 1;
}
ViewGroup.LayoutParams param = nativeList.LayoutParameters;
param.Height = totalHeight
+ (nativeList.DividerHeight * (mAdapter.Count - 1));
view.HeightRequest = param.Height;
}
}
}
然而,这并不总是为listview生成精确的高度,有时会在底部留下空间。此外,它在布局使用列表视图的页面时会造成很大的延迟。
有人可以帮我解决这个问题吗?
答案 0 :(得分:0)
有什么方法可以调整listview的高度,因为它在xamarin.forms中的内容高度?
您可以使用HasUnevenRows属性来实现此功能:
一旦HasUnevenRows
设置为true,就不必手动设置行高,因为高度将由Xamarin.Forms
自动计算。
C#:
RowHeightDemoListView.HasUnevenRows = true;
XAML:
<ListView x:Name="RowHeightDemoListView" HasUnevenRows="true" />