列表视图 SCROLL 顶部的堆栈布局

Stack Layout on top of List view SCROLL

如何将堆栈布局附加到列表视图的顶部,以便它随列表视图一起滚动?

我试过将堆栈布局和列表视图放在滚动视图中,但根据我滚动的位置,我得到了奇怪的重叠。 (见下文)

我希望堆栈布局固定在列表视图上并让它们一起滚动!

滚动前:

滚动后:

代码:

<!-- Scrollview-->
        <ScrollView Grid.Row="2" Grid.Column="0" BackgroundColor="#4D148C">
            <Grid RowSpacing="0">
                <Grid.RowDefinitions>
                    <RowDefinition Height="150"/> <!--detail -->
                    <RowDefinition Height="*"/> <!--related videos listview -->
                </Grid.RowDefinitions>

                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>

                <!--detail -->
                <StackLayout x:Name="VideoDetail" BackgroundColor="White" Grid.Row="0" Grid.Column="0" Padding="10, 10, 10, 10" Margin=" 0,0,0,3">

                    <Label Text ="{Binding Title}" FontAttributes="Bold"/>
                    <Label Text ="{Binding View_Count}" TextColor="Gray"/>
                    <Label Text ="{Binding Author_By}" TextColor="Gray" />
                    <Label Text ="{Binding Uploaded}" TextColor="Gray"/>


                </StackLayout>


                <!--related videos listview -->
                <ListView x:Name="VideoListView" HasUnevenRows="True" ItemTapped="ViewCellItem_Clicked" BackgroundColor="#4D148C" Grid.Row="1" Grid.Column="0">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <ViewCell.View>
                                    <Grid RowSpacing="0" ColumnSpacing="10" BackgroundColor="White" Padding="10,10,10,10" Margin="0,0,0,3">
                                        <!-- "left, top, right, bottom" -->
                                        <!-- "left, top, right, bottom" -->
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="auto"/>
                                            <RowDefinition Height="auto"/>
                                            <RowDefinition Height="auto"/>
                                        </Grid.RowDefinitions>

                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="auto"/>
                                            <ColumnDefinition Width="auto"/>
                                        </Grid.ColumnDefinitions>

                                        <!-- Image Container -->

                                        <!-- NOTE: youtube thumnail dimensions are 128x72-->
                                        <Grid Grid.Row="0" Grid.Column="0" Grid.RowSpan="3"  BackgroundColor="Black">
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="72"/>
                                            </Grid.RowDefinitions>

                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="128"/>
                                            </Grid.ColumnDefinitions>

                                            <Image 
                                        Source="{Binding Thumbnail}" 
                                        Grid.Row="0" Grid.Column="0"
                                        HorizontalOptions="Center"
                                        VerticalOptions="Center"/>
                                        </Grid>

                                        <Label 
                                            Text="{Binding Title}" 
                                            FontAttributes="Bold" 
                                            Grid.Row="0" Grid.Column="1" 
                                            VerticalTextAlignment="Center"/>
                                        <Label 
                                            Text="{Binding Author_Views}" 
                                            TextColor="Gray" 
                                            Grid.Row="1" Grid.Column="1" 
                                            VerticalTextAlignment="Center"/>
                                        <Label 
                                            Text="{Binding Uploaded}" 
                                            TextColor="Gray" 
                                            Grid.Row="2" Grid.Column="1" 
                                            VerticalTextAlignment="Center"/>
                                    </Grid>
                                </ViewCell.View>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>




            </Grid>   <!-- inner grid -->           
        </ScrollView>

如果您想将布局添加为 ListView 顶部的一部分,您始终可以使用 ListView Header。

对于您的情况,只需执行以下操作:

<ListView x:Name="VideoListView" HasUnevenRows="True" ItemTapped="ViewCellItem_Clicked" BackgroundColor="#4D148C" Grid.Row="1" Grid.Column="0">
    <ListView.Header>
        <StackLayout x:Name="VideoDetail" BackgroundColor="White" Grid.Row="0" Grid.Column="0" Padding="10, 10, 10, 10" Margin=" 0,0,0,3">
            <Label Text ="{Binding Title}" FontAttributes="Bold"/>
            <Label Text ="{Binding View_Count}" TextColor="Gray"/>
            <Label Text ="{Binding Author_By}" TextColor="Gray" />
            <Label Text ="{Binding Uploaded}" TextColor="Gray"/>
        </StackLayout>
    </ListView.Header>    
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <ViewCell.View>
                    <Grid RowSpacing="0" ColumnSpacing="10" BackgroundColor="White" Padding="10,10,10,10" Margin="0,0,0,3">
                        <!-- "left, top, right, bottom" -->
                        <!-- "left, top, right, bottom" -->

                        <!-- I omitted the ListView Item implementation -->

                    </Grid>
                </ViewCell.View>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

这样 StackLayout 将成为 ListView 的一部分,因为它会一直滚动。

注意:避免在 ScrollView 中使用 ListView 否则你会 运行 陷入很多问题。