Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android databinding in an expandable listview

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

I have a datamodel like this:

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:

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

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

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

and finally my 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();
}

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.

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?

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).

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.

Thank you in advance

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

like image 660
Mono Avatar asked Jun 06 '16 11:06

Mono


1 Answers

Try this..

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;
    }
}
like image 127
Efe ÖZYER Avatar answered Nov 15 '22 21:11

Efe ÖZYER