我需要创建可扩展列表的列表。问题是可扩展列表的高度是一行。它看起来像这样:
如果我将高度硬编码为300dp,它将如下所示:
这是我的代码(我删除了一些冗余信息):
public class AllQuestionsListAdapter extends BaseAdapter{
private final ArrayList<QuestionsList> questionsList;
private final LayoutInflater mInflater;
private final Context context;
public AllQuestionsListAdapter(ArrayList<QuestionsList> questionsList, Context context) {
this.questionsList = questionsList;
this.context = context;
this.mInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = mInflater.inflate(R.layout.all_question_list_item, null);
ViewHolder vh = new ViewHolder();
vh.theme = rowView.findViewById(R.id.aq_item_list_theme);
vh.theme.setText(questionsList.get(position).getTheme());
vh.listView = rowView.findViewById(R.id.aq_item_list_questions);
QuestionsExpandableListAdapter adapter = new QuestionsExpandableListAdapter(context, questionsList.get(position));
vh.listView.setAdapter(adapter);
return rowView;
}
static class ViewHolder{
TextView theme;
ExpandableListView listView;
}
}
public class QuestionsExpandableListAdapter implements ExpandableListAdapter {
private Context context;
private QuestionsList questionsList;
public QuestionsExpandableListAdapter(Context context, QuestionsList questionsList){
this.context = context;
this.questionsList = questionsList;
}
@Override
public Object getGroup(int groupPosition) {
return questionsList.getQuestions().get(groupPosition).getQuestion();
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return questionsList.getQuestions().get(groupPosition).getAnswer();
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String headerInfo = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.group_items, null);
}
TextView heading = convertView.findViewById(R.id.heading);
heading.setText(headerInfo);
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
String detailInfo = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.child_items, null);
}
TextView childItem = convertView.findViewById(R.id.childItem);
childItem.setText(detailInfo);
return convertView;
}
}
all_question_list_item.xml:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="300dp">
<TextView
android:id="@+id/aq_item_list_theme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="aaaaaaaaaaaa"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ExpandableListView
android:id="@+id/aq_item_list_questions"
android:layout_width="0dp"
android:layout_height="300dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/aq_item_list_theme">
</ExpandableListView>
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:0)
此代码为我解决了问题:
public class CustomExpandableList extends ExpandableListView {
public CustomExpandableList(Context context) {
super(context);
}
public CustomExpandableList(Context context, AttributeSet attrs) {
super(context, attrs);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
heightMeasureSpec = MeasureSpec.makeMeasureSpec(6000, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}