Skip to content
This repository was archived by the owner on May 9, 2018. It is now read-only.

Latest commit

History

90 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

THIS PROJECT IS NO LONGER MAINTAINED

Please refer to Android docs to customize lists depending on your needs.

ATableView

ATableView

Summary

ATableView intends to imitate same object model proposed on UIKit for building tables, so it's not only limited on theming Android ListView. If you've some background on iOS development you may jump over some of the sections below, you'll find a lot of similarities with the native framework.

If not, you should be good with the examples included here and the demo project.

Updates

Documentation is a little outdated, please use it only for reference and stay close to the examples included on the demo project. Feel free to submit a bug if you find any issues using it.

How to use it

Creating tables

@OverridepublicvoidonCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// ATableViewStyle.Plain & Grouped supported.ATableViewtableView = newATableView(ATableViewStyle.Grouped, this);
// don't forget to set the datasource, otherwise you'll get an exception.// it must be an object extending ATableViewDataSource, or ATableViewDataSourceExt (more on this later).tableView.setDataSource(newSampleATableViewDataSource());
// delegates are optional, it must extend ATableViewDelegate.tableView.setDelegate(newSampleATableViewDelegate());
FrameLayoutcontainer = (FrameLayout)findViewById(android.R.id.content);
container.addView(tableView);
}

Implementing a data source

It's your responsability to implement the required methods when extending ATableViewDataSource. The following are supported:

publicATableViewCellcellForRowAtIndexPath(ATableViewtableView, NSIndexPathindexPath); [Required]
publicintnumberOfRowsInSection(ATableViewtableView, intsection); [Required]
publicintnumberOfSectionsInTableView(ATableViewtableView);

More on how this methods works can be found on the iOS UITableViewDataSource Protocol Reference.

Example

@OverridepublicATableViewCellcellForRowAtIndexPath(ATableViewtableView, NSIndexPathindexPath) {
finalStringcellIdentifier = "CellIdentifier";
// ATableViewCellStyle.Default, Subtitle, Value1 & Value2 supported.ATableViewCellStylestyle = ATableViewCellStyle.Default;
// reuse cells. if the table has different row types it will result on performance issues.// Use ATableViewDataSourceExt on this cases.// please notice we ask the datasource for a cell instead the table as we do on ios.ATableViewCellcell = dequeueReusableCellWithIdentifier(cellIdentifier);
if (cell == null) {
cell = newATableViewCell(style, cellIdentifier, MainActivity.this);
// ATableViewCellSelectionStyle.Blue, Gray & None supported. It defaults to Blue.cell.setSelectionStyle(ATableViewCellSelectionStyle.Blue);
}
// set title.cell.getTextLabel().setText("Buenos Aires");
// set detail text. careful, detail text is not present on every cell style.// null references are not as neat as in obj-c.TextViewdetailTextLabel = cell.getDetailTextLabel();
if (detailTextLabel != null) {
detailTextLabel.setText("Argentina");
}
returncell;
}
@OverridepublicintnumberOfRowsInSection(ATableViewtableView, intsection) {
// return number of rows for this section.if (section == 1) {
return4;
}
return2;
}
@OverridepublicintnumberOfSectionsInTableView(ATableViewtableView) {
// defaults to 1.return2;
}

Table styles (ATableViewStyle)

All UITableViewStyle styles are supported. These are:

  • ATableViewStyle.Plain
  • ATableViewStyle.Grouped

Creating cells

You can use cell styles included by default, or extend ATableViewCell to create your own cells.

Cell styles (ATableViewCellStyle)

All UITableViewCellStyles styles are supported. These are:

  • ATableViewCellStyle.Default
  • ATableViewCellStyle.Subtitle
  • ATableViewCellStyle.Value1
  • ATableViewCellStyle.Value2

ATableViewCellStyle

Cell selection styles (ATableViewCellSelectionStyle)

All UITableViewCellSelectionStyle styles are supported These are:

  • ATableViewCellSelectionStyle.None
  • ATableViewCellSelectionStyle.Blue (Default)
  • ATableViewCellSelectionStyle.Gray

ATableViewCellSelectionStyle

Adding images to cells (imageView)

imageView is shown automatically when an image is defined for it, otherwise is hidden by default on each cell.

imageView

Example

Drawabledrawable = getResources().getDrawable(R.drawable.some_image);
cell.getImageView().setImageDrawable(drawable);

Creating custom cells

Creating custom cells is as simple as extending ATableViewCellStyle and indicate the layout you want your cell to use.

Example

publicclassMyCustomCellextendsATableViewCell {
privateUILabelmCustomLabel;
protectedintgetLayout(ATableViewCellStylestyle) {
// here it goes your custom cell layout.returnR.layout.my_custom_cell;
}
publicMyCustomCell(ATableViewCellStylestyle, StringreuseIdentifier, Contextcontext) {
super(style, reuseIdentifier, context);
mCustomLabel = (UILabel)findViewById(R.id.custom_cell_label);
}
publicUILabelgetCustomLabel() {
returnmCustomLabel;
}
}

Implementing a delegate

Adding a delegate to the table is optional. UITableViewDelegate defines many methods to describe how the table should look and behave. Only a few of them are currently supported on ATableViewDelegate. These are:

publicvoiddidSelectRowAtIndexPath(ATableViewtableView, NSIndexPathindexPath);
publicintheightForRowAtIndexPath(ATableViewtableView, NSIndexPathindexPath);

Example

@OverridepublicvoiddidSelectRowAtIndexPath(ATableViewtableView, NSIndexPathindexPath) {
// do something when the row is selected. rows are identified by its indexPath. 
}
@OverridepublicintheightForRowAtIndexPath(ATableViewtableView, NSIndexPathindexPath) {
// return height size on dip. defaults to 44 if not implemented.return54;
}

Table data source additional methods (ATableViewDataSourceExt)

On the case you need to use different cell styles on the same table, you should extend ATableViewDataSourceExt to avoid performance issues when scrolling. This is necessary since Android ListView uses different pools for reusing cells, a pool for each cell type. ATableViewDataSourceExt is used to specify how many rows and pools should be created to run smoothly.

You'll have additionally to implement the following methods:

publicintnumberOfRowStyles(); [Required]
publicintstyleForRowAtIndexPath(NSIndexPathindexPath); [Required]

Example

@OverridepublicintnumberOfRowStyles() {
// number of different rows on the table.return4;
}
@OverridepublicintstyleForRowAtIndexPath(NSIndexPathindexPath) {
// integer identifying the style for a cell at a given indexPath.returnmyOwnImplementationGetStyle(indexPath);
}

Extra stuff

UILabel

UILabel uses internally Roboto font, which make labels look-alike in iOS and it's rendered great by Android. Roboto font is available from ICS and above, so it should be included on your project /assets folder in order to make it work for any version.

Both Roboto-Regular.ttf and Roboto-Bold.ttf are included on the demo project. On the case you don't include these files, text will render using the default font available.

License

Copyright 2012 Diego Acosta - Contact me at diegonake@gmail.com / @nakardo

Released under the Apache 2.0. license.

Roboto font by Google Android - Licensed under the Apache License.

Awesome Android PSD images by slaveoffear.

About

[Deprecated] Create iOS-like tables (UITableView) for Android, using UIKit object model.

Resources

Stars

181 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages