maher2020
maher2020

Reputation: 61

Scrolling listview programmatically

Upon entering the contacts interface and scroll list to down, an up arrow appears at the end of the screen to refocus the first listview item or view.

How to add programming where when you exceed Scroll 15 item of listview action button appears or if you reach the first element the action button disappears.

code to go to first

//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;
                }
            };


Upvotes: 0

Views: 277

Answers (1)

Wendy Zang - MSFT
Wendy Zang - MSFT

Reputation: 10978

You could use ItemAppearing event. When you scroll to the last item, show the up button. And when you scroll the first item, hide this button.

ItemAppearing: https://learn.microsoft.com/en-us/dotnet/api/Xamarin.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>

Code:

 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

Upvotes: 2

Related Questions