我想在单个屏幕上设置3个expandablelistview我想添加多个expandablelistview
XmlLayout
//this is my scorllview
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
tools:context=".fragments.FragmentTestWellnessscoreBoard">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/llHealth"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvHealthTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textStyle="bold"
android:layout_margin="@dimen/margin_10"
android:text="Health"/>
<ExpandableListView
android:id="@+id/elHealth"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:id="@+id/llChallenge"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvChallengeTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textStyle="bold"
android:layout_margin="@dimen/margin_10"
android:text="Challenge"/>
<ExpandableListView
android:id="@+id/elChallenge"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/llRisk"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvRiskTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textStyle="bold"
android:layout_margin="@dimen/margin_10"
android:text="Risk"/>
<ExpandableListView
android:id="@+id/elRisk"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
我想在用户点击时设置逐个适配器我不知道如何设置多个expandableList?
Java文件
private TestWellnessscoreBoardexpandablelistadapter mHealthaAdapter;
private TestWellnessscoreBoardexpandablelistadapter mChallengeAdapter;
private TestWellnessscoreBoardexpandablelistadapter mRiskAdapter;
private ExpandableListView elHealth;
private ExpandableListView elChallenge;
private ExpandableListView elRisk;
// this is my click listener to set adapter
private View.OnClickListener mOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tvHealthTitle:
mHealthaAdapter = new TestWellnessscoreBoardexpandablelistadapter(getActivity(),mWellnessResult.getWellnessArrayList());
elHealth.setAdapter(mHealthaAdapter);
break;
case R.id.tvChallengeTitle:
mChallengeAdapter = new TestWellnessscoreBoardexpandablelistadapter(getActivity(),mWellnessResult.getWellnessArrayList());
elChallenge.setAdapter(mChallengeAdapter);
break;
case R.id.tvRiskTitle:
mRiskAdapter = new TestWellnessscoreBoardexpandablelistadapter(getActivity(),mWellnessResult.getWellnessArrayList());
elRisk.setAdapter(mRiskAdapter);
break;
}
}
};
每件事都很好,但看不到全屏显示
但它没有全屏显示
解决方法
试试这个:
xml代码:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_expandable_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<ExpandableListView
android:id="@+id/activity_expandable_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"/>
</LinearLayout>
</ScrollView>
java类
mListView = (ExpandableListView) findViewById(R.id.activity_expandable_list_view);
Myexpandablelistadapter adapter = new Myexpandablelistadapter(this,mGroups);
mListView.setAdapter(adapter);
mListView.setonGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent,View v,int groupPosition,long id) {
setListViewHeight(parent,groupPosition);
return false;
}
});
需要这个功能:
private void setListViewHeight(ExpandableListView listView,int group) {
expandablelistadapter listadapter = (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,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();
}