Expandablelistview呈现两次子视图(尽管高度设置为match_parent)

时间:2015-10-20 00:58:10

标签: java android expandablelistview expandablelistadapter

我现在正在努力解决ExpandableListView的双重渲染问题几个小时。我查看了有关stackoverflow的文档和问题,建议我将可扩展列表视图的高度设置为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);

很高兴任何人都可以帮我弄清楚这里出了什么问题。提前谢谢。

1 个答案:

答案 0 :(得分:1)

getGroupCount()方法告诉您在ExpableListView中有多少个组, getChildrenCount(int groupPosition)方法会告诉您各个子组的数量是多少基。

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