关键函数: notifyDataSetInvalidated
通过这个函数可以来刷新控件中的值
通过一个checkbox全选的小例子 来使用
效果图:
这里使用MCListView这个控件 来实现的效果
<window pos="312,104" size="216, 184">
<mclistview name="mclv_test" colorBkgnd="@color/white" pos="10,10,200,-10" headerHeight="30">
<header align="center" itemSwapEnable="1" fixWidth="0" font="underline:0,adding:-3" sortHeader="0" colorBkgnd="#ffffff">
<items>
<item width="100">测试1</item>
</items>
</header>
<template itemHeight="40" colorHover="#FFFFFF" colorSelected="#ffffff">
<window name="col1" clipClient="1">
<check pos="5,7" size="32, 32" skin="_skin.sys.checkbox" name="check_test" />
<button pos="|20,10,@60,@25" offset="-0.5,0" name="btn_test" />
</window>
</template>
<check pos="5,0" size="32, 32" skin="_skin.sys.checkbox" name="btn_checkall" />
</mclistview>
</window>
初始化适配器
virtual void getView(int position, SWindow * pItem, pugi::xml_node xmlTemplate)
{
if (pItem->GetChildrenCount() == 0)
{
pItem->InitFromXml(xmlTemplate);
}
SButton *pBtnUninstall = pItem->FindChildByName2<SButton>(L"btn_test");
pBtnUninstall->SetUserData(position);
pBtnUninstall->GetEventSet()->subscribeEvent(EVT_CMD, Subscriber(&CTestMcAdapter::OnButtonClick, this));
SCheckBox *pCheck = pItem->FindChildByName2<SCheckBox>(L"check_test");
pCheck->SetCheck(vctFlag[position]);
}
vctFlag 这个里面就装载这checkbox的一个状态 也就是一个vector<BOOL> 类型的一个数据结构 后面更新状态 也是和vctFlag这个结构有关系 定义 其实就是在 类中的一个私有成员变量 vector<BOOl> vctFlag;
全部选中的实现
void SelectAll(BOOL bflag)
{
for (auto &flag : vctFlag)
{
flag = bflag;
}
notifyDataSetInvalidated(); //最关键的一个函数 刷新数据
}
这里vctFlag这个数据结构配合notifyDataSetInvalidated这个函数 来进行控件状态的一个刷新
最后我们加上 控制状态的那个checkbox的点击事件
SWindow *pWnd = FindChildByName2<SCheckBox>(L"btn_checkall");
pWnd->GetEventSet()->subscribeEvent(EVT_CMD, Subscriber(&CMainDlg::OnCheckAll, this));
bool CMainDlg::OnCheckAll(EventArgs *e)
{
SCheckBox *pCheck = sobj_cast<SCheckBox>(e->sender);
SMCListView *pmclistView = FindChildByName2<SMCListView>(L"mclv_test");
if (pmclistView)
{
CTestMcAdapter *pAdapter = (CTestMcAdapter *)pmclistView->GetAdapter();
BOOL bflag = pCheck->IsChecked();
pAdapter->SelectAll(bflag);
}
return true;
}
这样就实现了数据刷新的过程