(ExpandableListView),当我单击按钮时,更新ImageView

时间:2012-03-05 05:30:47

标签: android expandablelistview

在子视图中单击按钮,然后更新组中的imageview。 问题是当我点击按钮时,如果选中所有复选框,则imageview显示最后一个,而不是所有复选框都选中,imageview显示第二个显示第一个复选框!

MyImage

问题是当我点击按钮时,图像视图无法更新到正确的位置!

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>

1 个答案:

答案 0 :(得分:0)

将此类扩展为Activity,它将处理保存和恢复位置和扩展状态。

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