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

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

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

  然后在Container的ConstructL中去创建它:

 

void CUniNewsAppContainer::ConstructL(const TRect& aRect) ...{
    CreateWindowL();
   
    //add your code here ...

    //construct a listbox
    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(TInt tabId)
...{
    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(TInt i=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(const CEikTextListBox& aListBox);
    ~CCustomListItemDrawer();

private:
    virtual void DrawActualItem(TInt aItemIndex, const TRect& aActualItemRect,
         TBool aItemIsCurrent, TBool aViewIsEmphasized, TBool aViewIsDimmed,
         TBool aItemIsSelected) const;

public:
    void SetIconArray(CArrayPtr<CGulIcon>* aIconArray);
    TSize MaxIconSize() const;

private:
    void DeleteIconArray();
    void CalculateMaxIconSize();

private:
    const CEikTextListBox& iListBox;
    CArrayPtr<CGulIcon>*   iIconArray;
    TSize                  iMaxIconSize;
};

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

  第二步是作一个自己的ListBox控件:

 

class CCustomListBox: public CEikTextListBox
    ...{
public: // constructors
    CCustomListBox();
    void ConstructL(const CCoeControl* aParent, TInt aFlags = 0);

public: // from CEikTextListBox
    virtual TKeyResponse OfferKeyEventL(const TKeyEvent& aKeyEvent,
        TEventCode aType);

private: // from CEikTextListBox
    virtual void CreateItemDrawerL();
    };

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

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

【编辑推荐】

  1. Symbian每6个月发布一款新开源操作系统
  2. 微软建WindowsMarketplace 完善智能机平台
  3. 大力模仿Android 传微软WM6.5可装插件
责任编辑:庞桂玉 来源: it168
相关推荐

2009-04-12 08:57:50

Symbian诺基亚移动OS

2009-04-12 09:03:50

Symbian诺基亚移动OS

2009-04-12 08:59:05

Symbian诺基亚移动OS

2009-04-12 09:00:08

Symbian诺基亚移动OS

2009-04-12 09:02:32

Symbian诺基亚移动OS

2009-04-12 09:06:00

Symbian诺基亚移动OS

2009-04-12 09:01:05

Symbian诺基亚移动OS

2009-08-12 15:50:40

C# ListBox

2009-04-12 08:36:09

Symbian诺基亚移动OS

2009-04-12 08:46:43

Symbian诺基亚移动OS

2009-04-12 08:51:50

Symbian诺基亚移动OS

2009-04-12 09:07:17

Symbian诺基亚移动OS

2009-04-12 08:45:32

Symbian诺基亚移动OS

2009-04-12 08:48:47

Symbian诺基亚移动OS

2009-04-12 08:50:08

Symbian诺基亚移动OS

2011-06-16 16:21:06

Qt Symbian FAQ

2010-03-16 10:51:35

Silverlight

2011-06-16 15:59:40

Qt Symbian

2009-04-12 08:52:52

Symbian诺基亚移动OS

2012-05-24 09:38:08

Symbian
点赞
收藏

51CTO技术栈公众号