问题
I am writing an custom list, in which displayed 3rd list item has toggle button as an child. On toggle button click it launch an confirmation dialog, if wants toggle button or not.
Now if list not scrolled then it work perfect. But if scroll list(so 3rd displayed as 1st item) then button toggle state(for toggleButton.setChecked(false)
) not working after Dialog-Cancel
action.
Any suggestion if I am missing anything.
EDIT
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if(convertView == null) {
vi = inflater.inflate(R.layout.setting_prefernce_row, parent, false);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.title = (TextView)vi.findViewById(R.id.txtPreferenceName); // Preference Name
viewHolder.txtPreferenceSub = (TextView)vi.findViewById(R.id.txtPreferenceSub); // Sub-Preference Name
viewHolder.txtArrow = (TextView)vi.findViewById(R.id.txtArrow); // text before arrow
viewHolder.imgArrow = (ImageView)vi.findViewById(R.id.imgArrow); // right_arrow
viewHolder.btnToggleState = (ToggleButton)vi.findViewById(R.id.tglToggleState); // toggle button
viewHolder.switchTemperatureUnit = (ImageView)vi.findViewById(R.id.switchTemperatureUnit); // switch button for temperature
//viewHolder.checkbox.setTag(list.get(position));
//viewHolder.scores.setTag(list.get(position));
vi.setTag(viewHolder);
} else {
vi = convertView;
}
....
}
回答1:
- After vi retrive via covertviiew .gettag and retrieve checkbox object
- Set the check box visibility to Gone (This code should run always)
- Check if row is 3 (as you want) and make the visibility change
- if toggle is visible the update the state based on model data
- When ever toggle is changed update the model
These are the usual things that we need to do for this type specific requirement. If you didn't get the issue resolved let me know.
回答2:
To optimize ViewHolder implementation damm required. And to get target item view following worked great.
for(int i = start, j = lstSettingPreference.getLastVisiblePosition(); i <= j; i++) {
if (ordinal == (int) lstSettingPreference.getItemAtPosition(i)) {
view = lstSettingPreference.getChildAt(i - start);
lstSettingPreference.getAdapter().getView(i, view, lstSettingPreference);
break;
}
}
Reference link: https://stackoverflow.com/a/9987616/2624806.
来源:https://stackoverflow.com/questions/29429704/list-item-child-not-refreshing