(ExpandableListView),当我点击按钮,更新的ImageView [英] (ExpandableListView),When I click button,update ImageView

查看:180
本文介绍了(ExpandableListView),当我点击按钮,更新的ImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当子视图点击按钮,然后更新组ImageView的。
问题是,当我按一下按钮,如果选择了所有复选框,则显示ImageView的最后一个,不是所有的复选框选中,ImageView的显示否则第二个显示第一个!

现在的问题是,当我按一下按钮,图像视图不能在正确的位置更新!

 包cn.nedu.exlistview;进口的java.util.ArrayList;
进口的java.util.List;
进口的java.util.Map;进口android.content.Context;
进口android.content.res.Resources;
进口android.graphics.Color;
进口android.graphics.drawable.Drawable;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.BaseExpandableListAdapter;
进口android.widget.Button;
进口android.widget.CheckBox;
进口android.widget.ImageView;
进口android.widget.TextView;
进口android.widget.Toast;类AccountExpandablebaseadapter扩展BaseExpandableListAdapter {    私人资源mResource;
    私人上下文的背景下;    清单<串GT; accountFather =新的ArrayList<串GT;();    清单<名单,LT;地图<字符串,字符串>>> accountChild =新的ArrayList<名单,LT;地图<字符串,字符串>>>();
    清单<视图> cachedGroupView =新的ArrayList<视图>();
    公共AccountExpandablebaseadapter(上下文的背景下,
            清单<串GT; accountFathers,
            清单<名单,LT;地图<字符串,字符串>>> accountChilds,资源mResouce){        this.accountFather = accountFathers;
        this.accountChild = accountChilds;
        this.mResource = mResouce;
        this.context =背景;
    }    公共查看getGroupView(INT groupPosition,布尔isExpanded,
            查看convertView,父母的ViewGroup){
        GroupViewHolder viewHolder = NULL;
        查看查看= convertView;        如果(查看== NULL){
            LayoutInflater吹气=(LayoutInflater)上下文
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            鉴于= inflater.inflate(R.layout.member_listview,NULL);
            viewHolder =新GroupViewHolder();
            viewHolder.groupTitle =(TextView中)view.findViewById(R.id.txtFather);
            / *自定义指标视图* /
            viewHolder.groupStatus =(ImageView的)view.findViewById(R.id.status);
            // RelativeLayout的tabLayout =(RelativeLayout的)view.findViewById();
            //mResource.getDrawable(R.drawable.indicator_close);
            view.setTag(viewHolder);        }其他{
            viewHolder =(GroupViewHolder)view.getTag();
        }
        cachedGroupView.add(视图); //缓存组视图
        viewHolder.groupTitle.setText(getGroup(groupPosition)的ToString());
        可绘制indicator_close = mResource.getDrawable(R.drawable.indicator_close);
        indicator_close.setBounds(0,0,indicator_close.getMinimumWidth(),indicator_close.getMinimumHeight());
        可绘制indicator_open = mResource.getDrawable(R.drawable.indicator_open);
        indicator_open.setBounds(0,0,indicator_open.getMinimumWidth(),indicator_open.getMinimumHeight());
        如果(isExpanded){
            //imageIndicator.setImageResource(R.drawable.indicator_close);
            viewHolder.groupTitle.setCompoundDrawables(indicator_close,NULL,NULL,NULL);
            viewHolder.groupTitle.setBackgroundColor(R.color.text_color_normal);
            viewHolder.groupTitle.setTextColor(mResource.getColor(R.color.text_color_click));
        }其他{
            //imageIndicator.setImageResource(R.drawable.indicator_open);
            viewHolder.groupTitle.setCompoundDrawables(indicator_open,NULL,NULL,NULL);
            viewHolder.groupTitle.setBackgroundColor(Color.TRANSPARENT);
            viewHolder.groupTitle.setTextColor(mResource.getColor(R.color.text_color_normal));        }
        返回视图。
    }    众长getGroupId(INT groupPosition){
        返回groupPosition;
    }    公共对象getGroup(INT groupPosition){
        返回accountFather.get(groupPosition)的ToString();
    }    公众诠释getGroupCount(){
        返回accountFather.size();    }    公共查看getChildView(最终诠释groupPosition,最终诠释childPosition,
            布尔isLastChild,查看convertView,父母的ViewGroup){
        查看查看= convertView;
        ChildViewHolder viewHolder = NULL;
        如果(查看== NULL){
            LayoutInflater吹气=(LayoutInflater)上下文
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            鉴于= inflater.inflate(R.layout.member_childitem2,NULL);
            viewHolder =新ChildViewHolder();            viewHolder.checkBox1 =(复选框)view.findViewById(R.id.checkbox1);
            viewHolder.checkBox2 =(复选框)view.findViewById(R.id.checkbox2);
            viewHolder.checkBox3 =(复选框)view.findViewById(R.id.checkbox3);
            viewHolder.checkBox4 =(复选框)view.findViewById(R.id.checkbox4);
            viewHolder.checkBox5 =(复选框)view.findViewById(R.id.checkbox5);
            viewHolder.checkBox6 =(复选框)view.findViewById(R.id.checkbox6);
            view.setTag(viewHolder);
        }其他{
            viewHolder =(ChildViewHolder)view.getTag();
        }        按钮btn_submit =(按钮)view.findViewById(R.id.btn_submit);
        查看groupView = cachedGroupView.get(childPosition);
        最终GroupViewHolder groupViewHolder =(GroupViewHolder)groupView.getTag();
        btn_submit.setOnClickListener(新View.OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根
                Toast.makeText(背景下,
                        groupPosition +:
                                + childPosition,
                        Toast.LENGTH_SHORT).show();                groupViewHolder.groupStatus.setImageResource(R.drawable.indicator_close);
            }
        });
        返回视图。
    }    众长getChildId(INT groupPosition,诠释childPosition){
        返回childPosition;
    }    公共对象getChild(INT groupPosition,诠释childPosition){
        返回accountChild.get(groupPosition)获得(childPosition)
                获得(ExListView.FATHERACCOUNT)的ToString();
    }    公众诠释getChildrenCount(INT groupPosition){
        返回accountChild.get(groupPosition).size();
    }    公共布尔hasStableIds(){
        返回true;
    }    公共布尔isChildSelectable(INT groupPosition,诠释childPosition){
        返回true;
    }
    类GroupViewHolder {
        TextView的groupTitle;
        ImageView的groupStatus;
    }
    类ChildViewHolder {        复选框checkBox1;
        复选框checkBox2;
        复选框checkBox3;
        复选框checkBox4;
        复选框checkBox5;
        复选框checkBox6;
    }}
包cn.nedu.exlistview;进口的java.util.ArrayList;
进口的java.util.HashMap;
进口的java.util.List;
进口的java.util.Map;进口android.app.Activity;
进口android.content.Context;
进口android.content.res.Resources;
进口android.os.Bundle;
进口android.os.StrictMode;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.view.Window;
进口android.view.View.OnClickListener;
进口android.widget.AbsListView;
进口android.widget.BaseExpandableListAdapter;
进口android.widget.ExpandableListView;
进口android.widget.ExpandableListView.OnGroupClickListener;
进口android.widget.ExpandableListView.OnGroupExpandListener;
进口android.widget.ImageView;
进口android.widget.LinearLayout;
进口android.widget.TextView;
进口android.widget.Toast;
进口android.widget.ExpandableListView.OnChildClickListener;公共类ExListView延伸活动{    最终静态字符串FATHERACCOUNT =fatherAccount;
    最终静态字符串CHILDACCOUNT =childAccount;
    最终静态字符串BALANCEACCOUNT =accountBalance;
    私人INT currentHourPosition = -1;
    清单<串GT; accountFather =新的ArrayList<串GT;();    清单<名单,LT;地图<字符串,字符串>>> accountChild =新的ArrayList<名单,LT;地图<字符串,字符串>>>();    AccountExpandablebaseadapter适配器;
    ExpandableListView listAccount;
    私人资源mResouce;    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        // StriceMode
        StrictMode.setThreadPolicy(新StrictMode.ThreadPolicy.Builder()
                。.detectAll()penaltyLog()penaltyDialog()建())。;        requestWindowFeature(Window.FEATURE_NO_TITLE);
        的setContentView(R.layout.main);
        mResouce = getResources();
        listAccount =(ExpandableListView)findViewById(R.id.listAccount);
        accountFather.add(GroupName1);
        accountFather.add(GroupName2);
        accountFather.add(GroupName3);        清单<地图<字符串,字符串>> financeAccount =新的ArrayList<地图<字符串,字符串>>();
        地图<字符串,字符串> child1Data1 =新的HashMap<字符串,字符串>();
        child1Data1.put(FATHERACCOUNTwwwwww));
        child1Data1.put(CHILDACCOUNTwwwwww);
        child1Data1.put(BALANCEACCOUNT,¥0.00);
        financeAccount.add(child1Data1);        清单<地图<字符串,字符串>> dummyAccount =新的ArrayList<地图<字符串,字符串>>();
        地图<字符串,字符串> child2Data1 =新的HashMap<字符串,字符串>();
        child2Data1.put(FATHERACCOUNTZZZZZ);
        child2Data1.put(CHILDACCOUNTZZZZZZ);
        child2Data1.put(BALANCEACCOUNT,¥0.00);
        dummyAccount.add(child2Data1);
        清单<地图<字符串,字符串>> cashAccount =新的ArrayList<地图<字符串,字符串>>();
        地图<字符串,字符串> child3Data1 =新的HashMap<字符串,字符串>();
        child3Data1.put(FATHERACCOUNTXXXXXX);
        child3Data1.put(CHILDACCOUNTEEEEEEE);
        child3Data1.put(BALANCEACCOUNT,¥0.00);
        cashAccount.add(child3Data1);
        清单<地图<字符串,字符串>> creditorAccount =新的ArrayList<地图<字符串,字符串>>();
        地图<字符串,字符串> child4Data1 =新的HashMap<字符串,字符串>();        清单<地图<字符串,字符串>> owesAccount =新的ArrayList<地图<字符串,字符串>>();
        地图<字符串,字符串> child5Data1 =新的HashMap<字符串,字符串>();        accountChild.add(financeAccount);
        accountChild.add(dummyAccount);
        accountChild.add(cashAccount);        适配器=新AccountExpandablebaseadapter(ExListView.this,
                accountFather,accountChild,mResouce);
        listAccount.setAdapter(适配器);
        listAccount.setGroupIndicator(NULL);
        listAccount.setDivider(NULL);        listAccount.setOnChildClickListener(新OnChildClickListener(){            @覆盖
            公共布尔onChildClick(ExpandableListView父视图V,
                    INT groupPosition,诠释childPosition,长ID){
                / *
                 * Toast.makeText(getBaseContext(),
                 *将String.valueOf(groupPosition)+:+
                 *将String.valueOf(childPosition),Toast.LENGTH_SHORT).show();
                 * /
                返回false;
            }
        });
        //
        listAccount.setOnGroupClickListener(新OnGroupClickListener(){            @覆盖
            公共布尔onGroupClick(ExpandableListView父视图V,
                    INT groupPosition,长ID){                返回false;
            }
        });        listAccount.setOnGroupExpandListener(新OnGroupExpandListener(){            @覆盖
            公共无效onGroupExpand(INT groupPosition){
                如果(currentHourPosition!= -1
                        &功放;&安培; currentHourPosition!= groupPosition){
                    listAccount.collapseGroup(currentHourPosition);
                }
                currentHourPosition = groupPosition;
                listAccount.setSelectedGroup(groupPosition);
            }
        });
    }}
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直
    机器人:背景=#003300>    < ExpandableListView
        机器人:ID =@ + ID / listAccount
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:groupIndicator =@空
        机器人:滚动条=无
        />< / LinearLayout中>< XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=横向>    < RelativeLayout的
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT        机器人:layout_alignParentBottom =真
        机器人:ID =@ + ID / tabLayout>        < ImageView的
            机器人:ID =@ + ID /指示器
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:contentDescription =@字符串/ APP_NAME
            机器人:知名度=水涨船高/>        <的TextView
            机器人:ID =@ + ID / txtFather
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_gravity =中心
            机器人:layout_marginLeft =6DP
            机器人:layout_toRightOf =@ ID /指示器
            机器人:文字颜色=#000000
            机器人:TEXTSIZE =16DP
             />
    < / RelativeLayout的>    < ImageView的
        机器人:ID =@ + ID /状态
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentRight =真
        机器人:contentDescription =@字符串/ APP_NAME
        机器人:SRC =@绘制/图标/>< / RelativeLayout的>< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:背景=#FFFFFF
    机器人:方向=垂直>    <复选框
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:ID =@ + ID / checkbox1
        机器人:文字=Checkbox1
        机器人:按钮=@绘制/ checkbox_bg
        机器人:文字颜色=#000000/>    <复选框
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:ID =@ + ID / checkbox2
        机器人:文字=Checkbox2
        机器人:按钮=@绘制/ checkbox_bg
        机器人:文字颜色=#000000/>    <复选框
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:ID =@ + ID / checkbox3
        机器人:文字=Checkbox3
        机器人:文字颜色=#000000
        机器人:按钮=@绘制/ checkbox_bg/>    <复选框
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字颜色=#000000
        机器人:ID =@ + ID / checkbox4
        机器人:文字=Checkbox4
        机器人:按钮=@绘制/ checkbox_bg/>    <复选框
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字颜色=#000000
        机器人:ID =@ + ID / checkbox5
        机器人:文字=Checkbox5
        机器人:按钮=@绘制/ checkbox_bg/>    <复选框
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字颜色=#000000
        机器人:ID =@ + ID / checkbox6
        机器人:文字=Checkbox6
        机器人:按钮=@绘制/ checkbox_bg/>    <的LinearLayout
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_gravity =CENTER_HORIZONTAL
        机器人:方向=横向>        <按钮
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文字=拍照/>        <按钮
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文字=提交
            机器人:ID =@ + ID / btn_submit/>        <按钮
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文字=取消
            机器人:ID =@ + ID / btn_cancel/>
    < / LinearLayout中>< / LinearLayout中>


解决方案

扩展此类作为你的活动,它会处理保存和恢复地位和扩张状态。

 进口的java.util.ArrayList;
进口的java.util.List;进口android.app.ExpandableListActivity;
进口android.os.Bundle;
进口android.widget.ExpandableListAdapter;
进口android.widget.ExpandableListView;公共类PersistentExpandableListActivity扩展ExpandableListActivity {
私人长期[] expandedIds;公共PersistentExpandableListActivity(){
    超();
}@覆盖
保护无效调用onStart(){
    super.onStart();
    如果(this.expandedIds!= NULL){
        restoreExpandedState(expandedIds);
    }
}@覆盖
保护无效的onStop(){
    super.onStop();
    expandedIds = getExpandedIds();
}@覆盖
保护无效的onSaveInstanceState(捆绑outState){
    super.onSaveInstanceState(outState);
    this.expandedIds = getExpandedIds();
    outState.putLongArray(ExpandedIds,this.expandedIds);
}@覆盖
保护无效onRestoreInstanceState(包状态){
    super.onRestoreInstanceState(州);
    长[] = expandedIds state.getLongArray(ExpandedIds);
    如果(expandedIds!= NULL){
        restoreExpandedState(expandedIds);
    }
}私人长期[] getExpandedIds(){
    ExpandableListView列表= getExpandableListView();
    ExpandableListAdapter适配器= getExpandableListAdapter();
    如果(适配器!= NULL){
        INT长度= adapter.getGroupCount();
        ArrayList的<龙> expandedIds =新的ArrayList<龙>();
        的for(int i = 0; I<长度;我++){
            如果(list.isGroupExpanded(ⅰ)){
                expandedIds.add(adapter.getGroupId(I));
            }
        }
        返回toLongArray(expandedIds);
    }其他{
        返回null;
    }
}私人无效restoreExpandedState(长[] expandedIds){
    this.expandedIds = expandedIds;
    如果(expandedIds!= NULL){
        ExpandableListView列表= getExpandableListView();
        ExpandableListAdapter适配器= getExpandableListAdapter();
        如果(适配器!= NULL){
            的for(int i = 0; I< adapter.getGroupCount();我++){
                长的id = adapter.getGroupId(ⅰ);
                如果(inArray(expandedIds,ID))list.expandGroup(ⅰ);
            }
        }
    }
}私有静态布尔inArray(长[]数组,长素){
    为(长L:数组){
        如果(L ==元素){
            返回true;
        }
    }
    返回false;
}私有静态长[] toLongArray(列表<龙>名单){
    长[] = RET新长[则为list.size()];
    INT I = 0;
    为(长E:名单)
        RET [我++] = e.longValue();
    返回RET;
}
}

when click button in child view then update imageview in group. The problem is when I click the button, if all the checkbox selected,the imageview show the last one, not all checkbox selected, the imageview show the second one else show the first one!

The question is when I click the button,the image view cannot update in the correct position!

package cn.nedu.exlistview;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

class AccountExpandablebaseadapter extends BaseExpandableListAdapter {

    private Resources mResource;
    private Context context;

    List<String> accountFather = new ArrayList<String>();

    List<List<Map<String, String>>> accountChild = new ArrayList<List<Map<String, String>>>();
    List<View> cachedGroupView = new ArrayList<View>();
    public AccountExpandablebaseadapter(Context context,
            List<String> accountFathers,
            List<List<Map<String, String>>> accountChilds,Resources mResouce) {

        this.accountFather = accountFathers;
        this.accountChild = accountChilds;
        this.mResource = mResouce;
        this.context = context;
    }

    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        GroupViewHolder viewHolder = null;
        View view = convertView;

        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.member_listview, null);


            viewHolder = new GroupViewHolder();
            viewHolder.groupTitle = (TextView) view.findViewById(R.id.txtFather);
            /*customize indicator view*/
            viewHolder.groupStatus = (ImageView) view.findViewById(R.id.status);
            //RelativeLayout tabLayout = (RelativeLayout)view.findViewById();
            //mResource.getDrawable(R.drawable.indicator_close);
            view.setTag(viewHolder);

        }else{
            viewHolder = (GroupViewHolder) view.getTag();
        }
        cachedGroupView.add(view); //cached group view
        viewHolder.groupTitle.setText(getGroup(groupPosition).toString());
        Drawable indicator_close = mResource.getDrawable(R.drawable.indicator_close);
        indicator_close.setBounds(0, 0, indicator_close.getMinimumWidth(), indicator_close.getMinimumHeight());
        Drawable indicator_open = mResource.getDrawable(R.drawable.indicator_open);
        indicator_open.setBounds(0, 0, indicator_open.getMinimumWidth(), indicator_open.getMinimumHeight());
        if (isExpanded) {
            //imageIndicator.setImageResource(R.drawable.indicator_close);
            viewHolder.groupTitle.setCompoundDrawables(indicator_close, null, null, null);
            viewHolder.groupTitle.setBackgroundColor(R.color.text_color_normal);
            viewHolder.groupTitle.setTextColor(mResource.getColor(R.color.text_color_click));
        }else{
            //imageIndicator.setImageResource(R.drawable.indicator_open);
            viewHolder.groupTitle.setCompoundDrawables(indicator_open, null, null, null);
            viewHolder.groupTitle.setBackgroundColor(Color.TRANSPARENT);
            viewHolder.groupTitle.setTextColor(mResource.getColor(R.color.text_color_normal));

        }


        return view;
    }

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

    public Object getGroup(int groupPosition) {
        return accountFather.get(groupPosition).toString();
    }

    public int getGroupCount() {
        return accountFather.size();

    }

    public View getChildView(final int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        View view = convertView;
        ChildViewHolder viewHolder = null;
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.member_childitem2, null);
            viewHolder = new ChildViewHolder();



            viewHolder.checkBox1 = (CheckBox)view.findViewById(R.id.checkbox1);
            viewHolder.checkBox2 = (CheckBox)view.findViewById(R.id.checkbox2);
            viewHolder.checkBox3 = (CheckBox)view.findViewById(R.id.checkbox3);
            viewHolder.checkBox4 = (CheckBox)view.findViewById(R.id.checkbox4);
            viewHolder.checkBox5 = (CheckBox)view.findViewById(R.id.checkbox5);
            viewHolder.checkBox6 = (CheckBox)view.findViewById(R.id.checkbox6);


            view.setTag(viewHolder);
        }else{
            viewHolder = (ChildViewHolder) view.getTag();
        }

        Button btn_submit = (Button)view.findViewById(R.id.btn_submit);


        View groupView = cachedGroupView.get(childPosition);
        final GroupViewHolder groupViewHolder = (GroupViewHolder) groupView.getTag();
        btn_submit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(context,
                        groupPosition+ ":"
                                + childPosition,
                        Toast.LENGTH_SHORT).show();

                groupViewHolder.groupStatus.setImageResource(R.drawable.indicator_close);
            }
        });


        return view;
    }

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

    public Object getChild(int groupPosition, int childPosition) {
        return accountChild.get(groupPosition).get(childPosition)
                .get(ExListView.FATHERACCOUNT).toString();
    }

    public int getChildrenCount(int groupPosition) {
        return accountChild.get(groupPosition).size();
    }

    public boolean hasStableIds() {
        return true;
    }

    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }


    class GroupViewHolder{
        TextView groupTitle;
        ImageView groupStatus;
    }
    class ChildViewHolder{

        CheckBox checkBox1;
        CheckBox checkBox2;
        CheckBox checkBox3;
        CheckBox checkBox4;
        CheckBox checkBox5;
        CheckBox checkBox6;
    }

}




package cn.nedu.exlistview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ExpandableListView.OnChildClickListener;

public class ExListView extends Activity {

    final static String FATHERACCOUNT = "fatherAccount";
    final static String CHILDACCOUNT = "childAccount";
    final static String BALANCEACCOUNT = "accountBalance";
    private int currentHourPosition = -1;
    List<String> accountFather = new ArrayList<String>();

    List<List<Map<String, String>>> accountChild = new ArrayList<List<Map<String, String>>>();

    AccountExpandablebaseadapter adapter;
    ExpandableListView listAccount;
    private Resources mResouce;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // StriceMode
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectAll().penaltyLog().penaltyDialog().build());

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        mResouce = getResources();
        listAccount = (ExpandableListView) findViewById(R.id.listAccount);


        accountFather.add("GroupName1");
        accountFather.add("GroupName2");
        accountFather.add("GroupName3");

        List<Map<String, String>> financeAccount = new ArrayList<Map<String, String>>();
        Map<String, String> child1Data1 = new HashMap<String, String>();
        child1Data1.put(FATHERACCOUNT, "wwwwww)");
        child1Data1.put(CHILDACCOUNT, "wwwwww");
        child1Data1.put(BALANCEACCOUNT, "¥0.00");
        financeAccount.add(child1Data1);

        List<Map<String, String>> dummyAccount = new ArrayList<Map<String, String>>();
        Map<String, String> child2Data1 = new HashMap<String, String>();
        child2Data1.put(FATHERACCOUNT, "zzzzz");
        child2Data1.put(CHILDACCOUNT, "zzzzzz");
        child2Data1.put(BALANCEACCOUNT, "¥0.00");
        dummyAccount.add(child2Data1);
        List<Map<String, String>> cashAccount = new ArrayList<Map<String, String>>();
        Map<String, String> child3Data1 = new HashMap<String, String>();
        child3Data1.put(FATHERACCOUNT, "xxxxxx");
        child3Data1.put(CHILDACCOUNT, "eeeeeee");
        child3Data1.put(BALANCEACCOUNT, "¥0.00");
        cashAccount.add(child3Data1);
        List<Map<String, String>> creditorAccount = new ArrayList<Map<String, String>>();
        Map<String, String> child4Data1 = new HashMap<String, String>();

        List<Map<String, String>> owesAccount = new ArrayList<Map<String, String>>();
        Map<String, String> child5Data1 = new HashMap<String, String>();

        accountChild.add(financeAccount);
        accountChild.add(dummyAccount);
        accountChild.add(cashAccount);

        adapter = new AccountExpandablebaseadapter(ExListView.this,
                accountFather, accountChild, mResouce);
        listAccount.setAdapter(adapter);
        listAccount.setGroupIndicator(null);
        listAccount.setDivider(null);

        listAccount.setOnChildClickListener(new OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                /*
                 * Toast.makeText( getBaseContext(),
                 * String.valueOf(groupPosition) + ":" +
                 * String.valueOf(childPosition), Toast.LENGTH_SHORT).show();
                 */
                return false;
            }
        });
        //
        listAccount.setOnGroupClickListener(new OnGroupClickListener() {

            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                    int groupPosition, long id) {

                return false;
            }
        });

        listAccount.setOnGroupExpandListener(new OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPosition) {
                if (currentHourPosition != -1
                        && currentHourPosition != groupPosition) {
                    listAccount.collapseGroup(currentHourPosition);
                }
                currentHourPosition = groupPosition;
                listAccount.setSelectedGroup(groupPosition);
            }
        });
    }

}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:background="#003300">

    <ExpandableListView
        android:id="@+id/listAccount"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:groupIndicator="@null"
        android:scrollbars="none"
        />

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_alignParentBottom="true"
        android:id="@+id/tabLayout">

        <ImageView
            android:id="@+id/indicator"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/app_name"
            android:visibility="gone" />

        <TextView
            android:id="@+id/txtFather"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="6dp"
            android:layout_toRightOf="@id/indicator"
            android:textColor="#000000"
            android:textSize="16dp"
             />
    </RelativeLayout>

    <ImageView
        android:id="@+id/status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:contentDescription="@string/app_name"
        android:src="@drawable/icon" />

</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:orientation="vertical" >

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/checkbox1"
        android:text="Checkbox1"
        android:button="@drawable/checkbox_bg"
        android:textColor="#000000" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/checkbox2"
        android:text="Checkbox2"
        android:button="@drawable/checkbox_bg"
        android:textColor="#000000" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/checkbox3"
        android:text="Checkbox3"
        android:textColor="#000000"
        android:button="@drawable/checkbox_bg" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:id="@+id/checkbox4"
        android:text="Checkbox4"
        android:button="@drawable/checkbox_bg" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:id="@+id/checkbox5"
        android:text="Checkbox5"
        android:button="@drawable/checkbox_bg" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:id="@+id/checkbox6"
        android:text="Checkbox6"
        android:button="@drawable/checkbox_bg" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Take Picture" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Submit"
            android:id="@+id/btn_submit" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cancel"
            android:id="@+id/btn_cancel" />
    </LinearLayout>

</LinearLayout>

解决方案

Extend this class as your Activity and it will handle saving and restoring position and expansion state.

import java.util.ArrayList;
import java.util.List;

import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;

public class PersistentExpandableListActivity extends ExpandableListActivity {
private long[] expandedIds;

public PersistentExpandableListActivity() {
    super();
}

@Override
protected void onStart() {
    super.onStart();
    if (this.expandedIds != null) {
        restoreExpandedState(expandedIds);
    }
}

@Override
protected void onStop() {
    super.onStop();
    expandedIds = getExpandedIds();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    this.expandedIds = getExpandedIds();
    outState.putLongArray("ExpandedIds", this.expandedIds);
}

@Override
protected void onRestoreInstanceState(Bundle state) {
    super.onRestoreInstanceState(state);
    long[] expandedIds = state.getLongArray("ExpandedIds");
    if (expandedIds != null) {
        restoreExpandedState(expandedIds);
    }
}

private long[] getExpandedIds() {
    ExpandableListView list = getExpandableListView();
    ExpandableListAdapter adapter = getExpandableListAdapter();
    if (adapter != null) {
        int length = adapter.getGroupCount();
        ArrayList<Long> expandedIds = new ArrayList<Long>();
        for(int i=0; i < length; i++) {
            if(list.isGroupExpanded(i)) {
                expandedIds.add(adapter.getGroupId(i));
            }
        }
        return toLongArray(expandedIds);
    } else {
        return null;
    }
}

private void restoreExpandedState(long[] expandedIds) {
    this.expandedIds = expandedIds;
    if (expandedIds != null) {
        ExpandableListView list = getExpandableListView();
        ExpandableListAdapter adapter = getExpandableListAdapter();
        if (adapter != null) {
            for (int i=0; i<adapter.getGroupCount(); i++) {
                long id = adapter.getGroupId(i);
                if (inArray(expandedIds, id)) list.expandGroup(i);
            }
        }
    }
}

private static boolean inArray(long[] array, long element) {
    for (long l : array) {
        if (l == element) {
            return true;
        }
    }
    return false;
}

private static long[] toLongArray(List<Long> list)  {
    long[] ret = new long[list.size()];
    int i = 0;
    for (Long e : list)  
        ret[i++] = e.longValue();
    return ret;
}
}

这篇关于(ExpandableListView),当我点击按钮,更新的ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆