Filed under Titanium Appcelerator

ViewPager 1.2

My ViewPager module has now reached version 1.2 and is pending in the Appcelerator Marketplace.
Update:
Now live in the Marketplace!

What’s new you ask?

Made quite a few changes “under the hood”, fixed a few bugs and hopefully made some smaller performance boost.

The “major” thing in this version are the added events. You can register three kind of events to the pager.

pageChange

pager.addEventListener("pageChange", function (e) {
    Ti.API.debug("page changed from " + e.from + " to " + e.to);
});

pageScroll

pager.addEventListener("pageScroll", function(e) {
    Ti.API.debug("pageScroll page: " + e.page + ", offset: " + e.offset + ", offsetPixels: " + e.offsetPx);
});

pageState

pager.addEventListener("pageState", function (e) {
    Ti.API.debug("page " + e.page + " changed state to " + e.state + " = " + e.stateString);
});

I’ve also added a bunch of properties (some getters and some setter). Like:

length – Get the number of pages in the pager
currentPage – Get the currently showing page
initalPage – Set the page to be the displayed when showing the pager

All the properties you can set on creation time can now also be set using properties. They’re named the same as the ones you already can set. If they are font or padding, just add the word before e.g. fontColor, fontSize, paddingLeft, etc.

The demo project has been updated.

I’ve also uploaded the demo application on Google Play which will make it easier for folks to try it before they buy it :)

Happy days.

Titanium ViewPager Module now available

I’m happy to announce that my ViewPager module is now available on the Appcelerator Marketplace!

Get it here

There’s been some minor changes since my last post. Unfortunately the market style tabs didn’t make it in to this release as I couldn’t get them to act the way I wanted, but they will be added soon.

Be sure to check out my demo project to get the full api of the module as this post is only to highlight some of the features.

At the very basic, this module will let you swipe between views in your Titanium Mobile application. You can optionally add tabs to the pager and also a simple indicator.

var pager = module.createViewPager({ data: pagerData });

This will create a viewpager and set the contents of pagerData as the “pages”. The data array can either be an array of TiView objects:

var pagerData = [ view1, view2 ];

Where view1 and view2 are some TiView (like a TableView as my demo). Or an array of objects with a “title” and “view” attributes.

var pagerData = [
    { title: "tab one", view: view1 },
    { title: "tab two", view: view2 }
]

Using this will automatically set the pager to use the “normal” tabs (the fixed ones) and to disable this (or to change the type of tabs you want) you’ll have to add a “tabs” attribute to the options passed into the createViewPager method.

var pager = module.createViewPager({
    data: pagerData,
    tabs: {
        style: module.NONE // or module.NORMAL or module.SCROLLING
    }
}

There are three (four when the market tabs are added) constants to use as style: NONE, NORMAL and SCROLLING check this video out to see the scrolling tabs in action. Now, obviously you might want to change the appearance of the tabs (not everyone is into the dark theme). Lucky for you, it’s as easy as adding a few attributes to the style object.

var pager = module.createViewPager({
    data: pagerData,
    tabs: {
        style: module.NONE, // or module.NORMAL or module.SCROLLING
        backgroundColor: "#ffffff",
        backgroundColorSelected: "#cccccc"
    }
}

The color values MUST be in hex starting with a hash (#), support for shorthand (like “black”, “white”, etc) might be added later. Here’s a list of styles you can set:

  • backgroundColor
  • backgroundColorSelected
  • lineColor
  • lineColorSelected
  • lineHeight
  • lineHeightSelected

These should be pretty self explanatory. All the color values must be like I said a hex string starting with a #. The heights take an integer.

You can also add a font object to the style object:

var pager = module.createViewPager({
    data: pagerData,
    tabs: {
        style: module.NONE, // or module.NORMAL or module.SCROLLING
        backgroundColor: "#ffffff",
        backgroundColorSelected: "#cccccc",
        font: {
            color: "#00ff00",
            colorSelected: "#fff000",
            size: 14
        }
    }
}

Possible attributes are:

  • color
  • colorSelected
  • size

And also a padding object. Which will set the padding of the tabs:

var pager = module.createViewPager({
    data: pagerData,
    tabs: {
        style: module.NONE, // or module.NORMAL or module.SCROLLING
        backgroundColor: "#ffffff",
        backgroundColorSelected: "#cccccc",
        font: {
            color: "#00ff00",
            colorSelected: "#fff000",
            size: 14
        },
        padding: {
            left: 20,
            top: 20,
            right: 20,
            bottom: 20
        }
    }
}

You can also add if you’d like to display an indicator at the bottom of the view. For now it’s just a line indicator (like the vanilla ICS app launcher), but more will be added in a coming update and also the ability to style it properly.

var pager = module.createViewPager({
    data: pagerData,
    tabs: {
        style: module.NONE, // or module.NORMAL or module.SCROLLING
        backgroundColor: "#ffffff",
        backgroundColorSelected: "#cccccc",
        font: {
            color: "#00ff00",
            colorSelected: "#fff000",
            size: 14
        },
        padding: {
            left: 20,
            top: 20,
            right: 20,
            bottom: 20
        }
    },
    indicator: {
        style: module.LINE // or module.NONE (or just leave it out)
    }
}

That should be all that’s needed to know to get you started using the module.

A future update, which hopefully will come within a fortnight (love that word) will include some getters/setters/properties for commonly used stuff. It will also include the “market” tabs along with some other goodies ;)

Please don’t hesitate to contact me and give as much feedback as you can muster, positive as well as negative, so that I can improve it and fix annoyances for you. And don’t be afraid to request features as well.