Xamarin ListView:展开项目

时间:2018-10-29 09:38:30

标签: listview xamarin xamarin.forms

我想创建一个新闻页面。

现在我有一个空的ListView。如果点击我的Button,将创建一个ListView Item

在下一步中,我希望能够点击ListView Item。点击后,我希望文本出现在点击的项目下方。

点击AddItem->创建NewsArticle 1->点击NewsArticle 1-> NewsArticle 1展开其下的文本

这样可能吗?

C#:

public partial class News : ContentPage
{

    ObservableCollection<NewsName> NewsItem = new ObservableCollection<NewsName>();
    int counter = 0;


    public News()
    {
        InitializeComponent();

        NewsView.ItemsSource = NewsItem;
    }

     void AddArticle(object sender, EventArgs e)
    {
        counter++;
        NewsItem.Insert(0, new NewsName { NewsItem = "NewsArticle " + counter.ToString() });
    }

    public class NewsName
    {
        public string NewsItem { get; set; }
    }

}

XAML:

<StackLayout>
    <Label Text="Newss"></Label>
    <Button Clicked="AddArticle" Text="AddItem"></Button>
    <ListView x:Name="NewsView">
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextCell Text="{Binding NewsItem}" />
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView> 
</StackLayout>   

2 个答案:

答案 0 :(得分:0)

在列表视图中,您具有“选定”属性,它为您提供了当前选定的项目。您可以为每个元素添加一个手势识别器-轻按它即可创建动作。

答案 1 :(得分:0)

您可以自定义 ListView.ItemTemplate ,并使用其 ItemSelected 可以做到。

XAML

  <ListView ItemsSource="{Binding Monkeys}" 
              HasUnevenRows="true" 
              ItemTapped="OnListViewItemTapped"
              ItemSelected="OnListViewItemSelected">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Margin="10"  VerticalOptions="Center">
                            <Label  x:Name="label"
                               Text="{Binding Location}"
                               VerticalOptions=" />
                            <Button Grid.RowSpan="2"
                                    Grid.Column="2"
                                    Text="Click"
                                    HorizontalOptions="End"
                                    Clicked="OnClicked"/>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

您需要创建一个ViewModel绑定到xaml。

ViewModel

class MonkeysViewModel
{
    public IList<Monkey> Monkeys { get; private set; }
    public MonkeysViewModel()
    {
        Monkeys = new List<Monkey>();
        //f = new F[5];
        Monkeys.Add(new Monkey
        {
            Name = "Baboon",
            Location = "Africa & Asia"
        });
    }
 }

  class Monkey : INotifyPropertyChanged
 {
   private string name = "520";
   public string Name
    {
        set
        {
            if (name != value)
            {
                name = value;
                OnPropertyChanged("Name");
            }
        }
        get
        {
            return name;
        }
    }

   public string Location { get; set; }

   public event PropertyChangedEventHandler PropertyChanged;


    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
 }

最后在ContentPage中,您应该绑定模型并添加EventHandle

ContentPage

  BindingContext = new MonkeysViewModel();

  async void OnListViewItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
       //do your listView selected method
    }
   void OnClicked(Object sender, EventArgs args)
    {
       //do your each list's button clicked method
    }

注意:如果您希望modle的值是动态的,则需要使用 INotifyPropertyChanged