listview, search bar, pull to refresh

main
syneffort 2 years ago
parent 49fc9d5319
commit 33a62908b5
  1. 6
      XamarinStudy/XamarinStudy/XamarinStudy/Views/SampleListViewPage.xaml
  2. 30
      XamarinStudy/XamarinStudy/XamarinStudy/Views/SampleListViewPage.xaml.cs

@ -4,10 +4,12 @@
x:Class="XamarinStudy.Views.SampleListViewPage">
<ContentPage.Content>
<StackLayout>
<ListView x:Name="mainListView" ItemSelected="mainListView_ItemSelected">
<SearchBar x:Name="foodSearchBar" Placeholder="원하시는 음식을 입력해 주세요" TextChanged="foodSearchBar_TextChanged"/>
<ListView x:Name="mainListView" ItemSelected="mainListView_ItemSelected" ItemsSource="{Binding .}" Footer="{Binding .}"
IsPullToRefreshEnabled="True" Refreshing="mainListView_Refreshing">
<ListView.Header>
<ContentView BackgroundColor="Salmon">
<Label Text="Food List" HorizontalTextAlignment="Center" FontSize="Large" TextColor="White"/>
<Label Text="음식 리스트" HorizontalTextAlignment="Center" FontSize="Large" TextColor="White"/>
</ContentView>
</ListView.Header>
<ListView.ItemTemplate>

@ -22,6 +22,13 @@ namespace XamarinStudy.Views
}
private void InitInstance()
{
//mainListView.ItemsSource = foods;
//mainListView.Footer = foods;
mainListView.BindingContext = GetFoodList();
}
private List<Food> GetFoodList()
{
foods = new List<Food>();
foods.Add(new Food() { Name = "냉면", Price = 10000, Flavor = "시원함" });
@ -29,8 +36,12 @@ namespace XamarinStudy.Views
foods.Add(new Food() { Name = "국수", Price = 5000, Flavor = "얼큰함" });
foods.Add(new Food() { Name = "쫄면", Price = 5000, Flavor = "매콤함" });
foods.Add(new Food() { Name = "떡볶이", Price = 3000, Flavor = "매콤함" });
mainListView.ItemsSource = foods;
mainListView.Footer = foods;
foods.Add(new Food() { Name = "라면", Price = 3000, Flavor = "매콤함" });
foods.Add(new Food() { Name = "짜장면", Price = 3000, Flavor = "달콤함" });
foods.Add(new Food() { Name = "떡라면", Price = 5000, Flavor = "매콤함" });
foods.Add(new Food() { Name = "짜장떡볶이", Price = 3000, Flavor = "달콤함" });
return foods;
}
private void mainListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
@ -42,5 +53,20 @@ namespace XamarinStudy.Views
DisplayAlert("맛", $"선택한 음식의 맛은 {selected.Flavor} 입니다.", "확인");
mainListView.SelectedItem = null;
}
private void foodSearchBar_TextChanged(object sender, TextChangedEventArgs e)
{
mainListView.BindingContext =
foods.Where(food => food.Name.ToLower().Contains(e.NewTextValue.Replace(" ", "").ToLower())).ToList();
}
private void mainListView_Refreshing(object sender, EventArgs e)
{
foodSearchBar.Text = "";
mainListView.BindingContext = GetFoodList();
DisplayAlert("상태", "음식 리스트를 다시 불러왔습니다.", "확인");
mainListView.EndRefresh();
}
}
}
Loading…
Cancel
Save