繁体   English   中英

Android数据绑定在可扩展的列表视图中

[英]Android databinding in an expandable listview

我有一个非常具体的问题。 我正在使用android数据绑定库: 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

视图模型:

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

我还为所有@Bindable字段使用Getter和Setter。

现在我有一个带有expandablelistview的活动,我通过XML将一个适配器绑定到expandablelistview,并将自定义的@BindingAdapter绑定到这样:

//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>

在绑定帮助器Class中这个静态方法

    @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个xml LayoutFiles,带有数据绑定layout.drink_list_item + layout.participant_list_item,它们绑定到参与者和饮料类。

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

我的脏解决方案是我告诉我onResume事件中活动的主要绑定,如果满足某个条件,它应该刷新所有绑定,导致ExpandableListView活动的完全重绘,所有项目将被折叠再次(这不是一个理想的行为)。

我从记忆中写下了所有的代码,因为我现在不在我的开发地点。 但是这个问题困扰着我,因为到目前为止我找不到Databinding和ExpandableListView的正确例子。 如果您需要更多信息,请询问。

我很高兴有任何提示可以帮助我找到一个很好的解决方案。

这是我的第一个问题,如果帖子不符合所有要求,请保持友好。

先感谢您

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

尝试这个..

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;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM