ExpandableListView 子项中的按钮单击处理

Button Click Handling inside the ExpandableListView Child

我面临的一个问题是,当我更改 ExpandableListView 子项中的 ImageView 资源时。我为不同的父项设置不同的子项,然后我处理其中一个父子项内的按钮单击(我在 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;
    }

}

请帮帮我。

您要更改的视图应在 childView 方法中声明,如果您想在单击 Button 时更改 ImageSwitcher 视图,则可以在 childView 方法中将其定义为 Final 变量。

所以 childView 方法会像下面这样

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