Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.aiyunxiao:tableview:1.0.1'
}
dependencies {
implementation("com.github.aiyunxiao:tableview:1.0.1")
}
<dependency>
<groupId>com.github.aiyunxiao</groupId>
<artifactId>tableview</artifactId>
<version>1.0.1</version>
</dependency>
libraryDependencies += "com.github.aiyunxiao" % "tableview" % "1.0.1"
:dependencies [[com.github.aiyunxiao/tableview "1.0.1"]]
You can check new implementations of TableView on the release page.
To use this library in your android project, just simply add the following dependency into your build.gradle
dependencies {
compile 'com.evrencoskun.library:tableview:0.8.8'
}
<com.evrencoskun.tableview.TableView
android:id="@+id/content_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
As default constants can be set programmatically, it can be set by also using xml attributes of TableView like this:
<com.evrencoskun.tableview.TableView
android:id="@+id/content_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:column_header_height="@dimen/column_header_height"
app:row_header_width="@dimen/row_header_width"
app:selected_color="@color/selected_background_color"
app:shadow_color="@color/shadow_background_color"
app:unselected_color="@color/unselected_background_color"
/>
Note: To be able use these attributes on xml side, the xmlns: namespace below line should be added on layout root view. Otherwise, Android Studio gives you compile error.
xmlns:app="http://schemas.android.com/apk/res-auto"
TableView tableView = new TableView(getContext());
Firstly, you must create your custom TableView Adapter which extends from AbstractTableAdapter
class.
AbstractTableAdapter
class requires 3 different lists which represent respectively; ColumnHeader, RowHeader and Cell ViewModels.
For example:
public class MyTableViewAdapter extends AbstractTableAdapter<ColumnHeader, RowHeader, Cell> {
public MyTableViewAdapter(Context context) {
super(context);
}
/**
* This is sample CellViewHolder class
* This viewHolder must be extended from AbstractViewHolder class instead of RecyclerView.ViewHolder.
*/
class MyCellViewHolder extends AbstractViewHolder {
public final TextView cell_textview;
public MyCellViewHolder(View itemView) {
super(itemView);
cell_textview = (TextView) itemView.findViewById(R.id.cell_data);
}
}
/**
* This is where you create your custom Cell ViewHolder. This method is called when Cell
* RecyclerView of the TableView needs a new RecyclerView.ViewHolder of the given type to
* represent an item.
*
* @param viewType : This value comes from #getCellItemViewType method to support different type
* of viewHolder as a Cell item.
*
* @see #getCellItemViewType(int);
*/
@Override
public AbstractViewHolder onCreateCellViewHolder(ViewGroup parent, int viewType) {
// Get cell xml layout
View layout = LayoutInflater.from(context).inflate(R.layout.my_cell_layout,
parent, false);
// Create a Custom ViewHolder for a Cell item.
return new MyCellViewHolder(layout);
}
/**
* That is where you set Cell View Model data to your custom Cell ViewHolder. This method is
* Called by Cell RecyclerView of the TableView to display the data at the specified position.
* This method gives you everything you need about a cell item.
*
* @param holder : This is one of your cell ViewHolders that was created on
* ```onCreateCellViewHolder``` method. In this example we have created
* "MyCellViewHolder" holder.
* @param cellItemModel : This is the cell view model located on this X and Y position. In this
* example, the model class is "Cell".
* @param columnPosition : This is the X (Column) position of the cell item.
* @param rowPosition : This is the Y (Row) position of the cell item.
*
* @see #onCreateCellViewHolder(ViewGroup, int);
*/
@Override
public void onBindCellViewHolder(AbstractViewHolder holder, Object cellItemModel, int
columnPosition, int rowPosition) {
Cell cell = (Cell) cellItemModel;
// Get the holder to update cell item text
MyCellViewHolder viewHolder = (MyCellViewHolder) holder;
viewHolder.cell_textview.setText(cell.getData());
// If your TableView should have auto resize for cells & columns.
// Then you should consider the below lines. Otherwise, you can ignore them.
// It is necessary to remeasure itself.
viewHolder.ItemView.getLayoutParams().width = LinearLayout.LayoutParams.WRAP_CONTENT;
viewHolder.cell_textview.requestLayout();
}
/**
* This is sample CellViewHolder class.
* This viewHolder must be extended from AbstractViewHolder class instead of RecyclerView.ViewHolder.
*/
class MyColumnHeaderViewHolder extends AbstractViewHolder {
public final TextView cell_textview;
public MyColumnHeaderViewHolder(View itemView) {
super(itemView);
cell_textview = (TextView) itemView.findViewById(R.id.cell_data);
}
}
/**
* This is where you create your custom Column Header ViewHolder. This method is called when
* Column Header RecyclerView of the TableView needs a new RecyclerView.ViewHolder of the given
* type to represent an item.
*
* @param viewType : This value comes from "getColumnHeaderItemViewType" method to support
* different type of viewHolder as a Column Header item.
*
* @see #getColumnHeaderItemViewType(int);
*/
@Override
public AbstractViewHolder onCreateColumnHeaderViewHolder(ViewGroup parent, int viewType) {
// Get Column Header xml Layout
View layout = LayoutInflater.from(context).inflate(R.layout
.table_view_column_header_layout, parent, false);
// Create a ColumnHeader ViewHolder
return new MyColumnHeaderViewHolder(layout);
}
/**
* That is where you set Column Header View Model data to your custom Column Header ViewHolder.
* This method is Called by ColumnHeader RecyclerView of the TableView to display the data at
* the specified position. This method gives you everything you need about a column header
* item.
*
* @param holder : This is one of your column header ViewHolders that was created on
* ```onCreateColumnHeaderViewHolder``` method. In this example we have created
* "MyColumnHeaderViewHolder" holder.
* @param columnHeaderItemModel : This is the column header view model located on this X position. In this
* example, the model class is "ColumnHeader".
* @param position : This is the X (Column) position of the column header item.
*
* @see #onCreateColumnHeaderViewHolder(ViewGroup, int) ;
*/
@Override
public void onBindColumnHeaderViewHolder(AbstractViewHolder holder, Object columnHeaderItemModel, int
position) {
ColumnHeader columnHeader = (ColumnHeader) columnHeaderItemModel;
// Get the holder to update cell item text
MyColumnHeaderViewHolder columnHeaderViewHolder = (MyColumnHeaderViewHolder) holder;
columnHeaderViewHolder.column_header_textview.setText(columnHeader.getData());
// If your TableView should have auto resize for cells & columns.
// Then you should consider the below lines. Otherwise, you can ignore them.
// It is necessary to remeasure itself.
columnHeaderViewHolder.column_header_container.getLayoutParams().width = LinearLayout
.LayoutParams.WRAP_CONTENT;
columnHeaderViewHolder.column_header_textview.requestLayout();
}
/**
* This is sample CellViewHolder class.
* This viewHolder must be extended from AbstractViewHolder class instead of RecyclerView.ViewHolder.
*/
class MyRowHeaderViewHolder extends AbstractViewHolder {
public final TextView cell_textview;
public MyRowHeaderViewHolder(View itemView) {
super(itemView);
cell_textview = (TextView) itemView.findViewById(R.id.cell_data);
}
}
/**
* This is where you create your custom Row Header ViewHolder. This method is called when
* Row Header RecyclerView of the TableView needs a new RecyclerView.ViewHolder of the given
* type to represent an item.
*
* @param viewType : This value comes from "getRowHeaderItemViewType" method to support
* different type of viewHolder as a row Header item.
*
* @see #getRowHeaderItemViewType(int);
*/
@Override
public AbstractViewHolder onCreateRowHeaderViewHolder(ViewGroup parent, int viewType) {
// Get Row Header xml Layout
View layout = LayoutInflater.from(context).inflate(R.layout
.table_view_row_header_layout, parent, false);
// Create a Row Header ViewHolder
return new MyRowHeaderViewHolder(layout);
}
/**
* That is where you set Row Header View Model data to your custom Row Header ViewHolder. This
* method is Called by RowHeader RecyclerView of the TableView to display the data at the
* specified position. This method gives you everything you need about a row header item.
*
* @param holder : This is one of your row header ViewHolders that was created on
* ```onCreateRowHeaderViewHolder``` method. In this example we have created
* "MyRowHeaderViewHolder" holder.
* @param rowHeaderItemModel : This is the row header view model located on this Y position. In this
* example, the model class is "RowHeader".
* @param position : This is the Y (row) position of the row header item.
*
* @see #onCreateRowHeaderViewHolder(ViewGroup, int) ;
*/
@Override
public void onBindRowHeaderViewHolder(AbstractViewHolder holder, Object rowHeaderItemModel, int
position) {
RowHeader rowHeader = (RowHeader) rowHeaderItemModel;
// Get the holder to update row header item text
MyRowHeaderViewHolder rowHeaderViewHolder = (MyRowHeaderViewHolder) holder;
rowHeaderViewHolder.row_header_textview.setText(rowHeader.getData());
}
@Override
public View onCreateCornerView() {
// Get Corner xml layout
return LayoutInflater.from(context).inflate(R.layout.table_view_corner_layout, null);
}
@Override
public int getColumnHeaderItemViewType(int columnPosition) {
// The unique ID for this type of column header item
// If you have different items for Cell View by X (Column) position,
// then you should fill this method to be able create different
// type of CellViewHolder on "onCreateCellViewHolder"
return 0;
}
@Override
public int getRowHeaderItemViewType(int rowPosition) {
// The unique ID for this type of row header item
// If you have different items for Row Header View by Y (Row) position,
// then you should fill this method to be able create different
// type of RowHeaderViewHolder on "onCreateRowHeaderViewHolder"
return 0;
}
@Override
public int getCellItemViewType(int columnPosition) {
// The unique ID for this type of cell item
// If you have different items for Cell View by X (Column) position,
// then you should fill this method to be able create different
// type of CellViewHolder on "onCreateCellViewHolder"
return 0;
}
}
AbstractTableAdapter
class requires 3 different lists which represent respectively; ColumnHeader, RowHeader and Cell ViewModels.
Assuming that we have the 3 item lists below:
private List<RowHeader> mRowHeaderList;
private List<ColumnHeader> mColumnHeaderList;
private List<List<Cell>> mCellList;
Setting data using our TableView adapter like this:
TableView tableView = new TableView(getContext());
// Create our custom TableView Adapter
MyTableViewAdapter adapter = new MyTableViewAdapter(getContext());
// Set this adapter to the our TableView
tableView.setAdapter(adapter);
// Let's set datas of the TableView on the Adapter
adapter.setAllItems(mColumnHeaderList, mRowHeaderList, mCellList);
### 4. Set Click listener to the TableView
public class MyTableViewListener implements ITableViewListener {
/**
* Called when user click any cell item.
*
* @param cellView : Clicked Cell ViewHolder.
* @param columnPosition : X (Column) position of Clicked Cell item.
* @param rowPosition : Y (Row) position of Clicked Cell item.
*/
@Override
public void onCellClicked(@NonNull RecyclerView.ViewHolder cellView, int columnPosition, int
rowPosition) {
// Do what you want.
}
/**
* Called when user long press any cell item.
*
* @param cellView : Long Pressed Cell ViewHolder.
* @param column : X (Column) position of Long Pressed Cell item.
* @param row : Y (Row) position of Long Pressed Cell item.
*/
@Override
public void onCellLongPressed(@NonNull RecyclerView.ViewHolder cellView, int column, int row) {
// Do What you want
}
/**
* Called when user click any column header item.
*
* @param columnHeaderView : Clicked Column Header ViewHolder.
* @param columnPosition : X (Column) position of Clicked Column Header item.
*/
@Override
public void onColumnHeaderClicked(@NonNull RecyclerView.ViewHolder columnHeaderView, int
columnPosition) {
// Do what you want.
}
/**
* Called when user click any column header item.
*
* @param columnHeaderView : Long pressed Column Header ViewHolder.
* @param columnPosition : X (Column) position of Clicked Column Header item.
* @version 0.8.5.1
*/
@Override
public void onColumnHeaderLongPressed(@NonNull RecyclerView.ViewHolder columnHeaderView, int
columnPosition) {
// Do what you want.
}
/**
* Called when user click any Row Header item.
*
* @param rowHeaderView : Clicked Row Header ViewHolder.
* @param rowPosition : Y (Row) position of Clicked Row Header item.
*/
@Override
public void onRowHeaderClicked(@NonNull RecyclerView.ViewHolder rowHeaderView, int
rowPosition) {
// Do what you want.
}
/**
* Called when user click any Row Header item.
*
* @param rowHeaderView : Long pressed Row Header ViewHolder.
* @param rowPosition : Y (Row) position of Clicked Row Header item.
* @version 0.8.5.1
*/
@Override
public void onRowHeaderLongPressed(@NonNull RecyclerView.ViewHolder rowHeaderView, int
rowPosition) {
// Do what you want.
}
}
Setting the listener to the TableView
tableView.setTableViewListener(new MyTableViewListener());
TableView has a sorting functionality with 0.8.5.1 version. TableView does not store or copy the data in its TableModel; instead it maintains a map from the row indexes of the view to the row indexes of the model.
To be able use this feature on your TableView. You have to implement ISortableModel to your Cell Model. This interface wants two methods from your cell model. These are:
String getId()
Object getContent()
As you seen getContent value returns Object. TableView controls the type of the object. And It sorts by considering to the type of class. So you can sort any type of value. Such as;
AbstractSorterViewHolder helps to listen to change of sorting actions. So you can do whatever you want on any sorting state.
onSortingStatusChanged(SortState sortState)
SortState getSortState()
/**
* Enumeration value indicating the items are sorted in increasing order.
* For example, the set <code>1, 4, 0</code> sorted in
* <code>ASCENDING</code> order is <code>0, 1, 4</code>.
*/
ASCENDING,
/**
* Enumeration value indicating the items are sorted in decreasing order.
* For example, the set <code>1, 4, 0</code> sorted in
* <code>DESCENDING</code> order is <code>4, 1, 0</code>.
*/
DESCENDING,
/**
* Enumeration value indicating the items are unordered.
* For example, the set <code>1, 4, 0</code> in
* <code>UNSORTED</code> order is <code>1, 4, 0</code>.
*/
UNSORTED
Several helper methods have been inserted on TableView. These are:
sortColumn(int column, SortState sortState)
SortState getSortingStatus(int column)
As of version 0.8.6, a filtering functionality has been added.
Filtering, by definition and usage in this context, is displaying a subset of data into the TableView based on a given filter globally. on a specified column or combination.
getFilterableKeyword()
.getFilterableKeyword()
requires a String value to be returned. Make sure that this item is unique for a specific filter and without conflict to other filter Strings. ...
private TableView tableView;
private Filter tableViewFilter;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
...
tableView = setUpTableView();
tableViewFilter = new Filter(tableView);
...
}
...
...
public void filterWholeTable(String filterKeyword) {
tableViewFilter.set(filterKeyword);
}
// assuming that Gender column is the 4th column in the TableView
public void filterTableForGender(String genderFilterKeyword) {
tableViewFilter.set(3, genderFilterKeyword);
}
public void filterTableColumnForKeyword(int column, String keyword) {
tableViewFilter.set(column, keyword);
}
...
null
) to the set
method. ...
public void clearWholeTableFilter() {
tableViewFilter.set("");
}
// assuming that Gender column is the 4th column in the TableView
public void clearFilterForGender() {
tableViewFilter.set(3, "");
}
...
String.contains(CharSequence s)
, the filtering process will return all data with male keyword, thus, female is also included in the filtered data set. Better use "boy" and "girl" or the hashed values of your keyword Strings. // FilterChangedListener implementation:
...
tableView.getFilterHandler().addFilterChangedListener(filterChangedListener);
...
// The filterChangedListener variable:
private FilterChangedListener filterChangedListener =
new FilterChangedListener() {
/**
* Called when a filter has been changed.
*
* @param filteredCellItems The list of filtered cell items.
* @param filteredRowHeaderItems The list of filtered row items.
*/
@Override
public void onFilterChanged(List<List<T>> filteredCellItems, List<T> filteredRowHeaderItems) {
// do something here...
}
/**
* Called when the TableView filters are cleared.
*
* @param originalCellItems The unfiltered cell item list.
* @param originalRowHeaderItems The unfiltered row header list.
*/
@Override
public void onFilterCleared(List<List<T>> originalCellItems, List<T> originalRowHeaderItems) {
// do something here...
}
};
As of version 0.8.6, a pagination functionality has been added.
Pagination, by definition and usage in this context, is the division of the whole set of data into subsets called pages and loading the data into the TableView page-by-page and not the whole data directly. This is useful if you have a large amount of data to be displayed.
Depending on your preference, you may not follow the following and create your own implementation. 1. Create a layout with the following components: Two Button views to control next and previous page, a Spinner if you want to have a customized number of pagination (e.g. 10, 20, 50, All), an EditText to have a user input on which page s/he wants to go to, a TextView to display details (e.g. Showing page X, items Y-Z) 2. Asign the views with the controls and methods which are discussed below.
...
private TableView tableView;
private Pagination mPagination;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
...
tableView = setUpTableView();
mPagination = new Pagination(mTableView);
...
}
...
/**
* Basic constructor, TableView instance is required.
*
* @param tableView The TableView to be paginated.
*/
public Pagination(ITableView tableView) { ... }
/**
* Applies pagination to the supplied TableView with number of items per page.
*
* @param tableView The TableView to be paginated.
* @param itemsPerPage The number of items per page.
*/
public Pagination(ITableView tableView, int itemsPerPage) { ... }
/**
* Applies pagination to the supplied TableView with number of items per page and an
* OnTableViewPageTurnedListener for handling changes in the TableView pagination.
*
* @param tableView The TableView to be paginated.
* @param itemsPerPage The number of items per page.
* @param listener The OnTableViewPageTurnedListener for the TableView.
*/
public Pagination(ITableView tableView, int itemsPerPage, OnTableViewPageTurnedListener listener) { ... }
nextPage()
method. You can assign this to your implementation of nextPageButton onClick action: ...
public void nextTablePage() {
mPagination.nextPage();
}
...
previousPage()
method. You can assign this to your implementation of previousPageButton onClick action: ...
public void previousTablePage() {
mPagination.previousPage();
}
...
goToPage(int page)
method. You can assign this to the EditText field TextChanged action (using TextWatcher): ...
public void goToTablePage(int page) {
mPagination.goToPage(page);
}
...
setItemsPerPage(int itemsPerPage)
method. You can assign this to your Spinner with the number of items per page list: ...
public void setTableItemsPerPage(int itemsPerPage) {
mPagination.setItemsPerPage(itemsPerPage);
}
...
...
mPagination = new Pagination(mTableView);
mPagination.setOnTableViewPageTurnedListener(onTableViewPageTurnedListener);
...
private Pagination.OnTableViewPageTurnedListener onTableViewPageTurnedListener =
new Pagination.OnTableViewPageTurnedListener() {
/**
* Called when the page is changed in the TableView.
*
* @param numItems The number of items currently being displayed in the TableView.
* @param itemsStart The starting item currently being displayed in the TableView.
* @param itemsEnd The ending item currently being displayed in the TableView.
*/
@Override
public void onPageTurned(int numItems, int itemsStart, int itemsEnd) {
// Do something here...
// You can update a TextView to display details (e.g. Showing page X, items Y-Z)
}
};
...
/**
* Removes the OnTableViewPageTurnedListener for this Pagination.
*/
void removeOnTableViewPageTurnedListener();
/**
* @return The current page loaded to the table view.
*/
int getCurrentPage();
/**
* @return The number of items per page loaded to the table view.
*/
int getItemsPerPage();
/**
* @return The number of pages in the pagination.
*/
int getPageCount();
/**
* @return Current pagination state of the table view.
*/
boolean isPaginated();
As of version 0.8.5.1, TableView has some helper functions to change desired cell item model easily. These are the following:
addRow(int rowPosition, YourRowHeaderModel rowHeaderItem, List<YourCellItemModel> cellItems)
addRowRange(int rowPositionStart, List<YourRowHeaderModel> rowHeaderItem, List<List<YourCellItemModel>> cellItems)
removeRow(int rowPosition)
removeRowRange(int rowPositionStart, int itemCount)
changeRowHeaderItem(int rowPosition, YourRowHeaderModel rowHeaderModel)
changeRowHeaderItemRange(int rowPositionStart, List<YourRowHeaderModel> rowHeaderModelList)
changeCellItem(int columnPosition, int rowPosition, YourCellItemModel cellModel)
changeColumnHeader(int columnPosition, YourColumnHeaderModel columnHeaderModel)
changeColumnHeaderRange(int columnPositionStart, List<YourColumnHeaderModel> columnHeaderModelList)
Note:TableViewSample 2 app also shows usage of these helper methods.
With 0.8.5.1 version, hiding and showing any of row is pretty easy for TableView. For that several helper methods have been inserted on TableView.
showRow(int row)
hideRow(int row)
showAllHiddenRows()
clearHiddenRowList()
isRowVisible(int row)
With 0.8.5.5 version, hiding and showing any of column is pretty easy for TableView. For that several helper methods have been inserted on TableView.
showColumn(int column)
hideColumn(int column)
showAllHiddenColumns()
clearHiddenColumnList()
isColumnVisible(int column)
remeasureColumnWidth(int column);
tableView.setHasFixedWidth(false);
tableView.setIgnoreSelectionColors(false);
setShowHorizontalSeparators(boolean showSeparators)
setShowVerticalSeparators(boolean showSeparators)
scrollToColumnPosition(int column)
scrollToRowPosition(int row)
setColumnWidth(int columnPosition, int width)
Contributions of any kind are welcome! I would like to thank the following contributors for sharing code and making TableView library a better product.
Copyright (c) 2017 Evren Coşkun
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.