【发布时间】:2019-11-14 04:26:40
【问题描述】:
我已经实现了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的状态。希望我能够解释我的问题。提前致谢。
【问题讨论】:
标签: android expandablelistadapter