Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Listview, scrolling position.

supersteffsupersteff Member ✭✭
edited October 2019 in Xamarin.Forms

Good Afternoon, Morning, Evening

I have a 3 list views in three columns, I have it set up this way for spacing issues.

Essentially, the 3 lists are the same list just different parts of the same class.

I want the three lists to scroll synchronously

Is there any way to bind the "scrolled percentage" to the other 2 listviews?

Best Answer

Answers

  • ColeXColeX Member, Xamarin Team Xamurai
    edited October 2019

    ListView has the method ScrollTo . You can call it when you scroll another listview .

  • supersteffsupersteff Member ✭✭

    Hi, how would you implement this in VMMV?

  • supersteffsupersteff Member ✭✭

    @ColeX said:
    Just simple suggestion

    1. Create ViewModel for the three listview .
    2. Use ItemAppearing event to detect the scrolling of the first listview.
    3. Use ScrollTo method to scroll other listview.

    Xaml

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
    
        <ListView Grid.Column="0" ItemAppearing="ListView_ItemAppearing" x:Name="list1">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextCell Text="{Binding}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
          
        </ListView>
    
        <ListView Grid.Column="1" x:Name="list2">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextCell Text="{Binding}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    
        <ListView Grid.Column="2" x:Name="list3">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextCell Text="{Binding}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
    

    Code behind

      public List<int> list { get; set; }
    
        public Page1()
        {
            InitializeComponent();
    
            list = new List<int>();
            for (int i = 0; i < 100; i++)
            {
                list.Add(i);
            }
    
            list1.ItemsSource = list;
            list2.ItemsSource = list;
            list3.ItemsSource = list;
        }
    
        private void ListView_ItemAppearing(object sender, ItemVisibilityEventArgs e)
        {
            list2.ScrollTo(e.Item,ScrollToPosition.MakeVisible,true);
            list3.ScrollTo(e.Item,ScrollToPosition.MakeVisible,true);
        }
    

    Does this only work when all 3 lists have the same information in them?

  • ColeXColeX Member, Xamarin Team Xamurai

    No , this is only a sample , you could create your own logic , refer the following code

      private void ListView_ItemAppearing(object sender, ItemVisibilityEventArgs e)
        {
            int index = list.IndexOf(e.Item as string);   //get the index
    
            list2.ScrollTo(list2[index], ScrollToPosition.MakeVisible, true);
            list3.ScrollTo(list3[index + 1], ScrollToPosition.MakeVisible, true);
        }
    
  • supersteffsupersteff Member ✭✭

    Thanks, I was working on this, but you saved me time!!

Sign In or Register to comment.