symbian 多视图开发

时间:2021-03-31 06:27:36

ZT:http://bbs.tech.ccidnet.com/read.php?tid=678157

 

symbian 多视图开发

第一步, AppUi 类继承自 CAknViewAppUi。如下所示


class AppUi : public CAknViewAppUi, MAknTabObserver
{
......


public: /** * iNaviPane * Not Owned by CsifAppUi object. */ CAknNavigationControlContainer* iNaviPane; /** * iTabGroup * Not Owned by CsifAppUi object. */ CAknTabGroup* iTabGroup; /** * iDecoratedTabGroup * Owned by CsifAppUi object. */ CAknNavigationDecorator* iDecoratedTabGroup;


private: // Data /** * iAppView1, The application view 1 * Not owned by CMultiViewsAppUi object. */ CMultiViewsView1* iAppView1; /** * iAppView2, The application view 2 * Not owned by CMultiViewsAppUi object. */ CMultiViewsView2* iAppView2;
}


第二步, 在 AppUi::ConstructL() 函数中作如下操作
AppUi::ConstructL()

    BaseConstructL(EAknEnableSkin);


    iAppView1 = CMultiViewsView1::NewL();
    iAppView2 = CMultiViewsView2::NewL();


    // Transfer ownership to base class
    AddViewL( iAppView1 );
    AddViewL( iAppView2 );


    SetDefaultViewL( *iAppView1 );



第三步,如果要实现 TAB 跳转,则需要在 AppUi::ConstructL() 中填加入下代码:


AppUi::ConstructL()

    BaseConstructL(EAknEnableSkin);


    CEikStatusPane *sp = StatusPane();   


    // Fetch pointer to the default navi pane control
    iNaviPane = static_cast<CAknNavigationControlContainer*>
( sp->ControlL( TUid::Uid( EEikStatusPaneUidNavi ) ) );
    iDecoratedTabGroup = iNaviPane->ResourceDecorator();
    if ( iDecoratedTabGroup )
    {
iTabGroup = static_cast<CAknTabGroup*>
( iDecoratedTabGroup->DecoratedControl() );
    }


    iAppView1 = CMultiViewsView1::NewL();
    iAppView2 = CMultiViewsView2::NewL();


    // Transfer ownership to base class
    AddViewL( iAppView1 );
    AddViewL( iAppView2 );


    SetDefaultViewL( *iAppView1 );



第三步, 处理用户按键,如果是方向键,则切换视图。
TKeyResponse AppUi::HandleKeyEventL(
            const TKeyEvent& aKeyEvent, TEventCode aType)
{


    if(iTabGroup == NULL)
    {
        return EKeyWasNotConsumed;
    }


    //如果按下的是左方向键或右方向键
    if(aKeyEvent.iCode==EKeyLeftArrow || aKeyEvent.iCode== EKeyRightArrow)
    {
        return iTabGroup->OfferKeyEventL(aKeyEvent, aType);
    }
    else
    {
        return EKeyWasNotConsumed;
    }



第四步, 重载 TabChangedL 方法,这是一个回调函数
void AppUi::TabChangedL(TInt aIndex)
{
  ActivateLocalViewL(
TUid::Uid(iTabGroup->TabIdFromIndex(aIndex))
      );//激活视图
}


第五步, 视图类继承自 CAknView。 如下所示
class View: public CAknView

......


public: // Functions from base classes from CAknView


        /**
        * Id
        * @return Id Uid value
        */
        TUid Id() const;


        /**
        * HandleCommandL
        * From CAknView, takes care of command handling.
        * @param aCommand Command to be handled
        */
        void HandleCommandL( TInt aCommand );
       
        /**
        * HandleSizeChange
        * Called by HandleResourceChangeL() from CMultiViewsAppUi when layout
        * is changed.
        * @param aType Type of resources that have changed
        */
        void HandleSizeChange( TInt aType );


        /**
        * DoActivateL
        * From CAknExView, activate an AknView.
        * @param aPrevViewId The id of the previous view
        * @param aCustomMessageId message identifier
        * @param aCustomMessage custom message provided when the view is changed
        */
        void DoActivateL( const TVwsViewId& aPrevViewId,
                          TUid aCustomMessageId,
                          const TDesC8& aCustomMessage );


        /**
        * DoDeactivate
        * From AknView, deactivate an AknView
        * Remove the container class instance from the App UI's stack and
        * deletes the instance
        */
        void DoDeactivate();


private: // Data


        /**
        * iContainer,container for this view
        * owned by CMultiViewsView1 object.
        */
        CMultiViewsContainer1* iContainer;


        /** View Identifier **/
        TUid iIdentifier;



第六步,实现部分


// -----------------------------------------------------------------------------
// CMultiViewsView1::Id()
// Returns View's ID.
// -----------------------------------------------------------------------------
//
TUid CMultiViewsView1::Id() const
    {
    return TUid::Uid( EMultiViewsView1Id );
    }


// -----------------------------------------------------------------------------
// CMultiViewsView1::DoActivateL()
// Activate an MultiView1
// -----------------------------------------------------------------------------
//
void CMultiViewsView1::DoActivateL( const TVwsViewId& /*aPrevViewId*/,
                                    TUid /*aCustomMessageId*/,
                                    const TDesC8& /*aCustomMessage*/)
    {
    iContainer = CMultiViewsContainer1::NewL( ClientRect() );
    }


// -----------------------------------------------------------------------------
// CMultiViewsView1::DoDeactivate()
// DeActivate an MultiView1
// -----------------------------------------------------------------------------
//
void CMultiViewsView1::DoDeactivate()
    {


    if ( iContainer )
        {
        // Remove View1's container form controllStack
        AppUi()->RemoveFromStack( iContainer );
        delete iContainer;
        iContainer = NULL;
        }
    }


// -----------------------------------------------------------------------------
// CMultiViewsView1::HandleCommandL()
// Takes care of Command handling.
// -----------------------------------------------------------------------------
//
void CMultiViewsView1::HandleCommandL( TInt aCommand )
    {


    if ( aCommand == EMultiViewsSwitchToView2 )
        {
        AppUi()->ActivateLocalViewL( TUid::Uid( EMultiViewsView2Id ) );
        }
    else
        {
        AppUi()->HandleCommandL( aCommand );
        }
    }


// -----------------------------------------------------------------------------
// CMultiViewsView1::HandleSizeChange()
// Called by HandleResourceChangeL() from CMultiViewsAppUi when layout is
// changed.
// -----------------------------------------------------------------------------
//
void CMultiViewsView1::HandleSizeChange( TInt aType )
    {
    if( iContainer )
        {
        iContainer->HandleResourceChange( aType );
       
        if ( aType==KEikDynamicLayoutVariantSwitch )
            {       
            TRect rect;
            AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, rect);
            iContainer->SetRect(rect);
            }
        }       
    }


第七步,在资源文件中加入TAB资源


RESOURCE EIK_APP_INFO
    {
status_pane = r_multiviewstest_status_pane;
    }


RESOURCE STATUS_PANE_APP_MODEL r_multiviewstest_status_pane
    {
    panes =
        {
          SPANE_PANE
              {
              id = EEikStatusPaneUidNavi;
              type = EAknCtNaviPane;
              resource = r_multiviewstest_navi_decorator;
              }
        };
    }


//    r_multiviewstest_navi_decorator
RESOURCE NAVI_DECORATOR r_multiviewstest_navi_decorator
    {
    type = ENaviDecoratorControlTabGroup;
    control = TAB_GROUP
        {
          tab_width = EAknTabWidthWithTwoTabs; // two tabs
        active = 0;
        tabs = {
              TAB
              {
                id = EMultiViewsTestView1Tab; // from application hrh                   
                txt = qtn_view1_tab;
              },
              TAB
              {
                id = EMultiViewsTestView2Tab;
                txt = qtn_view2_tab;
              }
              };
        };
    }


第八步,在头文件 multiviews.hrh 中加入枚举值
enum TMultiViewsTestTabViewId
{
    EMultiViewsTestView1Tab= 1,
    EMultiViewsTestView2Tab
};
转贴自:http://bbs.tech.ccidnet.com/read.php?tid=678157