本篇文章主要使用SListView的控件和适配器的使用,做过Android的都懂,跟Android下的listView功能差不多一致,我刚开始就只不知道Item如何点击的所以看了官方文档记录下来 写个小例子。
放上一张效果图,插入 删除 修改 没去实现 其实也是很简单的 就是修改数据源 然后刷新就行 跟Android的类似 所以没再实现
?界面布局很简单不用解释看效果图就知道
<SOUI name="mainWindow" title="@string/title" bigIcon="ICON_LOGO:32" smallIcon="ICON_LOGO:16" margin="5,5,5,5" resizable="1" wndType="appMain"
appWnd="1"
translucent="1"
>
<root skin="_skin.sys.wnd.bkgnd" cache="1" width="600" height="400" >
<caption pos="0,0,-0,30" show="1" font="adding:0">
<icon pos="10,8" src="ICON_LOGO:16"/>
<text pos="29,9">@string/title</text>
<imgbtn name="btn_close" skin="_skin.sys.btn.close" pos="-45,0" tip="close" animate="1"/>
<imgbtn name="btn_max" skin="_skin.sys.btn.maximize" pos="-83,0" animate="1" />
<imgbtn name="btn_restore" skin="_skin.sys.btn.restore" pos="-83,0" show="0" animate="1" />
<imgbtn name="btn_min" skin="_skin.sys.btn.minimize" pos="-121,0" animate="1" />
</caption>
<window pos="0,[2,0,0" size="-2,-2" layout="vbox">
<text name="displayInfo" size="-2,20" />
<window layout="hbox" size="-2,30">
<button name="showlist" text="显示列表" weight="1" size="0,-2" extend_left="5" drawFocusRect="0"></button>
<button name="insert" text="插入" weight="1" size="0,-2" extend_left="5" drawFocusRect="0"></button>
<button name="deletelist" text="删除" weight="1" size="0,-2" extend_left="5" drawFocusRect="0"></button>
<button name="modifylist" text="修改" weight="1" size="0,-2" extend_left="5" extend_right="5" drawFocusRect="0"></button>
</window>
<!--colorBkgnd="@color/red"-->
<window layout="vbox" weight="1" size="-2,-1" >
<listview pos="0,0,-0,[-10" size="-2,-2" name="listviewDemo" dividerSize="1">
<template itemHeight="50" colorNormal="@color/white" colorSelected="#e4f0fc" colorHover="#e4f0fc">
<window layout="vbox" size="-2,-2" pos="|5,|0" offset="-1,-0.5">
<text name="name" />
<text name="time" />
</window>
</template>
</listview>
</window>
</window>
</root>
</SOUI>
业务逻辑代码如下:
// MainDlg.cpp : implementation of the CMainDlg class
//
/
#include "stdafx.h"
#include "MainDlg.h"
#include <helper/SAdapterBase.h>
#include <helper/STime.h>
#include "printlog.h"
CMainDlg::CMainDlg() : SHostWnd(_T("LAYOUT:XML_MAINWND"))
{
}
CMainDlg::~CMainDlg()
{
}
class StudentsAdapter : public SAdapterBase
{
private:
CMainDlg *out = nullptr;
public:
struct Student
{
SStringT time;
SStringT name;
};
SArray<Student> studentList;
public:
StudentsAdapter(CMainDlg *cd)
{
out = cd;
Student recs[] =
{
{
_T("2021_9_19"),
_T("张三"),
},
{
_T("2021_9_19"),
_T("李四"),
},
{
_T("2021_9_19"),
_T("王五"),
},
{
_T("2021_9_19"),
_T("赵六"),
},
{
_T("2021_9_19"),
_T("老七"),
},{
_T("2021_9_19"),
_T("张三"),
},
{
_T("2021_9_19"),
_T("李四"),
},
{
_T("2021_9_19"),
_T("王五"),
},
{
_T("2021_9_19"),
_T("赵六"),
},
{
_T("2021_9_19"),
_T("老七"),
}
};
for (int i = 0; i < ARRAYSIZE(recs); i++)
{
studentList.Add(recs[i]);
}
}
void addStudents(Student s) {
studentList.Add(s);
this->notifyDataSetChanged();
}
protected:
virtual int getCount()
{
return studentList.GetCount();
}
virtual void getView(int position, SWindow * pItem, pugi::xml_node xmlTemplate)
{
if (pItem->GetChildrenCount() == 0)
{
pItem->InitFromXml(xmlTemplate);
}
Student & rec = studentList[position];
SStatic * sStatic = pItem->FindChildByID2<SStatic>(R.id.name);
sStatic->SetWindowText(rec.name);
PrintLog("%s\n", rec.time);
SStatic * sTime = pItem->FindChildByID2<SStatic>(R.id.time);
sTime->SetWindowText(rec.time);
pItem->SetUserData(position);
pItem->GetEventSet()->subscribeEvent(EventItemPanelClick::EventID, Subscriber(&StudentsAdapter::OnItemClick, this));
pItem->GetEventSet()->subscribeEvent(EventItemPanelDbclick::EventID, Subscriber(&StudentsAdapter::OnDoubleItemClick, this));
}
bool OnDoubleItemClick(EventArgs *pEvt) {
EventItemPanelDbclick* pEvt_ItemPanel = sobj_cast<EventItemPanelDbclick>(pEvt);
SASSERT(pEvt_ItemPanel);
SWindow* pPanel = sobj_cast<SWindow>(pEvt_ItemPanel->sender);
SOUI::CPoint pt(GET_X_LPARAM(pEvt_ItemPanel->lParam), GET_Y_LPARAM(pEvt_ItemPanel->lParam));
SWND swnd = pPanel->SwndFromPoint(pt);
SWindow *pClicked = NULL;
if (swnd)
{
pClicked = SWindowMgr::GetWindow(swnd);
}
int nIndex = pPanel->GetUserData();
PrintLogi("OnDoubleItemClick index=%s\n", nIndex);
return true;
}
bool OnItemClick(EventArgs *pEvt)
{
EventItemPanelClick* pEvt_ItemPanel = sobj_cast<EventItemPanelClick>(pEvt);
SASSERT(pEvt_ItemPanel);
SWindow* pPanel = sobj_cast<SWindow>(pEvt_ItemPanel->sender);
SOUI::CPoint pt(GET_X_LPARAM(pEvt_ItemPanel->lParam), GET_Y_LPARAM(pEvt_ItemPanel->lParam));
SWND swnd = pPanel->SwndFromPoint(pt);
SWindow *pClicked = NULL;
if (swnd)
{
pClicked = SWindowMgr::GetWindow(swnd);
}
int nIndex = pPanel->GetUserData();
PrintLogi("OnItemClick index=%s\n", nIndex);
out->OnItemClick(nIndex);
return true;
}
};
bool CMainDlg::OnItemClick(int nIndex) {
SStringT info = _T("点击Item:");
CString str;
str.Format(_T("%d"), nIndex);
info.Append(str);
displayInfo->SetWindowText(info);
return true;
}
void CMainDlg::initView()
{
if (insert == nullptr) {
displayInfo = FindChildByID2<SStatic>(R.id.displayInfo);
show = FindChildByID2<SButton>(R.id.showlist);
insert = FindChildByID2<SButton>(R.id.insert);
deleteList = FindChildByID2<SButton>(R.id.deletelist);
modify = FindChildByID2<SButton>(R.id.modifylist);
listView = FindChildByID2<SListView>(R.id.listviewDemo);
}
}
void CMainDlg::onClick(EventArgs *pEvt) {
SButton *pBtn = sobj_cast<SButton>(pEvt->sender);
SStringT btnText = pBtn->GetWindowText();
SStringT btnName = pBtn->GetName();
OutputDebugString(btnName);
if (btnName == _T("showlist")) {
initView();
ILvAdapter *pLvAdapter = new StudentsAdapter(this);
listView->SetAdapter(pLvAdapter);
pLvAdapter->Release();
show->EnableWindow(false);
}
else if (btnName == _T("insert")) {
SStringT info=_T("插入暂无实现");
displayInfo->SetWindowText(info);
}
else if (btnName == _T("deletelist")) {
SStringT info = _T("删除暂无实现");
displayInfo->SetWindowText(info);
}
else if (btnName == _T("modifylist")) {
SStringT info = _T("修改暂无实现");
displayInfo->SetWindowText(info);
}
}
BOOL CMainDlg::OnInitDialog(HWND hWnd, LPARAM lParam)
{
return 0;
}
//TODO:消息映射
void CMainDlg::OnClose()
{
SNativeWnd::DestroyWindow();
}
void CMainDlg::OnMaximize()
{
SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE);
}
void CMainDlg::OnRestore()
{
SendMessage(WM_SYSCOMMAND, SC_RESTORE);
}
void CMainDlg::OnMinimize()
{
SendMessage(WM_SYSCOMMAND, SC_MINIMIZE);
}
void CMainDlg::OnSize(UINT nType, CSize size)
{
SetMsgHandled(FALSE);
SWindow *pBtnMax = FindChildByName(L"btn_max");
SWindow *pBtnRestore = FindChildByName(L"btn_restore");
if (!pBtnMax || !pBtnRestore) return;
if (nType == SIZE_MAXIMIZED)
{
pBtnRestore->SetVisible(TRUE);
pBtnMax->SetVisible(FALSE);
}
else if (nType == SIZE_RESTORED)
{
pBtnRestore->SetVisible(FALSE);
pBtnMax->SetVisible(TRUE);
}
}
本骗文章学习的地方列表的点击和双击事件的处理。
核心代码:
pItem->GetEventSet()->subscribeEvent(EventItemPanelClick::EventID, Subscriber(&StudentsAdapter::OnItemClick, this));
pItem->GetEventSet()->subscribeEvent(EventItemPanelDbclick::EventID, Subscriber(&StudentsAdapter::OnDoubleItemClick, this));
看刚开始想了各种方法都不行 后面看了官方的例子找到这个订阅的事件和事件ID才知道的
代码例子稍后分享,上传后还拿不到链接地址。
代码链接:工程源代码链接
|