使用动态添加的元素进行数据绑定 [英] Data-binding with dynamically added elements

查看:171
本文介绍了使用动态添加的元素进行数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始在我的新应用中实现Android数据绑定库。但是,我在数据动态添加元素方面遇到了一些困难。在我的 POJO 中,它包含< String,Double> 的地图。这里, String 是用户的ID, Double 是金额。我有一个用于显示单个条目的布局文件。因此,如果map包含2个元素,它将如下所示:

I have started implementing Android data-binding library to my new app. But, I am having some difficulties with data dynamically added elements. In my POJO it contains a map of <String,Double>. Here, String is the id of a user and Double is the amount. I have a layout file for displaying single entry. So, if map contains 2 elements, it will looks like this:

之前,我通过在循环内部扩展布局并为地图的每个项目添加 LinearLayout 来完成此操作。但是,现在我想用数据绑定来做这件事。

Previously, I have done this by inflating layouts inside loop and adding to LinearLayout for each item of a map. But, now I want to do this with data-binding.

As, Map 中的元素数量可以是1到20之间的任何内容,默认情况下我无法在布局文件中添加。我必须根据地图中的条目进行充气。我已经使用 POJO 成功实现了数据绑定,而没有 Map 。但是,无法理解如何通过数据绑定轻松完成。

As, the number of elements in the Map can be anything from 1 to 20, I can not add in the layout file by default. I have to inflate as per the entries in the map. I have successfully implemented data-binding with the POJO without a Map. But, unable to understand how this can be done with data-binding easily.

那么,有没有办法在数据绑定中直接执行此操作(在布局文件中)或者在java代码中使用尽可能少的代码?

So, Is there any way to do this directly in the data binding (in layout file) or with as little code possible in java code?

编辑:
对于所有人,谁看到它是一个简单的 RecyclerView ,事实并非如此。我同意 RecyclerView 是一个非常好的选择,当你有一个包含一些元素的列表并滚动时,这些视图将是性能优势。这里我在这个列表的上方和下方有不同类型的多个元素。我也使用了 RecyclerView 和数据绑定,我知道它是如何工作的,但实际情况并非如此。我只是想使用数据绑定来扩充布局(LinearLayout)中的多个行。

For all, who sees that it is a simple RecyclerView, it is not. I agree RecyclerView is very good choice when you have a list with some elements and scrolling that views will be performance benefit. Here I have multiple elements of different types above and below this list. I have used RecyclerView with data-binding also and I know how it works, but here it is not the case. I just want to inflate multiple rows inside a layout (LinearLayout) using data-binding.

推荐答案

因为您更喜欢创建布局动态地自己,我建议采用以下方法。目前它完全是理论上的,但我没有看到原因,为什么这不适合你。

Since you prefer to create the layout dynamically yourself, I'd propose the following approach. It's completely theoretical at the moment, but I don't see a reason, why this should not work for you.

考虑你设置地图具有绑定的自定义属性。

Considering you set the Map with a custom attribute for binding.

<LinearLayout ...
    app:inflateData="@{dataMap}" />

然后你必须创建一个 @BindingAdapter 处理适配器会为你做什么。

You then have to create a @BindingAdapter to handle what the adapter would do for you.

@BindingAdapter({"inflateData"})
public static void inflateData(LinearLayout layout, Map<String, Double> data) {
    LayoutInflater inflater = LayoutInflater.from(layout.getContext());
    for (Entry<String, Double> entry : data.entrySet()) {
        MyItem myItem = inflater.inflate(R.layout.my_item, layout, true);
        myItem.setKey(entry.getKey);
        myItem.setValue(entry.getValue);
    }
}

在这里你夸大了项目的布局并添加它到父布局。您可以通过向布局和绑定适配器添加更多属性来进一步概括它。

Here you inflate the layout for the item and add it to the parent layout. You could generalize it even more by adding more attributes to the layout and binding adapter.

这篇关于使用动态添加的元素进行数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆