Compare commits

..

2 Commits

  1. 25
      XamarinStudy/XamarinStudy/XamarinStudy/Views/SampleListViewPage.xaml
  2. 44
      XamarinStudy/XamarinStudy/XamarinStudy/Views/SampleListViewPage.xaml.cs

@ -4,10 +4,31 @@
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>
<DataTemplate>
<TextCell Text="{Binding Name}" Detail="{Binding Price}"/>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.FooterTemplate>
<DataTemplate>
<Label Text="{Binding Count, StringFormat='{0}ea'}" HorizontalTextAlignment="Center" FontSize="Medium" TextColor="Navy"/>
</DataTemplate>
</ListView.FooterTemplate>
</ListView>
<ListView x:Name="groupedListView" ItemSelected="mainListView_ItemSelected" ItemsSource="{Binding .}" Footer="{Binding .}"
IsPullToRefreshEnabled="True" Refreshing="mainListView_Refreshing"
IsGroupingEnabled="True" GroupDisplayBinding="{Binding Key}">
<ListView.Header>
<ContentView BackgroundColor="Salmon">
<Label Text="음식 리스트" HorizontalTextAlignment="Center" FontSize="Large" TextColor="White"/>
</ContentView>
</ListView.Header>
<ListView.ItemTemplate>

@ -23,14 +23,27 @@ namespace XamarinStudy.Views
private void InitInstance()
{
foods = new List<Food>();
foods.Add(new Food() { Name = "냉면", Price = 10000, Flavor = "시원함" });
foods.Add(new Food() { Name = "모밀", Price = 80000, Flavor = "시원함" });
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;
//mainListView.ItemsSource = foods;
//mainListView.Footer = foods;
foods = GetFoodList();
mainListView.BindingContext = foods;
groupedListView.BindingContext = foods.OrderBy(f => f.Name).GroupBy(f => f.Flavor).ToList();
}
private List<Food> GetFoodList()
{
List<Food> list = new List<Food>();
list.Add(new Food() { Name = "냉면", Price = 10000, Flavor = "시원함" });
list.Add(new Food() { Name = "모밀", Price = 80000, Flavor = "시원함" });
list.Add(new Food() { Name = "국수", Price = 5000, Flavor = "얼큰함" });
list.Add(new Food() { Name = "쫄면", Price = 5000, Flavor = "매콤함" });
list.Add(new Food() { Name = "떡볶이", Price = 3000, Flavor = "매콤함" });
list.Add(new Food() { Name = "라면", Price = 3000, Flavor = "매콤함" });
list.Add(new Food() { Name = "짜장면", Price = 3000, Flavor = "달콤함" });
list.Add(new Food() { Name = "떡라면", Price = 5000, Flavor = "매콤함" });
list.Add(new Food() { Name = "짜장떡볶이", Price = 3000, Flavor = "달콤함" });
return list;
}
private void mainListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
@ -42,5 +55,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