BaseExpandableListAdapter convertview从getChildView()

时间:2016-04-10 19:08:59

标签: android android-layout listview android-fragments android-studio

我对BaseExpandableListAdapter概念感到困惑,解释了我的问题。

期望:

例如,我有两个小组,每个小组都有不同的布局,如下所示

  • Group1 -ReceivedRequest:

    child1 - 来自Layout1

    child2 - 来自Layout1

  • 组2 -ReceivedSent:

    child1 - 来自Layout2

    child2 - 来自Layout2

问题

当我首先点击 Group1 然后 Group2 时,它的工作完美(例如,当第一次在相应的群组上呼叫时,convertview为空,因此它正在以正确的方式充气),

但是,当我首先点击 Group2 然后 Group1 时,它仍会返回旧的 Group2 视图...(即convertview应为null但是回归旧观点)

请告诉我问题在哪里.. 我在Stackoverflow上找到了确切的问here,但还没有解决方案

以下是我的代码

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        SingleRow singleRow = new SingleRow();
        RequestInfo Req_child = (RequestInfo) getChild(groupPosition,childPosition);
        String type = Req_child.requestertype;
        if(convertView==null) {

            switch (type)
            {
                case RequestInfo.REQ_RECEIVED_ACCOUNT:

                    LayoutInflater infalInflater = (LayoutInflater) context
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = infalInflater.inflate(R.layout.row_reqlist_acctrcvd, null);
                    convertView.setTag( singleRow);
                    break;

                case RequestInfo.REQ_SENTACCOUNT:

                    LayoutInflater infalInflater1 = (LayoutInflater) context
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = infalInflater1.inflate(R.layout.row_reqlist_acctsent, null);
                    convertView.setTag(singleRow);
                    break;
                    }
        }

        singleRow = (SingleRow) convertView.getTag();
        switch (type)
        {
            case RequestInfo.REQ_RECEIVED_ACCOUNT:
                singleRow.tvUsername_req_rcd = (TextView) convertView.findViewById(R.id.rw_reqaccts_rcvd_username);
                singleRow.tvNickName_req_rcd = (TextView) convertView.findViewById(R.id.rw_reqaccts_rcvd_nickname);
                singleRow.im_gender_req_rcd = (ImageView) convertView.findViewById(R.id.rw_reqaccts_rcvd_profpic);
                singleRow.btAcceptReq = (Button) convertView.findViewById(R.id.rw_reqaccts_rcvd_accpt);
                singleRow.btRjctReq = (Button) convertView.findViewById(R.id.rw_reqaccts_rcvd_rjct);

                singleRow.tvUsername_req_rcd.setText(Req_child.requesterUsername);
                singleRow.tvNickName_req_rcd.setText(Req_child.requesterNickName);
                singleRow.im_gender_req_rcd.setImageBitmap(Req_child.requestergender.equals("Male") ? rmale_pic : rfemale_pic);
                Log.i("Expand", "your child username is " + Req_child.requesterUsername);

            break;

            case RequestInfo.REQ_SENTACCOUNT:
                singleRow.tvUsername_req_sent = (TextView) convertView.findViewById(R.id.rw_reqaccts_sent_username);
                singleRow.tvNickName_req_sent = (TextView) convertView.findViewById(R.id.rw_reqaccts_sent_nickname);
                singleRow.im_gender_req_sent = (ImageView) convertView.findViewById(R.id.rw_reqaccts_sent_profpic);
                singleRow.btReqSent = (Button) convertView.findViewById(R.id.rw_btn_sent_reqaccts);

                singleRow.tvUsername_req_sent.setText(Req_child.requesterUsername);
                singleRow.tvNickName_req_sent.setText(Req_child.requesterNickName);
                singleRow.im_gender_req_sent.setImageBitmap(Req_child.requestergender.equals("Male") ? rmale_pic : rfemale_pic);
                Log.i("Expand", "your child username is " + Req_child.requesterUsername);

        }
        return convertView;
    }

1 个答案:

答案 0 :(得分:0)

我只是回答我自己的问题,以便它可以帮助其他人,我想手动覆盖方法 getChildTypeCount() getChildType(int groupPosition,int childPosition) incase你有不同的子布局,找到我下面更新的代码。

@Override
public int getChildTypeCount() {
    return 2;
}

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


@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    SingleRow singleRow = null;
    RequestInfo Req_child = childtems.get(groupPosition).get(childPosition);
    String type = Req_child.requestertype;
    if(convertView==null ) {
        switch (type)
        {
            case RequestInfo.REQ_RECEIVED_ACCOUNT:
                 singleRow = new SingleRow();
                    convertView = inflater.inflate(R.layout.row_reqlist_acctrcvd, null);
                    singleRow.tvUsername_req_rcd = (TextView) convertView.findViewById(R.id.rw_reqaccts_rcvd_username);
                    singleRow.tvNickName_req_rcd = (TextView) convertView.findViewById(R.id.rw_reqaccts_rcvd_nickname);
                    singleRow.im_gender_req_rcd = (ImageView) convertView.findViewById(R.id.rw_reqaccts_rcvd_profpic);
                    singleRow.btAcceptReq = (Button) convertView.findViewById(R.id.rw_reqaccts_rcvd_accpt);
                    singleRow.btRjctReq = (Button) convertView.findViewById(R.id.rw_reqaccts_rcvd_rjct);
                    convertView.setTag(singleRow);
                break;

            case RequestInfo.REQ_SENTACCOUNT:
                 singleRow = new SingleRow();
                    convertView = inflater.inflate(R.layout.row_reqlist_acctsent, null);
                    singleRow.tvUsername_req_sent = (TextView) convertView.findViewById(R.id.rw_reqaccts_sent_username);
                    singleRow.tvNickName_req_sent = (TextView) convertView.findViewById(R.id.rw_reqaccts_sent_nickname);
                    singleRow.im_gender_req_sent = (ImageView) convertView.findViewById(R.id.rw_reqaccts_sent_profpic);
                    singleRow.btReqSent = (Button) convertView.findViewById(R.id.rw_btn_sent_reqaccts);
                    convertView.setTag(singleRow);
                break;
                }
    }
    else
    {
        singleRow = (SingleRow) convertView.getTag();
    }

    switch (type)
    {
        case RequestInfo.REQ_RECEIVED_ACCOUNT:
            singleRow.tvUsername_req_rcd.setText(Req_child.requesterUsername);
            singleRow.tvNickName_req_rcd.setText(Req_child.requesterNickName);
            singleRow.im_gender_req_rcd.setImageBitmap(Req_child.requestergender.equals("Male") ? rmale_pic : rfemale_pic);
        break;

        case RequestInfo.REQ_SENTACCOUNT:
            singleRow.tvUsername_req_sent.setText(Req_child.requesterUsername);
            singleRow.tvNickName_req_sent.setText(Req_child.requesterNickName);
            singleRow.im_gender_req_sent.setImageBitmap(Req_child.requestergender.equals("Male") ? rmale_pic : rfemale_pic);
    }
    return convertView;
}