执行搜索后,可扩展列表不会扩展子级

时间:2018-11-14 00:37:39

标签: android forms xamarin searchbar

借助这2个教程的帮助:https://www.youtube.com/watch?v=mTf6zHsM1y8https://www.youtube.com/watch?v=GG10QPrRC3w,一个Expandable可以正常工作,Add Searchbar搜索可以正常工作,但是在执行搜索后,Expandable list不会引起怀疑我在哪里做错了,所以请扩大帮助范围谢谢。

LexisPage.xaml(内容)

<ContentPage.BindingContext> <local:LexisListModel/> </ContentPage.BindingContext>

LexisPage.xaml

<Grid BackgroundColor="Black">
    <Grid.RowDefinitions>
        <RowDefinition Height="40" />
        <RowDefinition Height="8*" />
    </Grid.RowDefinitions>

    <SearchBar Grid.Row="0"
               x:Name="MySearch"
               Text="{Binding Word}"
               Margin="2,2,2,0"
               BackgroundColor="White"
               CancelButtonColor="Black"
               Placeholder="Search"
               TextChanged="MySearch_TextChanged"
               PlaceholderColor="LightGray"/>

    <ListView Margin="2,0,2,2"  
              Grid.Row="1"
              x:Name="MyList"
              BackgroundColor="White"
              ItemTapped="ListViewItem_Tabbed" 
              ItemsSource="{Binding Words}"  
              HasUnevenRows="True" >

        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout Padding="5">

                        <Label Text="{Binding Chang}"  
                               FontSize="Medium"  
                               TextColor="Black"/>

                        <StackLayout IsVisible="{Binding Isvisible}"  
                                     BackgroundColor="DarkGray"
                                     Margin="2,2,2,2">
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>

                                <Label Text="{Binding Hint}"  
                                       WidthRequest="110"
                                       Margin="2,2,2,2"
                                       FontSize="5"
                                       Font="Italic"
                                       TextColor="Blue"/>

                                <Label Text="{Binding English}"  
                                       HorizontalOptions="Center"
                                       WidthRequest="110"  
                                       FontSize="15"  
                                       TextColor="White"/>
                            </Grid>

                            <Label Text="{Binding Defination}"  
                                   WidthRequest="100"
                                   Margin="2"
                                   FontSize="15"  
                                   TextColor="White"/>
                        </StackLayout>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

LexisPage.xaml.cs

public ObservableCollection<Word> Words { get; set; }

    public LexisPage()
    {
        InitializeComponent();
        ViewList = new LexisListModel();
        BindingContext = ViewList;
    }

    private void ListViewItem_Tabbed(object sender, ItemTappedEventArgs e)
    {
        Word word = e.Item as Word;
        LexisListModel vm = BindingContext as LexisListModel;
        vm?.ShowOrHiddenWords(word);
    }

    public LexisListModel ViewList { get; set; }

    private void MySearch_TextChanged(object sender, TextChangedEventArgs e)
    {

          MyList.BeginRefresh();
          MyList.ItemsSource = string.IsNullOrWhiteSpace(e.NewTextValue)
              ? ViewList.Words
              : ViewList.Words
                  .Where(i => i.Chang.ToLower()
                      .Contains(e.NewTextValue));

          LexisListModel vm = BindingContext as LexisListModel;

          MyList.EndRefresh();
    }

ListViewModel.cs

    private Word _oldWord;
    public ObservableCollection<Word> Words { get; set; }
    public LexisListModel()
    {
        Words = new ObservableCollection<Word>
        {
            new Word{Chang="Aest", Hint="Eng", English="Teste", Defination="Test 1 2 3[TEST]" },
            new Word{Chang="Best", Hint="Eng", English="Testet", Defination="Test 1 2 3 4[TEST]" },
            new Word{Chang="Cest", Hint="Eng", English="Testete", Defination="Test 1 2 3 4 5[TEST]" },
            new Word{Chang="Dest", Hint="Eng", English="Testetet", Defination="Test 1 2 3 4 5 6[TEST]" },
        };
    }

    public void ShowOrHiddenWords(Word word)
    {
        if(_oldWord == word)
        {
            word.Isvisible = !word.Isvisible;
            UpDateWords(word);
        }
        else
        {
            if(_oldWord != null)
            {
                _oldWord.Isvisible = false;
                UpDateWords(_oldWord);
            }
            word.Isvisible = true;
            UpDateWords(word);
        }
        _oldWord = word;
    }

    private void UpDateWords(Word word)
    {
        int Index = Words.IndexOf(word);
        Words.Remove(word);
        Words.Insert(Index, word);
    }
}

Word.cs

public class Word
{
    public string Chang { get; set; }
    public string Hint { get; set; }
    public string English { get; set; }
    public string Defination { get; set; }
    public bool Isvisible { get; internal set; }
}

App.xaml.cs

public App()
    {
        InitializeComponent();

        MainPage = new LexisPage();
    }

0 个答案:

没有答案