×
Namespaces

Variants
Actions

How to Create a Scrollable Container

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Article
Created: kavitaobhan (29 Nov 2007)
Last edited: hamishwillee (30 May 2013)

Contents

Need for a scrollable container

Now a days many controls are present in a single view making the visibility of the control an point to argue on due to the limited screen dimensions. One way to overcome the problem is by placing all the controls over the container and when you want to bring the particular control in the view you can just move all the controls by the offset value upwards and downwards But doing this is tedious and might be difficult to maintain in the future and not a good logic . The other way around is to use the property of CCoeControl to move entirely up and down along with its contents( moving the entire container up and down ) instead of moving all the controls. Below is the way to do it.

Note :- for simplicity functions related to scrolling are only implemented.

Creating the scrollable container

You need to create two compound controls CParentContainer & CChildContainer.  
 class CParentContainer:public CCoeControl
{
public:
TKeyResponse OfferKeyEventL(const TKeyEvent &aKeyEvent,TEventCode aType);
 
public:
RPointerArray<CCoeControl> iParentArray;
CChildContainer* iChildContainer;
 
};
 
class CChildContainer:public CCoeControl
{
public:
TKeyResponse OfferKeyEventL(const TKeyEvent &aKeyEvent,TEventCode aType);
 
public:
RPointerArray<CCoeControl> iChildArray;
CParentContainer* iParent
};

ConstructL function of CParentContainer & CChildContainer

/*
* @ param aRect  :- Rect passed for the application
*/

void CParentContainer :: ConstructL(TRect &aRect)
{
CreateWindowL();
iChildContainer = CChildContainer::NewL( aRect);
iParentArray.Append(iChildContainer);
ActivateL();
 
}
 
/*
* @param aRect  :- Rect with in which all controls should be shown
*/

 
void CChildContainer::ConstructL(TRect & aRect,CParentContainer& aParent)
{
iParent = aParent;
// Create 1st control
iChildArray.Append(iControl1);
// Create 2st control
iChildArray.Append(iControl2);
// Create nth control
iChildArray.Append(iControln);
 
 
}

Performing the layout of Containers

void CParentContainer ::SetLayout()
{
SetExtent(Rect().iTl,Rect().Size());
iChildContainer-> SetLayout(Rect(),Rect().Width());
 
}
 
void CChildContainer::SetLayout(TPoint aPoint, TInt aWidth)
{
 
TInt newHeight = 0;
 
newHeight = iControl1.Size().iHeight + iControl2.Size().iHeight +....
.....+ iControln.Size().iHeight
 
// set the extent of the container to the new height
SetExtent(aPoint, TSize(aWidth, newHeight));
 
}


Scrolling Calculation

/** Scroll the page up and down */
void CChildContainer :: ScrollTheContainer(TScrollDirection aDirection, TInt aMovePixels)
{
/** Get the current position */
TPoint iPosition = Position();
/** If user press Up Key */
if(aDirection == EPageMovementUp)
{
/** move the container upward by adding aMovePixels to
Container Y co-ordinate */

}
/** if user press down key */
else if(aDirection == EPageMovementDown)
{
/** move the container downward by subtracting aMovePixels to
Container Y co-ordinate */

}
/** Set the position */
SetPosition(iPosition);
 
}
 
 
TBool CChildContainer :: UpMovementPossible()
{
if(Position().iY >= iParent.Rect().iTl.iY)
return EFalse;
else
return ETrue;
}
 
 
TBool CChildContainer:: DownMovementPossible()
{
if(Rect().Height() + Position().iY < iParent.Rect().iBr.iY )
return EFalse;
else
return ETrue;
}

OfferKeyEventL

TKeyResponse CChildContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
{
if(aType != EEventKey)
{
return EKeyWasNotConsumed;
}
switch (aKeyEvent.iScanCode)
{
 
case EStdKeyUpArrow :
{
 
if(!UpMovementPossible())
{
return EKeyWasNotConsumed;
}
else
{
TInt move_pixels = iParent.Rect().Height()* 0.10;
ScrollTheContainer(EPageMovementUp, move_pixels);
return EKeyWasConsumed;
}
break;
}
 
case EStdKeyDownArrow :
{
 
 
if(!DownMovementPossible())
{
return EKeyWasNotConsumed;
}
else
{
TInt move_pixels = iParent.Rect().Height()* 0.12;
ScrollTheContainer(EPageMovementDown, move_pixels);
return EKeyWasConsumed;
}
break;
}
 
};
return EKeyWasNotConsumed;
}
 
/* Offer KeyEventL of Parent Container*/
TKeyResponse CParentContainer::OfferKeyEventL( const TKeyEvent& aKeyEvent,TEventCode aType )
{
TKeyResponse keyRes = EKeyWasNotConsumed;
if(aType != EEventKey)
{
return EKeyWasNotConsumed;
}
 
keyRes = iChildContainer->OfferKeyEventL(aKeyEvent,aType);
return keyRes;
 
// nothing to do here
 
}
This page was last modified on 30 May 2013, at 04:33.
42 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×