GetChildView 在折叠和展开 ExpandableListView 时得到刷新
Posted
技术标签:
【中文标题】GetChildView 在折叠和展开 ExpandableListView 时得到刷新【英文标题】:GetChildView is getting refreshed on collapsing and expanding ExpandableListView 【发布时间】: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
的状态。希望我能够解释我的问题。提前致谢。
【问题讨论】:
【参考方案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)
...
);
注意:我只是想解决您的问题,因为还有其他方法可以解决。
【讨论】:
以上是关于GetChildView 在折叠和展开 ExpandableListView 时得到刷新的主要内容,如果未能解决你的问题,请参考以下文章