最近看一些程式中有多視圖設定列表,學習了一下怎麼實現的,簡單記錄一下,以免以後備查,主要功能如
程式主介面
選擇菜單裡的設定選項
開啟設定視圖一
開啟設定視圖二
1、UI類繼承自MAknTabObserver類,並且實現它的void TabChangedL(TInt aIndex);函數(詳細實現見下)。
2、在UI類裡定義導航指標的指標
CAknNavigationDecorator* iNaviDecorator;
3、在UI函數裡進行如下實現
void CS60UIExampleAppUi::HandleCommandL(TInt aCommand)
{
switch (aCommand)
{
case EEikCmdExit:
case EAknSoftkeyExit:
Exit();
break;
//選擇設定菜單
case ES60UIExampleSettings:
{
TUid naviPaneUid;
naviPaneUid.iUid = EEikStatusPaneUidNavi;
CEikStatusPane* statusPane = StatusPane();
CEikStatusPaneBase::TPaneCapabilities subPane =
statusPane->PaneCapabilities(naviPaneUid);
// if we can access the navigation pane
if (subPane.IsPresent() && subPane.IsAppOwned())
{
CAknNavigationControlContainer* naviPane =
(CAknNavigationControlContainer *) statusPane->ControlL(naviPaneUid);
delete iNaviDecorator;
iNaviDecorator = NULL;
// ownership is transferred to us here
iNaviDecorator = naviPane->CreateTabGroupL();
// ownership not transferred
CAknTabGroup* tabGroup = (CAknTabGroup*) iNaviDecorator->DecoratedControl();
// Display two tabs of normal length on the navigation pane at a time
tabGroup->SetTabFixedWidthL(KTabWidthWithOneTab);//設定檢視列是顯示一個標籤還是多個
tabGroup->SetObserver(this);
//添加兩個要顯示的視圖以及標題標題 ,也可以添加多個
HBufC* tab1Text = StringLoader::LoadLC(R_S60UIEXAMPLE_HIGHSCORE_TITLE_TEXT);
tabGroup->AddTabL(ES60UIExampleSettingsViewId1, *tab1Text);
CleanupStack::PopAndDestroy(tab1Text);
HBufC* tab2Text = StringLoader::LoadLC(R_S60UIEXAMPLE_RESETTING_TEXT);
tabGroup->AddTabL(ES60UIExampleSettingsViewId2, *tab2Text);
CleanupStack::PopAndDestroy(tab2Text);
// highlight the first tab
tabGroup->SetActiveTabByIndex(0);
naviPane->PushL(*iNaviDecorator);
//啟用第一個視圖
ActivateLocalViewL(TUid::Uid(tabGroup->TabIdFromIndex(0)));
}
break;
}
//當設定視圖返回時,銷毀導覽列
case ES60UIExampleDelNavi:
{
TUid naviPaneUid;
naviPaneUid.iUid = EEikStatusPaneUidNavi;
CEikStatusPane* statusPane = StatusPane();
CEikStatusPaneBase::TPaneCapabilities subPane =
statusPane->PaneCapabilities(naviPaneUid);
// if we can access the navigation pane
if (subPane.IsPresent() && subPane.IsAppOwned())
{
CAknNavigationControlContainer* naviPane =
(CAknNavigationControlContainer *) statusPane->ControlL(naviPaneUid);
delete iNaviDecorator;
iNaviDecorator = NULL;
naviPane->PushDefaultL();
}
break;
}
default:
Panic(ES60UIExampleBasicUi);
break;
}
}
//導覽列接受按鈕的響應
TKeyResponse CS60UIExampleAppUi::HandleKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType)
{
if (iNaviDecorator == NULL)
{
return EKeyWasNotConsumed;
}
CAknTabGroup* tabGroup = (CAknTabGroup*) iNaviDecorator->DecoratedControl();
if (tabGroup == NULL)
{
return EKeyWasNotConsumed;
}
// if we've got a tab group, then offer the key event to it.
return tabGroup->OfferKeyEventL(aKeyEvent, aType);
}
//根據按鍵顯示不同的視圖
void CS60UIExampleAppUi::TabChangedL(TInt aIndex)
{
if (iNaviDecorator != NULL)
{
CAknTabGroup* tabGroup = (CAknTabGroup*) iNaviDecorator->DecoratedControl();
ActivateLocalViewL(TUid::Uid(tabGroup->TabIdFromIndex(aIndex)));
}
}
//UI類的解構函式中刪除相應的導覽列指標
CS60UIExampleAppUi::~CS60UIExampleAppUi()
{
delete iNaviDecorator;
iNaviDecorator=NULL;
}
4、在繼續自CAknSettingItemList的設定列表類裡實現OfferKeyEventL函數,以避免左右導航失效
TKeyResponse CS60UIExampleList::OfferKeyEventL( const TKeyEvent & aKeyEvent,TEventCode aType)
{
if ( aType != EEventKey ) // Is not key event?
{
return EKeyWasNotConsumed;
}
switch(aKeyEvent.iCode)
{
case EKeyLeftArrow:
case EKeyRightArrow:
return EKeyWasNotConsumed;
break;
default:
break;
}
}
5、每一個設定列表的VIEW類裡按返回按鍵時銷毀UI裡建立的導航指標
void CS60UIExampleSettingsView::HandleCommandL (TInt aCommand)
函數裡添加以下代碼:
case EAknSoftkeyBack:
{
AppUi()->HandleCommandL(ES60UIExampleDelNavi);
AppUi()->ActivateLocalViewL (TUid::Uid (ES60UIExampleInitialViewId));
break;
}