gpt4 book ai didi

java - Expandablelistview subview 呈现两次(尽管高度设置为 match_parent)

转载 作者:行者123 更新时间:2023-11-30 01:49:53 25 4
gpt4 key购买 nike

我已经为 ExpandableListView 的双重渲染问题苦苦挣扎了几个小时。我浏览了 stackoverflow 上的文档和问题,建议我将可扩展 ListView 的高度设置为 fill_parentmatch_parent 但到目前为止没有任何效果。

查看代码如下:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="@color/white">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rlQnaContainer">

<ExpandableListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/elQnAList"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="@+id/llAskQuestionContainer"
android:groupIndicator="@null"
android:layout_alignParentTop="true"
android:layout_alignTop="@+id/llAskQuestionContainer"/>

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/llAskQuestionContainer"
android:gravity="center"
android:id="@+id/tvNoQuestions"
android:visibility="gone"
android:text="@string/company_qna_no_questions_text"/>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="vertical"
android:id="@+id/llAskQuestionContainer"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/lightgrey"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
/>

<Button
android:layout_margin="16dp"
android:id="@+id/btAskQuestion"
style="@style/HRTheme.ButtonPrimary"
android:text="@string/company_qna_ask_question"
/>

<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone"
android:id="@+id/pbAskQuestionProgressBar"/>

</RelativeLayout>

</RelativeLayout>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:background="@color/white"
android:id="@+id/llProgressBarContainer">

<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="visible"
android:id="@+id/pbProgressBar"
/>

</RelativeLayout>

<include layout="@layout/generic_loader_and_no_connection" />

</FrameLayout>

适配器代码如下:

public class CompanyQnAExpandableListAdapter extends BaseExpandableListAdapter {
List<QnA> qnas = new ArrayList<>();
Context context;

HeaderViewHolder headerViewHolder;
ChildViewHolder childViewHolder;

public void addNewQuestion(QnA qna) {
qnas.add(0, qna);
notifyDataSetChanged();
}

class HeaderViewHolder {
TextView tvQuestion;
}

class ChildViewHolder {
TextView tvAnswer;
TextView tvByText;
}
public CompanyQnAExpandableListAdapter(Context context, List<QnA> listQnA) {
this.context = context;
this.qnas = listQnA;
}

public void addItems(List<QnA> qnas) {
qnas.addAll(qnas);
notifyDataSetChanged();
}

@Override
public int getGroupCount() {
return qnas.size();
}

@Override
public int getChildrenCount(int groupPosition) {
return qnas.size();
}

@Override
public Object getGroup(int groupPosition) {
QnA qna = qnas.get(groupPosition);
return qna;
}

@Override
public Object getChild(int groupPosition, int childPosition) {
QnA qna = qnas.get(childPosition);
return qna;
}

@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
QnA qna = qnas.get(groupPosition);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.expandable_list_group_header_qna, null);
headerViewHolder = new HeaderViewHolder();
headerViewHolder.tvQuestion = (TextView) convertView.findViewById(R.id.tvHeaderQuestion);
convertView.setTag(headerViewHolder);
} else {
headerViewHolder = (HeaderViewHolder) convertView.getTag();
}

headerViewHolder.tvQuestion.setText(qna.question);
return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
QnA qna = qnas.get(childPosition);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.expandable_list_qna_item, null);
childViewHolder = new ChildViewHolder();
childViewHolder.tvByText = (TextView) convertView.findViewById(R.id.tvQuestionMeta);
childViewHolder.tvAnswer = (TextView) convertView.findViewById(R.id.tvQnaAnswer);
convertView.setTag(childViewHolder);
} else {
childViewHolder = (ChildViewHolder) convertView.getTag();
}

if (qna.answered == QnA.ANSWERED) {
childViewHolder.tvByText.setText(qna.byText);
childViewHolder.tvAnswer.setText(qna.answer);
} else {
childViewHolder.tvByText.setVisibility(View.GONE);
childViewHolder.tvAnswer.setText(context.getString(R.string.qna_awaiting_answer));
}

return convertView;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}

@Override
public boolean hasStableIds() {
return true;
}
}

这就是我调用设置适配器的方式:

adapter = new CompanyQnAExpandableListAdapter(getActivity(), qnas);
expandableListView.setAdapter(adapter);

如果有人能帮我弄清楚这里出了什么问题,那就太好了。提前谢谢你。

最佳答案

getGroupCount() 方法告诉您 ExpandableListView 中会有多少组,getChildrenCount(int groupPosition) 方法告诉您相应的子项有多少个组。

问题是您在这两种方法中返回的计数相同。我认为通过解决这个问题,您可能会摆脱这个问题。

关于java - Expandablelistview subview 呈现两次(尽管高度设置为 match_parent),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33226340/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com