【问题标题】:listview item text changes on scroll滚动时列表视图项目文本更改
【发布时间】:2019-07-15 07:55:12
【问题描述】:

我有一个列表视图,我在其中绑定了多个数据,例如标签和图像,并且我在我的框架中有该列表,所以当列表大小大于 10 项左右时,滚动我的图像会调整它自身的大小和标签的文本开始隐藏取消隐藏。

这是我的 xaml:

            <ListView 
                               x:Name="list" 
                               SelectionMode="None" 

                               SeparatorVisibility="None"
                               HasUnevenRows="True"
                               IsVisible="False"
                               BackgroundColor="Transparent"
                               ItemTapped="List_ItemTapped"
                               CachingStrategy="RetainElement"
                              >
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <ViewCell>
                                            <Frame Padding="10" Margin="10">

                                                <Grid>
                                                    <Grid.RowDefinitions>
                                                        <RowDefinition 
        Height="Auto" />
                                                        <RowDefinition Height="*" 
        />
                                                    </Grid.RowDefinitions>
                                                    <Grid.ColumnDefinitions>
                                                        <ColumnDefinition 
        Width="Auto" />
                                                        <ColumnDefinition 
        Width="*" />
                                                    </Grid.ColumnDefinitions>
                                                    <Label 
                                                        Grid.Row="0" 
                                                        Grid.Column="0"

                                                        Text="{Binding Note}" 
                                                        HorizontalOptions="Start"  
                                                        TextColor="Black"  
        FontSize="Small"
                                                        FontFamily=" 
       {StaticResource BoldFont}"
                                                        FontAttributes="Bold">
                                                    </Label>
                                                    <ImageButton
                                                         Grid.Row="0"  
                                                         Grid.Column="1"


        HorizontalOptions="EndAndExpand"
                                                        WidthRequest="22"
                                                        HeightRequest="22"
                                                        Padding="6"
                                                        Margin="0,0,0,0"
                                                        Clicked="btndelete"

        AbsoluteLayout.LayoutBounds="0,0,1,1"

        BackgroundColor="Transparent"
                                                        Source="close.png">
                                                    </ImageButton>
                                                    <Label 
                                                            Grid.Row="1"  
                                                            Grid.Column="0"

                                                            Text="{Binding 
        NOfQuestions}"
                                                            FontSize="12"
                                                            FontFamily=" 
       {StaticResource Regular}"
                                                            TextColor="White">

                                                    </Label>
                                                    <Label 
                                                            Grid.Row="1"  
                                                            Grid.Column="0"
                                                            Margin="15,0,0,0"

                                                            Text="{Binding 
        NOfDigits}"
                                                            FontSize="12"
                                                            FontFamily=" 
       {StaticResource Regular}"
                                                            TextColor="White">

                                                    </Label>
                                                </Grid>

                                            </Frame>
        </ViewCell>

                                </DataTemplate>
                            </ListView.ItemTemplate>
                          </ListView>

这是我在此视频中的问题的视频,您可以看到列表看起来不错,但是当我开始滚动它时,文本开始隐藏,取消隐藏它的大小更改交叉图像变小或变大,并删除列表项目所有文字消失

Gif video of my problem please watch this

【问题讨论】:

  • 你想明白了吗?我也有类似的问题。

标签: c# listview xamarin xamarin.forms listviewitem


【解决方案1】:

这种重新渲染列表单元格的行为通常与ListView 缓存策略有关。它定义了单元格的缓存方式,并在加载大量数据时尝试提高性能,但也可以与正确的显示相结合。尝试弄乱CachingStrategy。根据以往的经验,将其设置为“RecycleElement”可以解决渲染问题。

另外,请查看this link 了解有关 ListView 性能的更多信息。

【讨论】:

    【解决方案2】:

    当您在ListView 中有自定义单元格时,建议使用CachingStrategy

    ListView 是用于显示数据的强大视图,但它有一些限制。使用自定义单元格时,滚动性能可能会受到影响,尤其是当它们包含深度嵌套的视图层次结构或使用需要复杂测量的某些布局时。

    Xamarin.Forms 的 XAML 为对应于缓存策略参数的不存在属性提供 XAML 属性:

    <ListView CachingStrategy="RecycleElement" >
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <!-- ... -->
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    

    【讨论】:

      【解决方案3】:

      试试这个

                       <ListView 
                             x:Name="list" 
                             SelectionMode="None" 
                             HorizontalOptions="FillAndExpand"
                             VerticalOptions="FillAndExpand"
                             SeparatorVisibility="None"
                             HasUnevenRows="True"                     
                             BackgroundColor="Transparent"
                             CachingStrategy="RetainElement"
                            >
              <ListView.ItemTemplate>
                  <DataTemplate>
                      <ViewCell>
                          <Frame Padding="10" Margin="10">
      
                              <Grid>
                                  <Grid.RowDefinitions>
                                      <RowDefinition 
                                       Height="Auto" />
                                      <RowDefinition Height="Auto" 
                                              />
                                  </Grid.RowDefinitions>
                                  <StackLayout Grid.Row="0" Orientation="Horizontal" HorizontalOptions="FillAndExpand" >
      
      
                                      <Label                                                         
                                                      Text="{Binding Note}" 
                                                      HorizontalOptions="StartAndExpand"  
                                                      TextColor="Black"  
                                                      FontSize="Small"
                                                      FontFamily=" 
                                                      {StaticResource BoldFont}"
                                                      FontAttributes="Bold">
                                      </Label>
                                  <ImageButton
                                                
                                                       HorizontalOptions="EndAndExpand"
                                                       WidthRequest="22"
                                                       HeightRequest="22"
                                                       Padding="6"
                                                       Margin="0,0,0,0"
                                                       Clicked="btndelete"  
                                                       AbsoluteLayout.LayoutBounds="0,0,1,1"    
                                                       BackgroundColor="Transparent"
                                                      Source="close.png">
                                  </ImageButton>
      
      
      
      
                                  </StackLayout>
                                  
       
      
                                  <StackLayout Grid.Row="1" Orientation="Horizontal" HorizontalOptions="FillAndExpand">
                                      <Label 
                                                          
                                                          Text="{Binding 
                                                          NOfQuestions}"
                                                          HorizontalOptions="StartAndExpand" 
                                                          FontSize="12"
                                                          FontFamily=" 
                                                          {StaticResource Regular}"
                                                          TextColor="White">
      
                                      </Label>
                                      <Label 
                                                        
                                                          Margin="15,0,0,0"    
                                                          Text="{Binding 
                                                          NOfDigits}"
                                                          HorizontalOptions="CenterAndExpand"
                                                          FontSize="12"
                                                          FontFamily=" 
                                                         {StaticResource Regular}"
                                                          TextColor="White">
      
                                      </Label>
                                  </StackLayout>
                      
                              </Grid>
      
                          </Frame>
                      </ViewCell>
      
                  </DataTemplate>
              </ListView.ItemTemplate>
          </ListView>
      

      【讨论】:

      • 还是同样的问题
      • 还是同样的问题
      • 你能把 rowdefentions 改成一些固定值吗?
      • 如果我们创建一个带有一个标签的简单列表,同样的问题也会再次出现在简单列表中
      • 你是在listview之前加scrollview吗?
      猜你喜欢
      • 2016-06-07
      • 1970-01-01
      • 2015-05-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多