GetChildView在折叠和展开ExpandableListView时得到刷新

时间:2019-07-03 13:04:54

标签: android expandablelistadapter

我实现了ExpandableListView,它有3个组,每个组包含一个孩子。每个ChildView包含一个SwitchCompat

每当我折叠/展开其他组时,Switch侦听器都会再次被调用。它会在未选中的位置被反复检查。

有关更多信息,我还将发布一段代码:

   @Override
public View getChildView(final int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {
    try {

        final ViewHolder viewHolder;

        if (convertView == null) {
            // inflate the layout
            LayoutInflater inflater = ((Activity) activity).getLayoutInflater();
            convertView = inflater.inflate(R.layout.link_wallet_item_child, null);

            // well set up the ViewHolder
            viewHolder = new ViewHolder();
            viewHolder.autoPaySwitch = (SwitchCompat) convertView.findViewById(R.id.autoPay_switch);
            viewHolder.textInputEditText = (TextInputEditText) convertView.findViewById(R.id.limit_inputEdittext);
            viewHolder.okTextView = (TextView) convertView.findViewById(R.id.ok_textView);
            viewHolder.limitCheckBox = (CheckBox) convertView.findViewById(R.id.limit_checkBox);

            // store the holder with the view.
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        Wallet wallet = (Wallet) getChild(groupPosition, childPosition);

        //Switch
        if (wallet.isAutoPay()) {
            viewHolder.autoPaySwitch.setChecked(true);
        } else {
            viewHolder.autoPaySwitch.setChecked(false);
        }
        ....
        viewHolder.autoPaySwitch.setOnCheckedChangeListener
                (new CompoundButton.OnCheckedChangeListener() {
                     @Override
                     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                         if (InternetConnection.checkConnection(activity)) {
                             new UpdateWalletsAutoPayRequest(activity,
                                     walletList.get(groupPosition).getWalletId().toString(),
                                     isChecked);

                         } else {
                             Toast.makeText(activity, activity.getString(R.string.internet_error),
                                     Toast.LENGTH_SHORT).show();

                             viewHolder.autoPaySwitch.setChecked(!isChecked);
                         }
                     }
                 }
                );
        ...
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return convertView;
}

Listener上方,更改SwitchCompat的状态。希望我能解释我的问题。预先感谢。

1 个答案:

答案 0 :(得分:1)

再次调用该侦听器,因为在getChildView期间您正在更新它们的值:

// This is triggering the listener that you created previously
if (wallet.isAutoPay()) {
    viewHolder.autoPaySwitch.setChecked(true);
} else {
    viewHolder.autoPaySwitch.setChecked(false);
}

为了解决这个问题,我认为您可以清除侦听器(因为稍后再进行设置):

// Clear the listener since you are updating the value but don't need to listen to the callback
viewHolder.autoPaySwitch.setOnCheckedChangeListener(null);
if (wallet.isAutoPay()) {
    viewHolder.autoPaySwitch.setChecked(true);
} else {
    viewHolder.autoPaySwitch.setChecked(false);
}

// Set the listener again as you are already doing
viewHolder.autoPaySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
     @Override
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        ...
    }
});

注意:由于有其他解决方法,我只是想解决您的问题。