可扩展列表视图中的Android数据绑定 [英] Android databinding in an expandable listview

查看:76
本文介绍了可扩展列表视图中的Android数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常具体的问题。我正在使用android数据绑定库:
https:// developer .android.com / topic / libraries / data-binding / index.html

I have a very specific Question. I am using the android databinding library: https://developer.android.com/topic/libraries/data-binding/index.html

我有这样的数据模型:

Class Participant
Public String Name;
//and so on
Public ObservableArrayList<Drink> DrinkList;
End Class
Class Drink extends BaseObservable
@Bindable
Public String Name;
@Bindable
Public Float Price;
End Class

ViewModel:

ViewModel:

Class ParticipantList 
public ObservableArrayList<Participant> list = new ObservableArrayList<>();
//some Methods to add items and concstructor

我也对所有对象都使用Getter和Setter @bindable字段。

I use also Getter and Setter for all @Bindable fields.

现在我有一个具有expandablelistview的活动,在该活动中,我通过XML和自定义@BindingAdapter将适配器绑定到expandablelistview,如下所示:

Now I have an activity with an expandablelistview where I bind an adapter to the expandablelistview via XML and an custom @BindingAdapter like this:

//in XML for the layout
<layout xmlns:bind="http://schemas.android.com/apk/res-auto"
   <data>
       <variable name="Participants" type="com.example.ParticipantList"/>
   </data>
<ExpandableListview>
<bind:participants=Particpants.list>

在绑定助手类中,此静态方法

In a binding helper Class this static method

    @BindingAdapter("participants")
public static void bindList(ExpandableListView view, ObservableArrayList<Participant> list) {
    ParticipantListAdapter adapter = new ParticipantListAdapter(list);
    view.setAdapter(adapter);
}

最后是我的适配器:

Class ParticpantListAdapter Extends ExpandableListAdapter {
public ObservableArrayList<Participant> list;
@Override
public View getGroupView(..)
ParticipantListItemBinding binding = DataBindingUtil.inflate(inflater,R.layout.participant_list_item, parent, false);
binding.setParticipant(list.get(groupposition));
return binding.getRoot();
}
@Override 
public View getChildView(..) {
DrinkListItemBinding binding = DataBindingUtil.inflate(inflater, R.layout.drink_list_item, parent, false);
binding.setDrink(list.get(goupposition).DrinkList.get(childposition));
return binding.getRoot();
}

我还有2个带有数据绑定layout.drink_list_item + layout.participant_list_item的xml LayoutFiles

I have 2 more xml LayoutFiles with data binding layout.drink_list_item + layout.participant_list_item which are bound to the participant and to the drink class.

我跳过了很多代码,这在这里并不是很重要。
一切工作正常,除了我的layout.participant_list_item中有一个按钮,它将打开一个新活动,可以在我的数据类中向饮料清单添加新饮料。当我完成活动时,应将一个新孩子添加到该组中。但是它只会在我折叠并扩大组一次后才会显示。通常,当我为@Bindable字段调用NotifyPropertychanged或将新项添加到直接绑定的ObservableArrayList中时,数据绑定会为我执行此操作。但这种情况并非如此。我将一个项目添加到ObservableArrayList中某个项目的ObservableArrayList中。有没有一种方法可以只刷新该组的孩子?还是我的方法是某种错误的方法?

I skipped a lot of code which is not really important here. All is working fine except that I have a button in my layout.participant_list_item which will open a new activity where I can add a new drink to my drinklist in my dataclasses. When I finish the activity a new child should be added to the group. But it will only show after I collapse and expands the group once. Usually databinding does this for me when I call NotifyPropertychanged for a @Bindable field or if I add a new item to a directly bound ObservableArrayList. But this is not the case. I add an item to an ObservableArrayList of an item in an ObservableArrayList. Is there a way to refresh only the children of that group? Or is my approach somehow wrong?

我的肮脏解决方案是,在我的onResume事件中告诉活动的主要绑定条件是,如果满足特定条件,它应该刷新所有绑定,从而导致对ExpandableListView Activity进行完全重绘,并且所有项目都将再次折叠(这不是期望的行为)。

My dirty solution is at the moment that I tell my main binding of the activity in its onResume event that if a certain condition is met, that it should refresh all bindings which leads to a complete redraw of the ExpandableListView Activity and all items will be collapsed again (which is not a desired behavior).

我写了所有由于我暂时不在开发位置,因此会从内存中获取代码。但是这个问题自几天以来一直困扰着我,到目前为止,我找不到合适的数据绑定和ExpandableListView示例。如果您需要更多信息,请问。

I wrote all the code from my memory since I m not at my development place at moment. But this problem bothers me since days and I could not find a decent example of Databinding and ExpandableListView so far. If you need more information just ask.

我很高兴收到任何提示,可以帮助我找到一个不错的解决方案。

I am glad for any hints which helps me to find a nice solution.

这是我在这里的第一个问题,因此,如果该职位没有达到所有要求,请保持友善。

This is my first question here so please be kind if the post is not meeting all the requirements.

提前谢谢

//编辑。编辑了一些代码。这里有人有任何有用的评论吗?您是否需要更多代码或我如何获得提示。.

//Edit. Edited some Code. Is anybody here that has any helpful comment? Do you need more code or how can I get some tips..

推荐答案

尝试一下。.

public class IExpandableListViewAdapter extends BaseExpandableListAdapter {
    @Override
    public int getGroupCount() {
        return 0;
    }

    @Override
    public int getChildrenCount(int i) {
        return 0;
    }

    @Override
    public Object getGroup(int i) {
        return null;
    }

    @Override
    public Object getChild(int i, int i1) {
        return null;
    }

    @Override
    public long getGroupId(int i) {
        return 0;
    }

    @Override
    public long getChildId(int i, int i1) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        if(view == null) {
            parentBinding = DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()), R.layout.basket_list_layout, viewGroup, false);
            view = parentBinding.getRoot();
        }
        else {
            parentBinding = (BasketListLayoutBinding)view.getTag();
        }
        parentBinding.setBasketParentModel(parent.get(i));
        view.setTag(parentBinding);
        return view;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        final int index = i;
        if(view == null) {
            childBinding = DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()), R.layout.basket_detail_layout, viewGroup, false);
            view = childBinding.getRoot();
        }
        else {
            childBinding = (BasketDetailLayoutBinding) view.getTag();
        }
        childBinding.setBasketChildModel(parent.get(i).getBasketChildModel());
        view.setTag(childBinding);
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }
}

这篇关于可扩展列表视图中的Android数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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