GetChildView обновляется после свертывания и расширения ExpandableListView - PullRequest
0 голосов
/ 03 июля 2019

Я реализовал 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 Ответ

1 голос
/ 03 июля 2019

Слушатель вызывается снова, потому что во время 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) {
        ...
    }
});

Примечание: я просто пытаюсь исправить вашу проблему, так как есть другие способы сделать это.

...