赞
踩
TableView For Android
TableView is a powerful Android library for displaying complex data structures and rendering tabular data composed of rows, columns and cells. TableView relies on a separate model object to hold and represent the data it displays. This repository also contains a sample app that is designed to show you how to create your own TableView in your application.
Demo Full video »
Features
Each column width value can be calculated automatically considering the largest one
Setting your own model class to displayed in a table view easily.
TableView has an action listener interface to listen user touch interaction for each cell.
TableView columns can be sorted in ascending or descending order.
Hiding & Showing the row and the column is pretty easy.
Filtering by more than one data.
Pagination functionality.
What's new
You can check new implementations of TableView on the release page.
Table of Contents
Installation
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'
}
Implement your item on TableView
1. Create your TableView
XML
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:
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"
Programmatically
TableView tableView = new TableView(getContext());
2. Create your TableViewAdapter
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 {
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 "getColumnHeaderI
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。