Xamarin.Forms。重绘ListView项目

时间:2018-11-23 20:43:07

标签: listview xamarin.forms xamarin.android listviewitem

我仅在Android上有问题。我在Listview中的项目有时不会更新。我打开一个视图,看到没有文本或空格的项目(屏幕1-2)。我向下滚动,然后向上滚动。重新绘制项目并显示文本或删除空间(屏幕3) 我该如何解决? 下面是图片:

  1. https://i.stack.imgur.com/iv0RC.png

  2. https://i.stack.imgur.com/Kdjs8.png

  3. https://i.stack.imgur.com/FJ4JJ.png

我的列表:

    <Grid BackgroundColor="{Binding ListViewCustomizer.DefaultBackgroundColor}">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <recycle:RecycleListView ItemsSource="{Binding NewsFeedItems}" 
                VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"
                ShowAdvertisementEvery="10"
                ItemSelected="HandleItemSelected" RefreshCommand="{Binding RefreshCommand}"
                RowSpacing="0" ItemMargin="0"
                LoadMoreCommand="{Binding LoadMoreCommand}"
                IsLoadMoreEnabled="True" 
                IsLoadingMore="{Binding IsOldestLoading}"
                IsRefreshing="{Binding IsRefreshing}"
                IsPullToRefreshEnabled="True">
            <recycle:RecycleListView.IsAdvertisementEnabled>
                <OnPlatform x:TypeArguments="x:Boolean">
                  <OnPlatform.iOS>
                    false
                  </OnPlatform.iOS>
                  <OnPlatform.Android>
                    true
                  </OnPlatform.Android>
                </OnPlatform>
            </recycle:RecycleListView.IsAdvertisementEnabled>
            <recycle:RecycleListView.ItemHeight>
                <extensions:OnDeviceType x:TypeArguments="x:Double" Phone="130" Tablet="200"/>
            </recycle:RecycleListView.ItemHeight>
            <recycle:RecycleListView.Padding>
                <OnPlatform x:TypeArguments="Thickness">
                  <OnPlatform.iOS>
                    0, 0, 0, 0
                  </OnPlatform.iOS>
                  <OnPlatform.Android>
                    0, 0, 0, 5
                  </OnPlatform.Android>
                </OnPlatform>
            </recycle:RecycleListView.Padding>
            <recycle:RecycleListView.ItemTemplate>
                <DataTemplate>
                    <viewCells:NewsFeedListItem/>
                </DataTemplate>
            </recycle:RecycleListView.ItemTemplate>
        </recycle:RecycleListView>    </Grid>

RecycleListView是一个渲染器。

渲染器:

    protected RecyclerView _recyclerView;
    protected RecycleListViewAdapter _adapter;
    protected SwipeRefreshLayout _refresh;
    protected RecyclerView.ItemDecoration _itemDecoration;
    protected RecycleScrollListener _scrollListener;

    protected override void OnElementChanged(ElementChangedEventArgs<RecycleListView> e)
    {
        base.OnElementChanged(e);
        if (e.NewElement != null)
        {
            CreateNativeElement();
            SetNativeControl(_refresh);
        }
    }

protected void CreateNativeElement()
    {
        _recyclerView = new RecyclerView(Android.App.Application.Context);
        _recyclerView.HasFixedSize = true;
        _recyclerView.SetLayoutManager(new LinearLayoutManager(Context, OrientationHelper.Vertical, false));
        _recyclerView.SetItemAnimator(new DefaultItemAnimator());
        _recyclerView.HorizontalScrollBarEnabled = false;
        _recyclerView.VerticalScrollBarEnabled = true;
        var pool = _recyclerView.GetRecycledViewPool();
        pool.SetMaxRecycledViews(RecycleListViewAdapter.ADVERTISEMENT_VIEW_TYPE, 1);
        pool.SetMaxRecycledViews(RecycleListViewAdapter.DEFAULT_VIEW_TYPE, 25);

        _scrollListener = new RecycleScrollListener();
        _scrollListener.Loading += (object sender, EventArgs args) => { OnLoadMore(); };

        UpdateIsLoadMoreEnabled();

        _adapter = new RecycleListViewAdapter(
            Context,
            Element.ItemsSource,
            _recyclerView,
            Element,
            Resources.
            DisplayMetrics,
            Element.IsAdvertisementEnabled,
            Element.ShowAdvertisementEvery);

        _recyclerView.SetAdapter(_adapter);

        _itemDecoration = new SpacesItemDecoration(
            ConvertDpToPixels(Element.RowSpacing),
            ConvertDpToPixels(Element.ItemMargin.Left),
            ConvertDpToPixels(Element.ItemMargin.Top),
            ConvertDpToPixels(Element.ItemMargin.Right),
            ConvertDpToPixels(Element.ItemMargin.Bottom)
        );

        _recyclerView.AddItemDecoration(_itemDecoration);

        _refresh = new SwipeRefreshLayout(Android.App.Application.Context);
        _refresh.SetOnRefreshListener(this);
        _refresh.AddView(_recyclerView, LayoutParams.MatchParent);
    }

0 个答案:

没有答案