public class

ListView

extends AbsListView
java.lang.Object
   ↳ com.neomades.ui.View
     ↳ com.neomades.ui.AbsListView
       ↳ com.neomades.ui.ListView

Class Overview

A ListView is a vertical list of views, where only one view can be clicked at the same time.

Each view can be different.

Handling selection

 ItemSelectedListener listViewListener = new ItemSelectedListener() {
 	public void onItemSelected(int index, String item) {
 		// do some stuff
 	}
 }
 ListView list = new ListView();
 // add items here
 list.setItemSelectedListener(listViewListener);
 

Async ListView

In order to improve Long lists performance, prefer using ListAdapter.

A ListAdapter enable recycling item's view. Every time ListView needs to show a new item , it will call the getView() method from its adapter.

getView() takes three arguments arguments: the item position, a convertView, and the parent ViewGroup.

 public View getView(int position, View convertView, View parent) {
 	if (convertView == null) {
 		// build a new item
 		convertView = View.inflateXML(Res.layout.your_layout);
 	}
 
 	// update the convertView
 	// Warning: findView could damage performances (see ViewHolder pattern
 	// chapter)
 	TextLabel text = (TextLabel) convertView.findView(Res.id.your_textLabel);
 	text.setText("Position " + position);
 
 	return convertView;
 }
 
 

ViewHolder pattern

 public View getView(int position, View convertView, View parent) {
 	ViewHolder holder;
 	if (convertView == null) {
 		// build a new item
 		convertView = View.inflateXML(Res.layout.your_layout);
 		holder = new ViewHolder();
 		// Because findView may have poor performances
 		// It will done only once in the view build step
 		// and saved inside ViewHolder for the next use
 		holder.textLabel = (TextLabel) convertView.findView(Res.id.your_textLabel);
 		convertView.setTag(holder);
 	}
 
 	// update the convertView
 	holder = (ViewHolder) convertView.getTag();
 	holder.textLabel.setText("Position " + position);
 
 	return convertView;
 }
 
 

Cross Platform Considerations

Avoid setting MATCH_CONTENT for ListViews or ScrollViews at all times. The hierarchy of ListView is also concerned by this note. MATCH_CONTENT in the hierarchy could break down the ListView display.

Summary

XML Attributes
Attribute Name Related Method Description
listIndicatorVisible setListIndicatorVisible(boolean)  
[Expand]
Inherited XML Attributes
From class com.neomades.ui.AbsListView
From class com.neomades.ui.View
Constants
int STYLE_GROUPED A ListView whose sections present distinct groups of rows.
int STYLE_PLAIN A plain ListView.
[Expand]
Inherited Constants
From interface com.neomades.ui.Alignment
From interface com.neomades.ui.StretchMode
Public Constructors
ListView()
Creates a new list view.
ListView(int style)
Sets the iOS UITableView style.
Public Methods
void scrollToPosition(int itemPosition)
Set the scrolled position of your ListView.
void scrollToTop()
Move the scroll position to the first item of your ListView.
void setListIndicatorVisible(boolean indicatorVisible)
Displays or hide the list indicator (generally something that represents an arrow at the tight of the ListView cell).
void setPullToRefreshListener(PullToRefreshListener listener)
Sets a pull-to-refresh listener to the ListView.
void setPullToRefreshText(String text)
If a PullToRefreshListener is set, this text will be displayed at the top of the list when the user is pulling downward but not enough to trigger the pull-to-refresh event.
void setRefreshComplete()
If a PullToRefreshListener is set, notifies the ListView that the refresh is over.
void setRefreshLoadingText(String text)
If a PullToRefreshListener is set, this text will be displayed at the top of the list when a pull-to-refresh event occurred.
void setReleaseToRefreshText(String text)
If a PullToRefreshListener is set, this text will be displayed at the top of the list when the user has enough pulled downward.
void setRowHeightAdapter(RowHeightAdapter rowHeightAdapter)
Sets an adapter for row heights.
[Expand]
Inherited Methods
From class com.neomades.ui.AbsListView
From class com.neomades.ui.View
From class java.lang.Object

XML Attributes

listIndicatorVisible

Constants

public static final int STYLE_GROUPED

A ListView whose sections present distinct groups of rows. The section headers and footers do not float.

Constant Value: 1 (0x00000001)

public static final int STYLE_PLAIN

A plain ListView. Any section headers or footers are displayed as inline separators and float when the ListView is scrolled.

Constant Value: 0 (0x00000000)

Public Constructors

public ListView ()

Creates a new list view.

public ListView (int style)

Sets the iOS UITableView style. (Only for iOS)

Parameters
style STYLE_PLAIN (default) or STYLE_GROUPED

Public Methods

public void scrollToPosition (int itemPosition)

Set the scrolled position of your ListView.

If the position is higher than the item count, the ListView is scrolled to the last item.
If the position is less than 0, the ListView is scrolled to the first item.

Cross Platform considerations

On Android, ListView will scroll to the item position only if it is not already locate in the visible area.
If the item is below the visible area, it will move to the bottom of the ListView.
If the item is above the visible area, it will move to the top of the ListView.

Parameters
itemPosition the item position to scroll to

public void scrollToTop ()

Move the scroll position to the first item of your ListView.

public void setListIndicatorVisible (boolean indicatorVisible)

Displays or hide the list indicator (generally something that represents an arrow at the tight of the ListView cell).

Related XML Attributes
Parameters
indicatorVisible true to display the indicator

public void setPullToRefreshListener (PullToRefreshListener listener)

Sets a pull-to-refresh listener to the ListView. To have more precision about the pull-to-refresh mechanism, see PullToRefreshListener.

To properly configure a pull-to-refresh on the ListView, text in the header of the list can be configured. These texts will be shown in the upper part of the list when the user over-scrolls it.

Do not forget to call setRefreshComplete() in the pull-to-refresh callback to indicate to the list that the refresh is over.

Should be called before setListAdapter(ListAdapter) and related methods.

Parameters
listener non-null listener will enabled pull to refresh mode

public void setPullToRefreshText (String text)

If a PullToRefreshListener is set, this text will be displayed at the top of the list when the user is pulling downward but not enough to trigger the pull-to-refresh event.

Cross Platform considerations

This method has effect only in Android and iOS platforms.

Parameters
text the text that will be displayed at the top of the list

public void setRefreshComplete ()

If a PullToRefreshListener is set, notifies the ListView that the refresh is over. This will hide the refresh text.

Should be called at the end of onRefresh method.

public void setRefreshLoadingText (String text)

If a PullToRefreshListener is set, this text will be displayed at the top of the list when a pull-to-refresh event occurred. It may indicate to the user that the list is refreshing.

Cross Platform considerations

This method has effect only in Android and iOS platforms.

Parameters
text the text that will be displayed at the top of the list

public void setReleaseToRefreshText (String text)

If a PullToRefreshListener is set, this text will be displayed at the top of the list when the user has enough pulled downward. It may indicate to the user that he can release the list to trigger the refresh.

Cross Platform considerations

This method has effect only in Android and iOS platforms.

Parameters
text the text that will be displayed at the top of the list

public void setRowHeightAdapter (RowHeightAdapter rowHeightAdapter)

Sets an adapter for row heights.

The adapter gives an height for each row (could be different).

Parameters
rowHeightAdapter an adapter or null