ユーザークラスを次のように仮定します。
public class User {
public String name;
public String phone;
public List<String> emails;}
今あなたのレイアウトファイル:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="user"
type="com.androidbolts.databindingsample.model.User" />
</data>
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:emails="@{user}" />
</layout>
今あなたの活動クラスのサンプル
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
User user = new User();
List<String> emails = new ArrayList<>(5);
emails.add("email 1");
emails.add("email 2");
emails.add("email 3");
emails.add("email 4");
emails.add("email 5");
user.emails = emails;
binding.setUser(user);
}
@BindingAdapter({"bind:emails"})
public static void setEmails(LinearLayout parent, User user) {
for (int i = 0, size = user.getEmails().size(); i < size; i++) {
TextView tv = new TextView(parent.getContext());
tv.setText(user.getEmails().get(i));
parent.addView(tv);
}
}
}