首页 > 解决方案 > Listview 在 iOS 上滚动,但不是在 Android 上滚动

问题描述

在 iOS 上运行良好,但在 Android 上无法滚动。你能检查一下我做错了什么或者有什么想法可能导致这种情况,或者要检查什么?我尝试添加滚动视图,添加 heightRequest,并将verticalOptions 更改为FillAndExpand,但似乎没有任何效果

<ContentView.Content>
        <AbsoluteLayout Margin="0,0,30,0">
            <Entry
                x:Name="searchBar"
                AbsoluteLayout.LayoutBounds="0,0,1,AutoSize"
                AbsoluteLayout.LayoutFlags="WidthProportional"
                BackgroundColor="White"
                HeightRequest="40"
                Placeholder="Enter sensor"
                TextChanged="SearchBar_OnTextChanged"
                TextColor="{DynamicResource RelogixDarkGray}"
                VerticalOptions="Center" />
            <ListView
                x:Name="dataListView"
                AbsoluteLayout.LayoutBounds="5,40,.98,.4"
                AbsoluteLayout.LayoutFlags="SizeProportional"
                BackgroundColor="White"
                CachingStrategy="RecycleElement"
                HasUnevenRows="True"
                IsVisible="False"
                ItemTapped="ListView_OnItemTapped">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>

                            <StackLayout Margin="5" Padding="0">
                                <Label
                                    BackgroundColor="White"
                                    FontFamily="{StaticResource NormalFont}"
                                    FontSize="16"
                                    Text="{Binding .}"
                                    TextColor="#FF464859"
                                    VerticalOptions="Center"
                                    VerticalTextAlignment="Center" />
                            </StackLayout>

                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </AbsoluteLayout>

标签: xamarin.formsxamarin.androidxamarin.ios

解决方案


首先,我不知道你用的是什么TextColor="{DynamicResource RelogixDarkGray}"FontFamily="{StaticResource NormalFont}"所以我删除它进行测试。

然后你IsVisible="False"在你的里面设置ListView,如果你把值设置为false,就看不到了,运行的时候我把它设置为True。

而且你没有ItemsSource="{Binding myModels}"为你的listview. 你应该设置它,这是我的布局代码。

<AbsoluteLayout Margin="0,0,30,0">
                <Entry
                x:Name="searchBar"
                AbsoluteLayout.LayoutBounds="0,0,1,AutoSize"
                AbsoluteLayout.LayoutFlags="WidthProportional"
                BackgroundColor="White"
                HeightRequest="40"
                Placeholder="Enter sensor"
                TextChanged="searchBar_TextChanged"

                VerticalOptions="Center" />
                <ListView
                    ItemsSource="{Binding myModels}"
                x:Name="dataListView"
                AbsoluteLayout.LayoutBounds="5,40,.98,.4"
                AbsoluteLayout.LayoutFlags="SizeProportional"
                BackgroundColor="White"
                CachingStrategy="RecycleElement"
                HasUnevenRows="True"
                IsVisible="True"
                ItemTapped="dataListView_ItemTapped">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>

                                <StackLayout Margin="5" Padding="0">
                                    <Label
                                    BackgroundColor="White"

                                    FontSize="16"
                                    Text="{Binding .}"
                                    TextColor="#FF464859"
                                    VerticalOptions="Center"
                                    VerticalTextAlignment="Center" />
                                </StackLayout>

                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </AbsoluteLayout>

我注意到您使用了 MVVM,您应该BindingContext = new MyModelView();像这样简单地添加布局背景代码。

  public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();

            BindingContext = new MyModelView();
        } 
}

这是MyModelView.cs

    public class MyModelView
    {
        public ObservableCollection<MyModel> myModels { get; set; }
        public MyModelView()
        {

            myModels = new ObservableCollection<MyModel>();
            myModels.Add(new MyModel() { Prop0="xxx",Prop1="xxx1",Prop2="xxxx2",Prop3=true,Prop4="xxxx4" });
            myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
            myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
            myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
            myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
            myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
            myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
            myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
            myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
            myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
            myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
            myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
            myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
            myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
            myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
            myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
            myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
            myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
            myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
            myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
            myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
            myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
            myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
            myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
        }
    }

这里是MyModel.cs

    public class MyModel
    {
        public string Prop0 { get; set; }
        public string Prop1 { get; set; }
        public string Prop2 { get; set; }
        public bool Prop3 { get; set; }
        public string Prop4 { get; set; }
    }

这里正在运行 GIF。

在此处输入图像描述

如果我为Label

<Label
                                    BackgroundColor="White"

                                    FontSize="16"
                                    Text="{Binding Prop0}"
                                    TextColor="#FF464859"
                                    VerticalOptions="Center"
                                    VerticalTextAlignment="Center" />

这里正在运行 GIF。

在此处输入图像描述


推荐阅读