帶搜尋框的listbox 例子
阿新 • • 發佈:2019-02-20
Reviewer Approved
is “1/tText “, where 1 is a zero based index for the icon array.
The CSearchListContainer illustrates how to use CAknSearchField with list boxes to allow users to search the list items. To use this example modify the GetArrayL() method and add code to populate the text item array for the list box as well as add icons to the list box icon array. The text string format for the CAknSingleGraphicStyleListBox
If you want to use a list box without the search box option you can use the List box example instead.
[edit]SearchListbox.cpp
CSearchListContainer* CSearchListContainer::NewL(void)
{
CSearchListContainer* self = CSearchListContainer:: NewLC();
CleanupStack::Pop(self);
return self;
}
CSearchListContainer* CSearchListContainer::NewLC(void)
{
CSearchListContainer* self = new (ELeave) CSearchListContainer();
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
CSearchListContainer::~CSearchListContainer()
{
delete iFindBox;
iFindBox = NULL;
delete iListBox;
}
void CSearchListContainer::ConstructL()
{
CreateWindowL();
SetRect(CEikonEnv::Static()->EikAppUi()->ClientRect());
MakeListBoxL();
ActivateL();
DrawNow();
}
void CSearchListContainer::MakeListBoxL()
{
TInt MySetIndex(0);
if(iListBox)
{
MySetIndex = GetSelectedIndexL();
}
delete iListBox;
iListBox = NULL;
iListBox = new( ELeave ) CAknSingleGraphicStyleListBox();
iListBox->ConstructL(this,EAknListBoxSelectionList);
CArrayPtr<CGulIcon>* icons =new( ELeave ) CAknIconArray(10);
CleanupStack::PushL(icons);
iListBox->Model()->SetItemTextArray(GetArrayL(icons));
iListBox->Model()->SetOwnershipType(ELbmOwnsItemArray);
CleanupStack::Pop(icons);
iListBox->ItemDrawer()->ColumnData()->SetIconArray(icons);
iListBox->CreateScrollBarFrameL( ETrue );
iListBox->ScrollBarFrame()->SetScrollBarVisibilityL(
CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto );
iListBox->SetRect(Rect());
delete iFindBox;
iFindBox = NULL;
iFindBox = CreateFindBoxL(iListBox, iListBox->Model(),
CAknSearchField::ESearch);
SizeChanged();
iListBox->ActivateL();
TInt ItemsCount = iListBox->Model()->ItemTextArray()->MdcaCount();
if(ItemsCount > MySetIndex && MySetIndex >= 0)
iListBox->SetCurrentItemIndex(MySetIndex);
else if(ItemsCount > 0)
iListBox->SetCurrentItemIndex(0);
UpdateScrollBar(iListBox);
DrawNow();
}
CAknSearchField* CSearchListContainer::CreateFindBoxL(CEikListBox* aListBox,
CTextListBoxModel* aModel, CAknSearchField::TSearchFieldStyle aStyle)
{
CAknSearchField* findbox = NULL;
if (aListBox && aModel)
{
// Gets pointer of CAknFilteredTextListBoxModel.
CAknFilteredTextListBoxModel* model =
STATIC_CAST( CAknFilteredTextListBoxModel*, aModel );
// Creates FindBox.
findbox = CAknSearchField::NewL( *this, aStyle, NULL,
KAknExListFindBoxTextLength);
CleanupStack::PushL(findbox);
// Creates CAknListBoxFilterItems class.
model->CreateFilterL( aListBox, findbox );
//Filter can get by model->Filter();
CleanupStack::Pop(findbox); // findbox
}
return findbox;
}
CDesCArray* CSearchListContainer::GetArrayL(CArrayPtr<CGulIcon>* aIcons)
{
CDesCArrayFlat* MyArray = new(ELeave)CDesCArrayFlat(10);
CleanupStack::PushL(MyArray);
// Append text items and icons in here...
//MyArray->AppendL(TextBuf);
//aIcons->AppendL(CGulIcon::NewL(Icon,IconMsk));
CleanupStack::Pop(MyArray);
return MyArray;
}
void CSearchListContainer::UpdateScrollBar(CEikListBox* aListBox)
{
if (aListBox)
{
TInt pos(aListBox->View()->CurrentItemIndex());
if (aListBox->ScrollBarFrame())
{
aListBox->ScrollBarFrame()->MoveVertThumbTo(pos);
}
}
}
void CSearchListContainer::SizeChanged()
{
if (iListBox)
{
if (iFindBox)
{
CAknColumnListBox* aknListBox = STATIC_CAST(CAknColumnListBox*,
iListBox);
AknFind::HandleFixedFindSizeChanged(this, aknListBox, iFindBox);
}
else
{
iListBox->SetRect(Rect()); // Sets rectangle of lstbox.
}
}
}
void CSearchListContainer::HandleResourceChange(TInt aType)
{
TRect rect;
if ( aType==KEikDynamicLayoutVariantSwitch )
{
AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, rect);
SetRect(rect);
}
CCoeControl::HandleResourceChange(aType);
}
TInt CSearchListContainer::GetSelectedIndexL(void)
{
TInt Ret(-1);
if(iListBox)
{
TInt CurrItemInd = iListBox->CurrentItemIndex();
CAknFilteredTextListBoxModel* model =
STATIC_CAST(CAknFilteredTextListBoxModel*,iListBox->Model());
if(model && CurrItemInd >= 0)
{
Ret = model->Filter()->FilteredItemIndex(CurrItemInd);
}
}
return Ret;
}
TKeyResponse CSearchListContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,
TEventCode aEventCode)
{
TKeyResponse Ret = EKeyWasNotConsumed;
switch (aKeyEvent.iCode)
{
case EKeyDevice3:
break;
default:
if(iListBox)
{
if ( iFindBox )
{
TBool needRefresh( EFalse );
// Offers the key event to find box.
if ( AknFind::HandleFindOfferKeyEventL( aKeyEvent, aEventCode, this,
iListBox, iFindBox,
EFalse,
needRefresh ) ==
EKeyWasConsumed )
{
if ( needRefresh )
{
SizeChanged();
DrawNow();
}
return EKeyWasConsumed;
}
}
Ret = iListBox->OfferKeyEventL(aKeyEvent, aEventCode);
}
break;
}
return Ret;
}
void CSearchListContainer::Draw(const TRect& /*aRect*/) const
{
CWindowGc& gc = SystemGc();
gc.Clear(Rect());
}
CCoeControl* CSearchListContainer::ComponentControl( TInt aIndex) const
{
if(iFindBox && aIndex)
return iFindBox;
else
return iListBox;
}
TInt CSearchListContainer::CountComponentControls() const
{
if(iListBox && iFindBox)
{
return 2;
}
else
{
return 0;
}
}
[edit]SearchListbox.h
#include <coecntrl.h>
#include <aknlists.h>
#include <EIKLBX.H>
#include <aknsfld.h>
const TInt KAknExListFindBoxTextLength = 20;
class CSearchListContainer : public CCoeControl
{
public:
static CSearchListContainer* NewL(void);
static CSearchListContainer* NewLC(void);
~CSearchListContainer();
public:
TInt CountComponentControls() const;
CCoeControl* ComponentControl( TInt aIndex) const;
TKeyResponse OfferKeyEventL(const TKeyEvent& aKeyEvent,
TEventCode aEventCode);
TInt GetSelectedIndexL(void);
private:
void MakeListBoxL();
CAknSearchField* CreateFindBoxL(CEikListBox* aListBox,
CTextListBoxModel* aModel, CAknSearchField::TSearchFieldStyle aStyle);
CDesCArray* GetArrayL(CArrayPtr<CGulIcon>* aIcons);
void UpdateScrollBar(CEikListBox* aListBox);
virtual void SizeChanged();
virtual void HandleResourceChange(TInt aType);
void Draw(const TRect& aRect) const;
void ConstructL();
private:
CAknSingleGraphicStyleListBox* iListBox;
CAknSearchField* iFindBox;
};