繁体   English   中英

Android-使用ExpandableListView在父视图内渲染子视图

[英]Android - Render child views inside parent view with ExpandableListView

我想将我的ExpandableListView显示为单独的CardView。 因此,我需要将子视图添加到父布局中,而不是在其下方。

有办法吗?

我想出了一个可作为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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM