List Item child not refreshing

断了今生、忘了曾经 提交于 2019-12-12 05:39:58

问题


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:


  1. After vi retrive via covertviiew .gettag and retrieve checkbox object
  2. Set the check box visibility to Gone (This code should run always)
  3. Check if row is 3 (as you want) and make the visibility change
  4. if toggle is visible the update the state based on model data
  5. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!