首页 > 解决方案 > Android - 使用 ExpandableListView 在父视图中渲染子视图

问题描述

我想将我的 ExpandableListView 显示为单独的 CardView。因此,我需要将我的子视图膨胀到父布局中,而不是低于它。

有没有办法做到这一点?

标签: androidlayoutparent-childexpandablelistview

解决方案


我想出了一个充当 ExpandableListView 的解决方案,但实际上是由 CardViews 填充的 RecyclerView。单击标题(组标题)具有 onClick 事件以显示/隐藏子项

如果有人对我的解决方案感兴趣:

我创建了扩展 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);
}
}

每个子项都使用以下布局(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>

最后是组(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>

推荐阅读