1
我現在正在努力解決ExpandableListView的雙重渲染問題幾個小時。我經歷了關於stackoverflow的文檔和問題,建議我應該將可擴展列表視圖的高度設置爲fill_parent
或match_parent
,但目前爲止沒有任何工作。Expandablelistview子視圖呈現兩次(儘管高度設置爲match_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);
任何人都可以幫我弄清楚這裏出了什麼問題,這將是非常棒的。先謝謝你。