androidandroid-recyclerviewexpandablerecyclerview

How do you set an Expandable Recycler View's parent views to be expanded by default


I have an expandable recycler view, and it is displayed when my activity is started. However by default the parent views are collapsed.

I have tried setting setExpanded(true) inside my parent view holder like so:

public PersonParentViewHolder(View itemView) {
    super(itemView);

    mPersonTitleTextView = (TextView) itemView.findViewById(R.id.parent_list_item_person_title_text_view);
    mParentDropDownArrow = (ImageButton) itemView.findViewById(R.id.parent_list_item_expand_arrow);
    setExpanded(true);
}

But that does not seem to help.

This is the creation of the ParentViewHolder

@Override
public PersonParentViewHolder onCreateParentViewHolder(ViewGroup viewGroup) {
    View view = mInflater.inflate(R.layout.person_parent_layout, viewGroup, false);

    return new PersonParentViewHolder(view);
}

Here is where the recycler view is created if that helps at all

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.person_fragment_layout, container, false);

    recyclerView = (RecyclerView) view
            .findViewById(R.id.person_recycler_view);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    updateUI(view);

    return view;
}

Thanks in advance!


Solution

  • I figured out what I was doing wrong. I was using a deprecated version of BigNerdRanch's ExpandableRecyclerView. I was able to expand the ParentView by updating to the latest version of the ExpandableRecyclerView. To do so:

    add compile 'com.bignerdranch.android:expandablerecyclerview:2.1.1' to the dependencies of the app gradle, and install the necessary packages.

    Then on the your Parent objects for the expandable list that implement ParentListItem you need to override the isInitiallyExpanded like this

    @Override
    public boolean isInitiallyExpanded() {
        return true;
    }
    

    This was standard as of version 2.0.0 or so.

    The complete tutorial for the latest version (2.1.1) can be found here. http://bignerdranch.github.io/expandable-recycler-view/

    Hope this helps!