android中动态添加tableview,TableView一个功能强大的Android库,用于显示复杂的数据结构和表格数据...

625332134c6f4d4600884b99daebf603.png

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 »

625332134c6f4d4600884b99daebf603.png

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值