我在滚动视图中的片段中使用了两个不同的ExpandableListViews
,一个在另一个下面。
问题是调用活动时只显示一个ExpandableListView
标题。请参考下图:
此外,当我单击可展开列表视图时,列表视图会展开,另一个ExpandableListView
也会显示。请参考下图:
我希望第一次调用活动时显示可扩展列表视图。
这是我的xml:
<ExpandableListView
android:id="@+id/exLInTheMoodFor"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:scrollbars="none"
android:groupIndicator="@null"
android:layout_below="@+id/lblInTheMoodFor"
android:layout_marginTop="10dp"/>
这是用于定义和初始化Expandable列表视图并设置height:
的java代码 @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View convertView = inflater.inflate(R.layout.cafes_more_fragment, container, false);
final ExpandListChild1 items = new ExpandListChild1();
exLInTheMoodFor = (ExpandableListView) convertView.findViewById(R.id.exLInTheMoodFor);
ExpListItems = SetStandardGroups();
ExpAdapter = new ExpandListAdapterProduct(activity, ExpListItems);
exLInTheMoodFor.setAdapter(ExpAdapter);
exLInTheMoodFor.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
setListViewHeight(exLInTheMoodFor, i);
return false;
}
});
return convertView;
}
//for set height show method in expnadable list view
private void setListViewHeight(ExpandableListView listView, int group) {
android.widget.ExpandableListAdapter listAdapter = (android.widget.ExpandableListAdapter) listView.getExpandableListAdapter();
int totalHeight = 0;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(),
View.MeasureSpec.EXACTLY);
for (int i = 0; i < listAdapter.getGroupCount(); i++) {
View groupItem = listAdapter.getGroupView(i, false, null, listView);
groupItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += groupItem.getMeasuredHeight();
if (((listView.isGroupExpanded(i)) && (i != group))
|| ((!listView.isGroupExpanded(i)) && (i == group))) {
for (int j = 0; j < listAdapter.getChildrenCount(i); j++) {
View listItem = listAdapter.getChildView(i, j, false, null,
listView);
listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
}
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
int height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getGroupCount() - 1));
if (height < 10)
height = 200;
params.height = height;
listView.setLayoutParams(params);
listView.requestLayout();
}
答案 0 :(得分:0)
扩展列表中的所有组
for(int i=0; i < myAdapter.getGroupCount(); i++)
mexpandableListView.expandGroup(i);
如果扩展列表中的一个项目
mexpandableListView.expandGroup(0);
一项扩展和平衡项目压缩
mexpandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int i) {
ExpandableAdapter myAdapter= (ExpandableHomeworkAdapter) mexpandableListView.getExpandableListAdapter();
if (myAdapter== null){
return;
}
for (int k=0;k<myAdapter.getGroupCount();k++){
if (k!=i){
mexpandableListView.collapseGroup(k);
}
}
}
});
答案 1 :(得分:0)