程序化滚动列表视图

问题描述 投票:0回答:1

进入联系人界面后,将列表向下滚动,屏幕末尾会出现一个向上的箭头,重新聚焦第一个列表视图项目或视图。

如何添加编程,当你超过滚动15项的列表视图的动作按钮出现,或者如果你到达第一个元素,动作按钮消失。

代码转到第一个

//go to first
listView1.SmoothScrollToPosition(0);


//bad result 
  listview1.ScrollChange += (o, e) =>
            {

                if (listview1.FirstVisiblePosition == 0 && Convert.ToInt32(listview1.GetChildAt(0).GetY()) == 0)
                {
                    fab.Visibility = ViewStates.Gone;
                }

                else
                {
                    fab.Visibility = ViewStates.Visible;
                }
            };


xamarin.android
1个回答
2
投票

你可以使用 ItemAppearing 事件。当你滚动到最后一个项目时,显示向上按钮。而当你滚动到第一个项目时,隐藏这个按钮。

ItemAppearing: https:/docs.microsoft.comen-usdotnetapiXamarin.Forms.ListView.ItemAppearing?view=xamarin-forms。

Xaml:

 <StackLayout>
        <ListView
            ItemAppearing="ListView_ItemAppearing"             
            ItemsSource="{Binding strs}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Label Text="{Binding Str}" />
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <ImageButton
            x:Name="btn_Icon"
            HorizontalOptions="End"
            IsVisible="False"
            Source="up_big.png" />
    </StackLayout>

代码:

 public partial class Page7 : ContentPage
  {
    public ObservableCollection<Strs> strs { get; set; }
    public Page7()
    {
        InitializeComponent();
        strs = new ObservableCollection<Strs>()
        {
            new Strs(){ Str="mono1"},
            new Strs(){ Str="mono2"},
            new Strs(){ Str="mono3"},
            new Strs(){ Str="mono4"},
            new Strs(){ Str="mono5"},
            new Strs(){ Str="mono6"},
            new Strs(){ Str="mono7"},
            new Strs(){ Str="mono8"},
            new Strs(){ Str="mono9"},
            new Strs(){ Str="mono10"},
            new Strs(){ Str="mono11"},
            new Strs(){ Str="mono12"},
            new Strs(){ Str="mono13"},
            new Strs(){ Str="mono14"},
            new Strs(){ Str="mono15"},
            new Strs(){ Str="mono16"},
            new Strs(){ Str="mono17"},
            new Strs(){ Str="mono18"},
            new Strs(){ Str="mono19"},
            new Strs(){ Str="mono20"},
            new Strs(){ Str="mono21"},
            new Strs(){ Str="mono22"},
            new Strs(){ Str="mono23"}
        };
        this.BindingContext = this;
    }

    private void ListView_ItemAppearing(object sender, ItemVisibilityEventArgs e)
    {
        var item = e.Item as Strs;
        if (item == strs.Last())
        {
            btn_Icon.IsVisible = true;
        }
        else if (item == strs.First())
        {
            btn_Icon.IsVisible = false;
        }
    }

}
public class Strs
{
    public string Str { get; set; }
}

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.