Symbian学习笔记(10):使用ListBox

本文介绍了Symbian系统中CAknSingleStyleListBox的基本使用方法,包括创建、填充内容及自定义ItemDrawer的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

symbian中的ListBox比较复杂也是比较常用的,我只能先从最简单的CAknSingleStyleListBox入手来尝试看看。太复杂的东西不是我这样的新手要立刻去明白的。

先声明一个列表组件: CAknSingleStyleListBox* iListBox;

然后在Container的ConstructL中去创建它:
void CUniNewsAppContainer::ConstructL( const TRect & aRect) ... {
CreateWindowL();

//addyourcodehere...

//constructalistbox
iListBox=new(ELeave)CAknSingleStyleListBox
iListBox
->SetContainerWindowL(*this);
iListBox
->SetListBoxObserver(this);
iListBox
->ConstructL(this,EAknListBoxSelectionList|EAknListBoxLoopScrolling);

iListBox
->CreateScrollBarFrameL(ETrue);
iListBox
->ScrollBarFrame()->SetScrollBarVisibilityL(CEikScrollBarFrame::EOn,CEikScrollBarFrame::EOn);
iListBox
->ItemDrawer()->ColumnData()->EnableMarqueeL(ETrue);
iListBox
->SetRect(aRect);

SetRect(aRect);
ActivateL();
}

这里有几句话,一是SetScrollBarVisibilityL设置使用滚动条,二是ItemDrawer()->ColumData()->EnableMarqueeL()让选中的文本超长后可以左右滚动。

有一点比较奇怪,我得先设置ListBox的Rect才能设置整个Container的Rect?否则ListBox会不占整个主面板的位置。

接着在合适的地方需要去给ListBox增加内容:
void CUniNewsAppContainer::InitListBox(TInttabId)
... {
if(iListBox==NULL)return;


CDesCArray
*list=static_cast<CDesCArray*>(iListBox->Model()->ItemTextArray());
TBuf
<256>str;

list
->Reset();

CUniNewsAppView
*appView=STATIC_CAST(CUniNewsAppUi*,iCoeEnv->AppUi())->iAppView;
RArray
<TNewsContent>*rc=appView->iChannelHandler->GetContents();
for(TInti=0;i<rc->Count();i++)
...{
if((*rc)[i].pid==tabId)...{
str.FillZ(str.MaxLength());
str.Format(KITEMFORMAT,(
*rc)[i].title);
list
->AppendL(str);
}

}


iListBox
->HandleItemAdditionL();
iListBox
->SetFocus(ETrue);
iListBox
->SetCurrentItemIndexAndDraw(appView->iListIndex);
iListBox
->ActivateL();

iListBox
->DrawNow();
}

这里的HandleItemAdditionL()通知一下ListBox模型作了增加操作,同样还有一个HandleItemRemovalL()则是通知ListBox作了一个删除操作。
这里的KITEMFORMAT定义是"\t%S"。这里的格式似乎挺重要的,一般是:图标ID\t题头字串\t主要字串\t图标ID\t图标ID。

我这里因为没有用到图标所以是一个\t%S,这个\t不可省略。如果用图标,则变成%d\t%S了,同时还要增加iconArray在创建iListBox的时候。
CAknIconArray * icons = new (ELeave)CAknIconArray( 2 );
CleanupStack::PushL(icons);
icons
-> ConstructFromResourceL(R_ICON_LISTICONS);
iListBox
-> ItemDrawer() -> ColumnData() -> SetIconArray(icons);
CleanupStack::Pop();

在ListBox中有一个叫Model()的还有一个叫View()的,从名字上就可以看出它们的含义了。前面我们从Model中操作列表内容,而我们可以从View中获取ItemDrawer去操作列表显示

的一些参数,但是我觉得有一点不爽的是,缺省生成的列表框字体比较大,不是太喜欢,在网上搜了一下,似乎那个设置字体的方法对我的机器不管用?

iListBox->ItemDrawer()->SetFont(ApacPlain12());

不过有一种方法是可行的,只是比较麻烦,那就是自己去实现ListBox,以及它的ItemDrawer。在网上看到的代码我试了一下,还行。方法如下。

第一步作一个自己的ItemDrawer:
class CCustomListItemDrawer: public CListItemDrawer
... {
public:
CCustomListItemDrawer(
constCEikTextListBox&aListBox);
~CCustomListItemDrawer();

private:
virtualvoidDrawActualItem(TIntaItemIndex,constTRect&aActualItemRect,
TBoolaItemIsCurrent,TBoolaViewIsEmphasized,TBoolaViewIsDimmed,
TBoolaItemIsSelected)
const;

public:
voidSetIconArray(CArrayPtr<CGulIcon>*aIconArray);
TSizeMaxIconSize()
const;

private:
voidDeleteIconArray();
voidCalculateMaxIconSize();

private:
constCEikTextListBox&iListBox;
CArrayPtr
<CGulIcon>*iIconArray;
TSizeiMaxIconSize;
}
;

实现的代码中最重要的就是那个DrawActualItem方法负责具体的绘制工作,从它的参数表中足够得到绘制所需的信息,剩下的事情就是用SystemGc去绘制。

第二步是作一个自己的ListBox控件:
class CCustomListBox: public CEikTextListBox
... {
public://constructors
CCustomListBox();
voidConstructL(constCCoeControl*aParent,TIntaFlags=0);

public://fromCEikTextListBox
virtualTKeyResponseOfferKeyEventL(constTKeyEvent&aKeyEvent,
TEventCodeaType);

private://fromCEikTextListBox
virtualvoidCreateItemDrawerL();
}
;

在它的CreateItemDrawerL()中创建成员iItemDrawer = new (ELeave) CCustomListItemDrawer(*this)。而OfferKeyEvent主要的作用是处理上下方向键。

关于ListBox的使用,可以参考这个地址: http://pagesperso-orange.fr/klisa/3650/ListBox/page01.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值