【问题标题】:Android databinding in an expandable listview可扩展列表视图中的 Android 数据绑定
【发布时间】:2016-06-06 11:33:32
【问题描述】:

我有一个非常具体的问题。我正在使用 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。

现在我有一个带有可扩展列表视图的活动,我通过 XML 和自定义 @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>

在绑定助手类中这个静态方法

    @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 绑定到参与者和饮料类。

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

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

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

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

这是我在这里的第一个问题,所以如果帖子不符合所有要求,请善待。

提前谢谢你

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

【问题讨论】:

  • 我遇到了这个确切的问题,你能弄清楚吗?
  • 很遗憾没有,因为这是一个爱好项目,我被困在那里并没有找到一个干净的解决方案。所以我使用了 refresh all 绑定方法,虽然很丑,但至少有效。如果您找到合适的解决方案,请告诉我。

标签: java android data-binding expandablelistview expandablelistadapter


【解决方案1】:

试试这个..

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

【讨论】:

  • 这对我没有帮助,抱歉。我知道如何将列表绑定到列表视图。正如您在我的帖子中看到的那样,我使用了一个可扩展的列表视图,其中有 GetChildView 和 GetGroupView 并将我的列表绑定到它。我遇到的问题是如何在子视图中绑定列表发生变化时更新视图。
  • 尝试新版本
  • 看起来很有希望,有时间我会尝试并给你反馈。到目前为止,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多