【问题标题】:Non scrollable listview in xamarin.formsxamarin.forms 中的不可滚动列表视图
【发布时间】: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


    【解决方案1】:

    有什么方法可以调整列表视图的高度,因为它是 xamarin.forms 中的内容高度?

    您可以使用HasUnevenRows 属性来实现此功能:

    • HasUnevenRows – 真/假值,如果设置为真,行具有不同的高度。默认为 false。

    一旦HasUnevenRows 设置为true,就不必手动设置行高,因为高度将由Xamarin.Forms 自动计算。

    C#:

    RowHeightDemoListView.HasUnevenRows = true;
    

    XAML:

    <ListView x:Name="RowHeightDemoListView" HasUnevenRows="true" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多