我想将我的ExpandableListView显示为单独的CardView。 因此,我需要将子视图添加到父布局中,而不是在其下方。
有办法吗?
答案 0 :(得分:0)
我想出了一个充当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>