ListView渲染器视图单元格文本在分组时向右移动

时间:2018-01-29 13:45:43

标签: xamarin.forms xamarin.ios

我在listview中使用了渲染器,因为groupheader过去常常卡在滚动上。使用渲染后,滚动问题得到修复,但

  • Viewcell文本在Load上右侧对齐。
  • 滚动时,未显示对齐问题。

如何以正确的方式对齐视单元文本(向左,滚动时如何显示)

这是我在渲染器中的代码

public class CustomListviewEventDetailsRenderer : ListViewRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
        {
            base.OnElementChanged(e);
             if (this.Control != null && e.NewElement != null)
             {

                var frame = CoreGraphics.CGRect.Empty;
                var tbl = new UITableView(frame, UITableViewStyle.Grouped)
                {
                    Source = this.Control.Source
                };
                this.SetNativeControl(tbl);

            }
        }
    }

XAML:

              <local1:CustomListviewEventDetails x:Name="lstItems" HasUnevenRows="True" SeparatorVisibility="None" BackgroundColor="#EEEEEE" ItemsSource="{Binding lstViewItemSource}" ItemSelected="lstItems_ItemSelected" VerticalOptions="FillAndExpand"  HeightRequest="{Binding LtHeight}"
                                                           IsGroupingEnabled="True" >

     <local1:CustomListviewEventDetails.GroupHeaderTemplate>
                                            <DataTemplate>
                                                <local1:CustomViewCellFS Height="30" >
                                                    <StackLayout Padding="20,0,0,0" VerticalOptions="CenterAndExpand" BackgroundColor="Green">
                                                        <Label Text="{Binding SectionName}" FontSize="15" FontAttributes="Bold" TextColor="Gray" VerticalOptions="CenterAndExpand" VerticalTextAlignment="Center"  />
                                                    </StackLayout>
                                                </local1:CustomViewCellFS>
                                            </DataTemplate>
                                        </local1:CustomListviewEventDetails.GroupHeaderTemplate>
 <local1:CustomListviewEventDetails.ItemTemplate>
                                        <DataTemplate>
                                            <ViewCell>
                                                <ViewCell.View>
                                                    <StackLayout Padding="0,0,0,0" Margin="0,0,0,0">
                                                        <Grid RowSpacing="0">
                                                            <Grid.RowDefinitions>
                                                                <RowDefinition Height="Auto"></RowDefinition>
                                                                <RowDefinition Height="Auto"></RowDefinition>
                                                                <RowDefinition Height="Auto"></RowDefinition>
                                                                <RowDefinition Height="5"></RowDefinition>
                                                            </Grid.RowDefinitions>
                                                            <Grid.ColumnDefinitions>
                                                                <ColumnDefinition Width="0.1*"></ColumnDefinition>
                                                                <ColumnDefinition Width="0.9*"></ColumnDefinition>
                                                            </Grid.ColumnDefinitions>

                                                            <Label Grid.Row="0" Grid.Column="0" Text="{Binding PackageQuantity}" FontSize="15" TextColor="Black" HorizontalOptions="Start" IsVisible="{Binding PackageNameVisibility}" FontAttributes="Bold" >
                                                            </Label>
                                                            <Label Grid.Row="0" Grid.Column ="1" Text="{Binding PackageName}" FontSize="15" TextColor="Black" HorizontalOptions="Start" HorizontalTextAlignment="Start" IsVisible="{Binding PackageNameVisibility}" FontAttributes="Bold" ></Label>
                                                            <Label Grid.Row="1" Grid.Column="0" Text="{Binding Quantity}" FontSize="15" TextColor="Black" HorizontalOptions="Start" HorizontalTextAlignment="Start" IsVisible="{Binding PackageNameVisibility}" >
                                                            </Label>
                                                            <Label  Grid.Row="1" Grid.Column="1" Text="{Binding Item}" FontSize="15" TextColor="Black" HorizontalOptions="StartAndExpand" HorizontalTextAlignment="Start" IsVisible="{Binding PackageNameVisibility}">
                                                            </Label>
                                                            <Label Grid.Row="2" Grid.Column="0" Text="{Binding SectionQuantity}" FontSize="15" TextColor="Black" HorizontalOptions="Start" HorizontalTextAlignment="Start" IsVisible="{Binding SectionItemVisibility}" >
                                                            </Label>
                                                            <Label  Grid.Row="2" Grid.Column="1" Text="{Binding SectionItem}" FontSize="15" TextColor="Black" HorizontalOptions="StartAndExpand" HorizontalTextAlignment="Start" IsVisible="{Binding SectionItemVisibility}" >
                                                            </Label>
                                                            <BoxView x:Name="txt" Grid.Row="3" Grid.ColumnSpan="2" HeightRequest="5" ></BoxView>

                                                        </Grid>
                                                    </StackLayout>
                                                </ViewCell.View>
                                            </ViewCell>
                                        </DataTemplate>
                                    </local1:CustomListviewEventDetails.ItemTemplate>
      </local1:CustomListviewEventDetails>

2 个答案:

答案 0 :(得分:0)

在我的测试中,如果您不使用渲染器,ListView可以将其单元格放在正确的位置。此外,您似乎只是创建一个新的UITableView,但与您在表单中定义的相同,为什么要这样做?

如果确实需要创建此渲染器。我想你可以尝试重新加载新的UITableView,如:

          "Some words .testword"
should be "Some words ."
I have    "Some words"    (there is no dot at the end).

这可能不是一个完美的解决方案,但它可以解决您的问题。

答案 1 :(得分:0)

感谢大家的帮助,我能够根据此处提供的解决方案解决问题link

如果其他人遇到此问题,这是我的工作代码:

 protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
        {
            base.OnElementChanged(e);
             if (this.Control != null && e.NewElement != null)
             {
                UITableView oUITableView = (UITableView)this.Control;
                UIView dummyView = new UIView(new CoreGraphics.CGRect(0, 0, oUITableView.Bounds.Size.Width, 40));
                oUITableView.TableHeaderView = dummyView;
                oUITableView.ContentInset = new UIEdgeInsets(-40, 0, 0, 0);
                this.SetNativeControl(oUITableView);

            }
        }