GetChildView 在折叠和展开 ExpandableListView 时刷新
GetChildView is getting refreshed on collapsing and expanding ExpandableListView
我实现了 ExpandableListView
,它有 3 个组,每个组包含一个 child。每个 ChildView
包含一个 SwitchCompat
每当我 collapse/expand 其他组时,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
的状态。希望我能解释我的问题。提前致谢。
正在再次调用侦听器,因为在 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) {
...
}
});
注意:我只是想解决你的问题,因为还有其他方法可以解决。
我实现了 ExpandableListView
,它有 3 个组,每个组包含一个 child。每个 ChildView
包含一个 SwitchCompat
每当我 collapse/expand 其他组时,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
的状态。希望我能解释我的问题。提前致谢。
正在再次调用侦听器,因为在 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) {
...
}
});
注意:我只是想解决你的问题,因为还有其他方法可以解决。