数据绑定自定义项目列表

roc*_*cer 7 android android-databinding

如何将自定义项列表绑定到ListView或RecyclerView?仅使用Android DEFAULT DataBinding(无外部库)

<layout>
    <data>
        <import type="java.util.List"/>
        <variable name="listOfString" type="List&lt;String>"/>
    </data>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:?????="@{listOfString}/>  <!--Like we have ItemsSource in WPF-->

</layout>
Run Code Online (Sandbox Code Playgroud)

我来自WPF背景,其中有一个ItemTemplate选项.使用ItemTemplate,您可以纯粹通过XML将数据映射到视图.就像是:

<ListView ItemsSource="{Binding Path=UserCollection}">
  <ListView.ItemTemplate>
    <!--Populate template with each user data-->
    <DataTemplate>
      <WrapPanel>
        <!--Bind to user.Name-->
        <TextBlock Text="{Binding Name}" FontWeight="Bold" />
        <TextBlock Text="{Binding Age}" FontWeight="Bold" />
        <TextBlock Text="{Binding Mail}" />
      </WrapPanel>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>
Run Code Online (Sandbox Code Playgroud)

Ale*_*rek -1

查看这个库: https: //github.com/evant/binding-collection-adapter

该库允许将自定义对象的 ObservableList 绑定到 RecyclerView。