Search code examples
androidexpandablelistviewimageswitcher

Button Click Handling inside the ExpandableListView Child


A problem that I am facing which is, When I am changing the ImageView Resource inside the ExpandableListView child. I am setting different Child for Different parent and then I am handling the button click inside one of the parent-child as (Here I am doing all this in groupPosition ==3):

FilterAdapter.java

public  class FilterAdapter extends BaseExpandableListAdapter {
    List<String> groupList;
    Map<String,List<String>> childMapList;
    List<String> childList;
    Context mContext;
    FilterListGroupLayoutBinding binding;
    FilterbyChildLayoutBinding filterbyChildLayoutBinding;
    TransferStatusChildLayoutBinding transferStatusChildLayoutBinding;
    SortByDateChildLayoutBinding sortByDateChildLayoutBinding;
    private static String  childView= "";
    private static String groupName="";
    private static String childName="";
    static  int c=1;
    TextView ivPickUpTxt;
    ImageButton pickUpImage,vouchredImage,cancledImage;
    ImageSwitcher bookedonImage;
    CheckBox pendingyesCheckBox,pendingNoCheckBox,vehicleAssignCheckBox,vehicleAssignNoCheckbox,driverYesCheckBox,driverNoCheckbox,vehicleDispatchCheckBox;
   CheckBox vehicleDispatchNocheckbox,transferCompletedYesCheckBox,transferCompletedNoCheckBox;
    CheckBox  ivCancleCheckBox,voucheredCheckBox;
    RelativeLayout text2Layout,text1Layout;
    View convertView1;
    TextView bookedonToTxt,bookedOnEdtTxt, pickUpFromEditTxt,pickupDateToText;
    EditText supplierConfNo,confEditTxt,passangerNameTxt,pickUpEditTxt ;

    Button btm,btm1;
    String dateTime;
//    FilterFragment filterFragment = new FilterFragment();

    SimpleDateFormat sdf =  new SimpleDateFormat("dd-MM-yyyy");

    public FilterAdapter(Context context, List<String> groupList, Map<String,List<String>> childMapList) {
        this.groupList=groupList;
        this.childMapList = childMapList;
        this.mContext = context;
    }

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

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        String s;
        s = groupList.get(groupPosition);
        childList = childMapList.get(s);
        return childList.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 false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View view, ViewGroup viewGroup) {
        String headerTitle = (String) getGroup(groupPosition);
        if(view == null){
            LayoutInflater infalInflater = (LayoutInflater) this.mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = infalInflater.inflate(R.layout.filter_list_group_layout, null);
            /*binding = DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()), R.layout.filter_list_group_layout,viewGroup,false);
            view = binding.getRoot()*/;

        }
        ImageView groupIndicatorImage = (ImageView) view.findViewById(R.id.groupIndicatorImage);
        ExpandableListView eLV = (ExpandableListView) viewGroup;
       eLV.expandGroup(0);


        if (isExpanded) {
            if(groupPosition==0){
                groupIndicatorImage.setVisibility(View.INVISIBLE);
            }else {
                groupIndicatorImage.setVisibility(View.VISIBLE);
                groupIndicatorImage.setBackgroundResource(R.drawable.arrow_down);
            }
        } else if (!isExpanded) {
            if(groupPosition==0){
                groupIndicatorImage.setVisibility(View.INVISIBLE);
            }else {
                groupIndicatorImage.setVisibility(View.VISIBLE);
                groupIndicatorImage.setBackgroundResource(R.drawable.filter_arrow);
            }
        }

        TextView groupIndicator = (TextView) view.findViewById(R.id.ivGroupIndicator);
        groupIndicator.setText(headerTitle);
       /* binding.ivGroupIndicator.setText(headerTitle);*/
        return view;
    }

    @Override
    public void onGroupCollapsed(int groupPosition) {
        super.onGroupCollapsed(groupPosition);

    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean b, View convertView, ViewGroup viewGroup) {
        LayoutInflater infalInflater = (LayoutInflater) this.mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        String headerTitle = (String) getGroup(groupPosition);
        int childType = getChildType(groupPosition, childPosition);
//        String child = getChild(groupPosition, childPosition).toString();
        String parent = ((String) getGroup(groupPosition));

// || (int)convertView.getTag()!= childType
        if(convertView == null){
            if(groupPosition == 0){
                convertView = infalInflater.inflate(R.layout.booking_status_child_layout, null);

            }
            else if(groupPosition == 1){
                convertView = infalInflater.inflate(R.layout.filterby_child_layout, null);
            }
            else if(groupPosition == 2){
                convertView = infalInflater.inflate(R.layout.transfer_status_child_layout, null);
            }
            else if(groupPosition == 3){
                convertView = infalInflater.inflate(R.layout.sort_by_date_child_layout, null);

            }

        }
        else{

            if(groupPosition == 0){
                convertView = infalInflater.inflate(R.layout.booking_status_child_layout, null);


            }
           else if(groupPosition == 1){

                convertView = infalInflater.inflate(R.layout.filterby_child_layout, null);


            }
            else if(groupPosition == 2){
                convertView = infalInflater.inflate(R.layout.transfer_status_child_layout, null);


            }
            else if(groupPosition == 3){


                convertView = infalInflater.inflate(R.layout.sort_by_date_child_layout, null);
                /*image.... initialization*/
                bookedonImage = (ImageSwitcher) convertView.findViewById(R.id.booedOnImage);
                btm = (Button) convertView.findViewById(R.id.btm);
                btm1 = (Button) convertView.findViewById(R.id.btm1);


                bookedonImage.setFactory(new ViewSwitcher.ViewFactory() {
                    @Override
                    public View makeView() {
                        ImageView myView = new ImageView(mContext);
                        myView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                        return myView;
                    }
                });
                btm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                            bookedonImage.setImageResource(R.drawable.ascending_active);

                    }
                });
                btm1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                            bookedonImage.setImageResource(R.drawable.decending_active);

                    }
                });



        return convertView;
    }

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

}

Please help me out.


Solution

  • The view which you want to change should be declared inside childView method and if you want to change ImageSwitcher View on click of Button then you can define it as Final variable inside childView method.

    So childView Method would be like below

    public View getChildView(int groupPosition, int childPosition, boolean b, View convertView, ViewGroup viewGroup) {
        LayoutInflater infalInflater = (LayoutInflater) this.mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        String headerTitle = (String) getGroup(groupPosition);
        int childType = getChildType(groupPosition, childPosition);
        String parent = ((String) getGroup(groupPosition));
        if(convertView == null){
            switch (childType){
                case CHILD_TYPE_1:
                    convertView = infalInflater.inflate(R.layout.booking_status_child_layout, null);
                    convertView.setTag(childType);
                    break;
                case CHILD_TYPE_2:
                    convertView = infalInflater.inflate(R.layout.filterby_child_layout, null);
                    convertView.setTag(childType);
                    break;
                case CHILD_TYPE_3:
                    convertView = infalInflater.inflate(R.layout.transfer_status_child_layout, null);
                    convertView.setTag(childType);
                    break;
                case CHILD_TYPE_4:
                    convertView = infalInflater.inflate(R.layout.sort_by_date_child_layout, null);
                    convertView.setTag(childType);
                    break;
                default:
                    break;
            }
        }
    
      switch (childType){
            case CHILD_TYPE_1:
                break;
            case CHILD_TYPE_2:
                break;
            case CHILD_TYPE_3:
                break;
            case CHILD_TYPE_4:
                btm = (Button) convertView.findViewById(R.id.btm);
                btm1 = (Button) convertView.findViewById(R.id.btm1);
                final ImageSwitcher bookedonImage= (ImageSwitcher) convertView.findViewById(R.id.booedOnImage);
                btm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                           bookedonImage.setImageResource(R.drawable.ascending_active);             
    
                    }
                });
               btm1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        bookedonImage.setImageResource(R.drawable.decending_active);
    
                    }
                });
                break;
        }
        return convertView;
    }