简体   繁体   English

notifyDataSetChanged如何工作?

[英]How does notifyDataSetChanged work?

I have a question on how notifyDataSetChanged() works in a BaseExpandableListAdapter 我有一个关于notifyDataSetChanged()如何在BaseExpandableListAdapter工作的BaseExpandableListAdapter

I am updating a boolean variable, the output of which determines the output of getChildrenCount() but the number of comments visible does not change on calling it 我正在更新一个布尔变量,该变量的输出确定getChildrenCount()的输出,但是可见的注释数在调用它时不会改变

Code: 码:

buttonViewComments.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                allCommentsVisible = !allCommentsVisible;
                notifyDataSetChanged();
            }
        });

getChildrenCount: getChildrenCount:

public int getChildrenCount(int groupPosition) {
        if(allCommentsVisible || postList.get(groupPosition).commentListSize()<=3)
            return postList.get(groupPosition).commentListSize();
        else{
            return 3;
        }
    }

notifyDataSetChanged

Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself. 通知附加的观察者基础数据已更改,任何反映该数据集的视图都应刷新自身。 Reference 参考

And in your case, there isn't any underlying data changes. 在您的情况下,没有任何基础数据更改。 In order to make it work you need to change your list postList items so it will update the set and the counter will be updated along with the size of the set. 为了使其工作,您需要更改列表postList项目,以便它将更新集合,并且计数器将随着集合的大小而更新。

You will get some useful information about this from this link. 您将从此链接中获得一些有用的信息。

Android. 安卓 How does notifyDataSetChanged() method and ListViews work? notifyDataSetChanged()方法和ListViews如何工作?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM