BaseExpandableListAdapter convertview 从 getChildView() 返回旧视图

Posted

技术标签:

【中文标题】BaseExpandableListAdapter convertview 从 getChildView() 返回旧视图【英文标题】:BaseExpandableListAdapter convertview returns old view from getChildView() 【发布时间】:2016-07-31 18:21:01 【问题描述】:

我对 BaseExpandableListAdapter 概念有混淆,下面解释了我的问题。

预期:

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

Group1-ReceivedRequest:

child1 - 来自 Layout1

child2 - 来自 Layout1

Group2-ReceivedSent:

child1 - 来自 Layout2

child2 - 来自 Layout2

问题

当我先单击 Group1 然后单击 Group2 时,它的工作完美(即第一次在相应组上调用时 convertview 为空,因此它以正确的方式膨胀),

但是当我先点击 Group2 然后点击 Group1 它仍然返回旧的 Group2 视图...(即 convertview 应该为空,但它的返回旧视图)

请告诉我问题出在哪里.. 我在 *** 上找到了确切的问题 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】:

我只是回答自己的问题,以便对其他人有所帮助,我想手动覆盖该方法 getChildTypeCount()getChildType(int groupPosition, int childPosition) 以防万一您有不同的子布局,请在下面找到我的更新代码。

@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;

【讨论】:

以上是关于BaseExpandableListAdapter convertview 从 getChildView() 返回旧视图的主要内容,如果未能解决你的问题,请参考以下文章

BaseExpandableListAdapter convertview 从 getChildView() 返回旧视图

BaseExpandableListAdapter 为每个组位置返回相同的子级

ExpandableListView总结

ExpandableListView 点击事件

Android:ExpandableListView使用

ExpandableListView二级列表