samedi 14 mai 2016

checkbox in ExpandableListView

I am creating an Android App , but now I meet a hard problem for me. There are checkboxs both in group and child,and there are two type view for the group.

Now I want to make it like when I click the checkbox in child ,it will be checked and its group with be also checked ,at the same time ,other group will not be checked and their child will be unchecked . It means the group is single choose,only one group can be choosed. I use a PayWayBean to save the information for it.

But now I meet a problem is ,when I click the secord group , it will expand its child ,when I check the checkbox in the child ,it will be checked but when I click another group it will be unchecked !!! Just like that A video I record

And the another problem is sometimes when I checked the second group's child , child's parent( is the group ) will be alse checked , so the other group will be unchecked , but it now work sometimes ,look like the notifyDataSetChanged not be call but it was called actually another bug video

that is my code Adapter:

public class PayWayAdapter extends BaseExpandableListAdapter{

private Context mContext;
private LayoutInflater mLayoutInflater;

private static int ECARD_TYPE = 0;
private static int THIRD_WAY_TYPE =1;
private static int TYPE_COUNT = 2;
private static int THIRD_WAY_COUNT = 2;

private ArrayList<PayWayBean> mPayWayList = new ArrayList<>();

public void setmOnCheckListener(onChooseListener onChooseListener) {
    this.mOnChooseListener = onChooseListener;
}

private onChooseListener mOnChooseListener;

public interface onChooseListener{
    void choose(int groupPosition , int childPosition );
}

public PayWayAdapter(Context context , ArrayList<PayWayBean> list){
    this.mContext = context;
    this.mPayWayList = list;
    mLayoutInflater = LayoutInflater.from(context);

}

@Override
public int getGroupCount() {
    return mPayWayList.size();
}

@Override
public int getChildrenCount(int groupPosition) {
    return mPayWayList.get(groupPosition).getDeposits().size();
}

@Override
public Object getGroup(int groupPosition) {
    return mPayWayList.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return mPayWayList.get(groupPosition).getDeposits().get(childPosition);
}

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

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

@Override
public boolean hasStableIds() {
    return true;
}

@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    ParentViewHolder mParentViewHolder = null;


    switch (getGroupType(groupPosition)){
        case 0:
            if (convertView == null){
                convertView = mLayoutInflater.inflate(R.layout.single_payway_parent,parent,false);
                mParentViewHolder = new ParentViewHolder();
            }else {
                mParentViewHolder = (ParentViewHolder) convertView.getTag();
            }
            mParentViewHolder.mPayWay = (TextView) convertView.findViewById(R.id.parent_check_pay_way_text);
            mParentViewHolder.mCheckbox = (CheckBox) convertView.findViewById(R.id.parent_check_pay_way_radio_button);
            mParentViewHolder.mCheckbox.setChecked(mPayWayList.get(groupPosition).isCheck());

            String cardNum = mPayWayList.get(groupPosition).getEcardNo();
            mParentViewHolder.mPayWay.setText(cardNum);

            convertView.setTag(mParentViewHolder);
            break;
        case 1:
            if (convertView == null){
                convertView = mLayoutInflater.inflate(R.layout.single_payway_parent_another,parent,false);
                mParentViewHolder = new ParentViewHolder();
            }else {
                mParentViewHolder = (ParentViewHolder) convertView.getTag();
            }

            mParentViewHolder.mCheckbox = (CheckBox) convertView.findViewById(R.id.order_pay_way_checkbox);
            mParentViewHolder.mCheckbox.setText(mPayWayList.get(groupPosition).getEcardNo());
            mParentViewHolder.mCheckbox.setChecked(mPayWayList.get(groupPosition).isCheck());
            setIcon(mParentViewHolder.mCheckbox,mPayWayList.get(groupPosition).getPayWayIcon());

            convertView.setTag(mParentViewHolder);
            break;
        default:
            break;
    }



    mParentViewHolder.mCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Log.d("Adapter","groupposition "+groupPosition+" is check "+isChecked);

            if (isChecked){
                //current parent was checked 
                for (int i = 0; i < mPayWayList.size(); i++) {
                    if (i == groupPosition){
                        //set the current parent checked 
                        mPayWayList.get(i).setCheck(true);
                    }else {
                        //set other uncle unchecked 
                        mPayWayList.get(i).setCheck(false);
                        //set other uncle's child unchecked 
                        for(int k = 0; k<mPayWayList.get(i).getDeposits().size();k++){
                            mPayWayList.get(i).getDeposits().get(k).setCheck(false);
                        }
                    }
                }

            }else {
                //todo 
            }
            notifyDataSetChanged();

        }
    });

    return convertView;
}

@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    ChildViewHolder mChildViewHolder;
    if (convertView == null){
        convertView = mLayoutInflater.inflate(R.layout.single_payway_child,parent,false);
        mChildViewHolder = new ChildViewHolder();
    }else {
        mChildViewHolder = (ChildViewHolder) convertView.getTag();
    }

    mChildViewHolder.mProductName = (TextView) convertView.findViewById(R.id.child_card_name);
    mChildViewHolder.mCheckbox = (CheckBox) convertView.findViewById(R.id.child_check_order_pay_way);

    PayWayBean.Deposits tmpList = mPayWayList.get(groupPosition).getDeposits().get(childPosition);
    mChildViewHolder.mProductName.setText(tmpList.getProductName());
    mChildViewHolder.mCheckbox.setText(""+tmpList.getBalance()/100);
    mChildViewHolder.mCheckbox.setChecked(tmpList.isCheck());



    //set when a child was checked , its parent will be checked 
    mChildViewHolder.mCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


            mPayWayList.get(groupPosition).getDeposits().get(childPosition).setCheck(isChecked);
            if (isChecked){
                //current child be checked ,set its parent be checked 
                mPayWayList.get(groupPosition).setCheck(true);
            }else {
                //current child not be checked , find is its brother was checked ,if its brocher was checked ,it parent should be checked 
                for (int i = 0; i < mPayWayList.get(groupPosition).getDeposits().size(); i++) {
                    if (mPayWayList.get(groupPosition).getDeposits().get(i).isCheck()){
                        mPayWayList.get(groupPosition).setCheck(true);
                        break;
                    }else {
                        mPayWayList.get(groupPosition).setCheck(false);
                    }
                }
            }

            notifyDataSetChanged();
        }
    });

    convertView.setTag(mChildViewHolder);
    return convertView;
}

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

private class ParentViewHolder{
    TextView mPayWay;
    CheckBox mCheckbox;
}

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

@Override
public int getGroupType(int groupPosition) {
    if (groupPosition < mPayWayList.size() - 2){
        return 0;
    }else {
        return 1;
    }
}



private class ChildViewHolder{
    TextView mProductName;
    CheckBox mCheckbox;
}



}

single_payway_parent.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">


<TextView
    android:id="@+id/parent_check_pay_way_text"
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:drawableEnd="@drawable/change_btn"
    android:drawableLeft="@drawable/icon_ecard"
    android:drawablePadding="15dp"
    android:drawableRight="@drawable/change_btn"
    android:drawableStart="@drawable/icon_ecard"
    android:gravity="center_vertical"
    android:paddingLeft="10dp"
    android:text="PayWay"
    android:textSize="@dimen/medium_text" />


<CheckBox
    android:id="@+id/parent_check_pay_way_checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginEnd="10dp"
    android:layout_marginRight="10dp"
    android:button="@null"
    android:background="@drawable/radio_button_bg"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false" />

</RelativeLayout>

single_payway_parent_another.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/white">

<CheckBox
    android:id="@+id/order_pay_way_checkbox"
    android:layout_width="match_parent"
    android:layout_height="42dp"
    android:background="@null"
    android:button="@null"
    android:clickable="true"
    android:drawableLeft="@drawable/pay_zfb_icon"
    android:drawablePadding="15dp"
    android:drawableRight="@drawable/radio_button_bg"
    android:focusable="false"
    android:gravity="center_vertical"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:text="AnotherPayWay"
    android:textColor="@color/blacktext"
    android:textSize="@dimen/medium_text" />
</LinearLayout>

single_payway_child.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/white">

<CheckBox
    android:id="@+id/order_pay_way_checkbox"
    android:layout_width="match_parent"
    android:layout_height="42dp"
    android:background="@null"
    android:button="@null"
    android:clickable="true"
    android:drawableLeft="@drawable/pay_zfb_icon"
    android:drawablePadding="15dp"
    android:drawableRight="@drawable/radio_button_bg"
    android:focusable="false"
    android:gravity="center_vertical"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:text="支付宝支付"
    android:textColor="@color/blacktext"
    android:textSize="@dimen/medium_text" />
</LinearLayout>




Aucun commentaire:

Enregistrer un commentaire