【发布时间】:2018-04-08 20:43:51
【问题描述】:
有什么方法可以调整列表视图的高度,因为它是 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;
}
}
}
然而,这并不总是为列表视图生成精确的高度,有时会在底部留下空间。此外,它会在布局使用列表视图的页面时产生很大的延迟。
谁能帮我解决这个问题?
【问题讨论】:
标签: android listview xamarin.forms xamarin.android custom-renderer