expandablelistview适配器子代不会在运行时通过调用notifyDataSetChange进行更新

编程入门 行业动态 更新时间:2024-10-28 04:28:43
本文介绍了expandablelistview适配器子代不会在运行时通过调用notifyDataSetChange进行更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

组视图的BaseExpandableListAdapter上有一个按钮,用于在其子级中添加注释.当将该项目添加到列表中并在该适配器类内部调用notifyDataSetChanged()时,它不会立即更改,但是会在折叠和扩展列表之后更改.

There is a button on BaseExpandableListAdapter on group view for adding comments in its child. When adding the item to the list and called notifyDataSetChanged() inside that adapter class, it is not changing instantly but changed after collapsing and expanding the list.

private List<String> ParentItem; private LinkedHashMap<String, List<JsonCase>> ChildItem; public View getChildView(final int listPosition, final int expandedListPosition,boolean isLastChild, View convertView, final ViewGroup parent) { final JsonCase expandedListText = (JsonCase) getChild(listPosition, expandedListPosition); if (convertView == null) { LayoutInflater layoutInflater = (LayoutInflater) this.context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = layoutInflater.inflate(R.layout.hearing_detail,parent, false); } final EditText comment = (EditText) convertView.findViewById(R.idment); TextView commentorName = (TextView) convertView.findViewById(R.idmentorName); TextView commentDate = (TextView) convertView.findViewById(R.idmentDate); comment.setText("" + expandedListText.details); if(expandedListTextmentorName!=null &&expandedListTextmentorName.equals("")){ commentorName.setEnabled(false); }else{ commentorName.setText(expandedListTextmentorName); } commentDate.setText(expandedListText.date); return convertView; } public void addCaseHearingsToAdapter(int listPosition){ String hearingId = this.ChildItem.get(this.ParentItem.get(listPosition)).get(0).hearingId; String userId = PreferenceData.getLoggedInUserId(context); JsonCase comment = new JsonCase(); commentmentId = ""; comment.hearingId = hearingId; commentmentedBy = userId; comment.details = ""; commentmentorName = ""; comment.date = new SimpleDateFormat("dd MMMM yyyy hh:mm:ss").format(Calendar.getInstance().getTime()); this.ChildItem.get(this.ParentItem.get(listPosition)).add(comment); this.notifyDataSetChanged(); }

单击添加注释按钮后,子项未更新:

After clicking add comment button child not updated:

折叠并再次展开后,子项已更新:

After collapsing and expanding again child updated:

推荐答案

实际上,就我而言,expandableListView的高度未更新,因此会导致运行时出现问题.深入研究代码之后.我在notifyDataSetChanged()之后调用此setListViewHeight函数,使我的消耗性列表视图在运行时更新.我将我的expandablelistView的高度设置为:

Actually, in my case, the height of expandableListView is not updated so it causes a problem on runtime. After digging into code. I call this setListViewHeight function after notifyDataSetChanged(), makes my expendable list view updated on runtime. I set the Height of my expandablelistView as:

public static void setListViewHeight(ExpandableListView listView, int group) { ExpandableListAdapter listAdapter = (ExpandableListAdapter) listView.getExpandableListAdapter(); int totalHeight = 0; int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.EXACTLY); for (int i = 0; i < listAdapter.getGroupCount(); i++) { View groupItem = listAdapter.getGroupView(i, false, null, listView); groupItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED); totalHeight += groupItem.getMeasuredHeight(); if (((listView.isGroupExpanded(i)) && (i != group)) || ((!listView.isGroupExpanded(i)) && (i == group))) { for (int j = 0; j < listAdapter.getChildrenCount(i); j++) { View listItem = listAdapter.getChildView(i, j, false, null, listView); listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED); totalHeight += listItem.getMeasuredHeight()+1; } } } ViewGroup.LayoutParams params = listView.getLayoutParams(); int height = totalHeight + (listView.getDividerHeight() * (listAdapter.getGroupCount() - 1)); if (height < 10) height = 200; params.height = height; listView.setLayoutParams(params); listView.requestLayout(); }

更多推荐

expandablelistview适配器子代不会在运行时通过调用notifyDataSetChange进行更新

本文发布于:2023-11-26 22:18:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635425.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:子代   会在   适配器   expandablelistview   notifyDataSetChange

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!