0

I want to display my ExpandableListView as separate CardViews. Therefore I need to inflate my child views into the parent layout and not below it.

Is there a way to do it?

Megabight
  • 71
  • 1
  • 1
  • 10
  • 1
    For ExpandableListView, you may try my answer here: https://stackoverflow.com/questions/52369091/round-corners-expandablelistview/52402060#52402060, or you may use RecyclerView with 2 different view types: http://stackoverflow.com/questions/26245139/how-to-create-recyclerview-with-multiple-view-type. Hope that helps! – i_A_mok Oct 24 '18 at 03:29

1 Answers1

0

I came up with a solution that acts as ExpandableListView, but is actually a RecyclerView populated by CardViews. Clicking the header (group title) has onClick event to show/hide sub-items

If anyone was interested in my solution:

I have created custom Adapter extending RecyclerView.Adapter

public class CustomRecyclerAdapter extends RecyclerView.Adapter<CustomRecyclerAdapter.CustomElementViewHolder> {
private Context context;
private List<CustomElement> elements;

public CustomRecyclerAdapter(Context context, List<CustomElement> elements) {
    this.context = context;
    this.elements = elements;
}

public static class CustomElementViewHolder extends RecyclerView.ViewHolder {
    TextView title;
    LinearLayout container;
    LinearLayout header;

    CustomElementViewHolder(View itemView) {
        super(itemView);
        title = itemView.findViewById(R.id.list_group_title);
        container = itemView.findViewById(R.id.element_group);
        header = itemView.findViewById(R.id.group_title_container);
    }
}

@Override
public int getItemCount() {
    return elements.size();
}

@Override
public CustomElementViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_group, viewGroup, false);
    CustomElementViewHolder vh = new CustomElementViewHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(final CustomElementViewHolder customElementViewHolder, int i) {
    final CustomElement element = elements.get(i);

    customElementViewHolder.title.setText(element.Name);

    customElementViewHolder.header.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(customElementViewHolder.container.getVisibility()==View.GONE)
                customElementViewHolder.container.setVisibility(View.VISIBLE);
            else if(customElementViewHolder.container.getVisibility()==View.VISIBLE)
                customElementViewHolder.container.setVisibility(View.GONE);
        }
    });

    for (SubItem subItem: element.SubList) {
        LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View actualView = inflater.inflate(R.layout.list_item, null);

        TextView itemHeader = actualView.findViewById(R.id.list_item_label);
        itemHeader.setText(subItem.header);
        TextView itemMessage = actualView.findViewById(R.id.list_item_text);
        itemMessage.setText(subItem.text);

        customElementViewHolder.container.addView(actualView);
    }
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}
}

Each sub-item is rendered with the following layout (list_item.xml)

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<View
    style="@style/DetailCardViewSeparator"/>

<LinearLayout
style="@style/DetailCardValueLlt">

    <TextView
        android:id="@+id/list_item_label"
        style="@style/DetailCardNameValueTvw"/>

    <TextView
        style="@style/DetailCardValueTvw"
        android:id="@+id/list_item_text"
        android:justificationMode="inter_word"/>

</LinearLayout>

and finally the group (list_group.xml)

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">

    <android.support.v7.widget.CardView
    style="@style/DetailCardView">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/group_title_container"
            style="@style/DetailCardViewHeadLlt">
            <TextView
                style="@style/DetailCardViewHeadTitle"
                android:id="@+id/list_group_title"/>
        </LinearLayout>

        <LinearLayout
            style="@style/DetailCardViewMainLlt"
            android:id="@+id/element_group"/>       

    </LinearLayout>     
    </android.support.v7.widget.CardView>
</LinearLayout>
Megabight
  • 71
  • 1
  • 1
  • 10