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

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
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值