A Vector containing the list of our monochrome views are to be placed in the scroll view. Here's how they are created:
Code:
public MyView () {
super ();
initWithFrame (CGRect. CGRectMake (0, 0, 320, 380));
/ *
* Construction Views
* /
Vector colors = new Vector <UIColor> <UIColor> ();
colors.add (UIColor. blueColor ());
colors.add (UIColor. whiteColor ());
colors.add (UIColor. redColor ());
colors.add (UIColor. greenColor ());
viewControllers = new Vector <UIViewController> ();
for (int i = 0; i <colors.size (); i + +)
{
// New UIViewController
UIViewController viewCtr UIViewController = new ();
viewCtr.init ();
// New UIView
UIView view = new UIView ();
view.initWithFrame (CGRect. CGRectMake (0, 0, 320, 380));
view.setBackgroundColor (colors.elementAt (i));
// Add a label
UILabel UILabel label = new ();
label.initWithFrame (CGRect. CGRectMake (0, 100, 320, 40));
label.setBackgroundColor (UIColor. clearColor ());
label.setTextColor (UIColor. blackColor ());
label.setTextAlignment (UITextAlignment. UITextAlignmentCenter);
label.setText ("PAGE" + (i +1));
view.addSubview (label);
viewCtr.setView (view);
viewControllers. add (viewCtr);
}
A UILabel is included on the page to indicate what page it is to see that the control page returns a good value.
Code:
/ *
* PageControl
* /
PageControl UIPageControl = new () {
@ Override
public ControlEvent void () {
changePage (this. currentPage ());
}
};
PageControl. initWithFrame (CGRect. CGRectMake (0, 385, 320, 31));
PageControl. setNumberOfPages (viewControllers. size ());
PageControl. setCurrentPage (0);
PageControl. addTargetActionForControlEvents (UIControlEvents. UIControlEventValueChanged);
addSubview (PageControl);
The page control is initialized to manage 4 pages, starting at page 0 and reacts to the event UIControlValueChanged. When this event occurs, its method ControlEvent () is called. This is how the paradigm UIKit Target-Action has been adapted to Java. This event is triggered whenever the currentPage attribute will be changed. We'll see where this is the case in the next section with the section of scroll view delegate. When this event occurs, it calls the method changePage ().
Bookmarks