转载请注明出处:王亟亟的大牛之路
现在各种公司财务流程走到了手机端啊pad这一些产品上面,安卓画类似xls的库并不是太多(可能我见识少),看到一个Git上的库,觉得蛮好用的,尝试性的移植到了产品中去,写一下使用的心得
项目结构
实现效果:
Gradle:
compile 'de.codecrafters.tableview:tableview:0.9.5'
最低Minimum SDK-Version: 11 最新版本 0.9.5
如何使用这样的一个tableview?
<de.codecrafters.tableview.TableView
xmlns:table="http://schemas.android.com/apk/res-auto"
android:id="@+id/tableView"
android:layout_width="match_parent"
android:layout_height="match_parent"
table:columnCount="4" />
table:columnCount是你表单的列数,当然需要引入xmlns:table=”http://schemas.android.com/apk/res-auto”
还有其他一些属性,在attributes.xml文件里定义
<resources>
<declare-styleable name="TableView">
<attr name="columnCount" format="integer"/>
<attr name="headerElevation" format="integer"/>
<attr name="headerColor" format="color"/>
</declare-styleable>
</resources>
我拿Demo包的代码来作为解释的例子
MainActivity
public class MainActivity extends AppCompatActivity {
//放置数据的容器,每一个对象其实就是等于一行
private static List<Car> CAR_LIST = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
//自定义View绑定Id
SortableCarTableView carTableView = (SortableCarTableView) findViewById(R.id.tableView);
//填充数据
carTableView.setDataAdapter(new CarTableDataAdapter(this, CAR_LIST));
//添加监听事件
carTableView.addDataClickListener(new CarClickListener());
}
//表单内的点触时间,以行为单位
private class CarClickListener implements TableDataClickListener<Car> {
@Override
public void onDataClicked(int rowIndex, Car clickedData) {
String carString = clickedData.getProducer().getName() + " " + clickedData.getName();
Toast.makeText(MainActivity.this, carString, Toast.LENGTH_SHORT).show();
}
}
//塞数据!!!
static {
CarProducer audi = new CarProducer(R.mipmap.audi, "Audi");
Car audiA1 = new Car(audi, "A1", 150, 25000);
Car audiA3 = new Car(audi, "A3", 120, 35000);
Car audiA4 = new Car(audi, "A4", 210, 42000);
Car audiA5 = new Car(audi, "S5", 333, 60000);
Car audiA6 = new Car(audi, "A6", 250, 55000);
Car audiA7 = new Car(audi, "A7", 420, 87000);
Car audiA8 = new Car(audi, "A8", 320, 110000);
CarProducer bmw = new CarProducer(R.mipmap.bmw, "BMW");
Car bmw1 = new Car(bmw, "1er", 170, 25000);
Car bmw3 = new Car(bmw, "3er", 230, 42000);
Car bmwX3 = new Car(bmw, "X3", 230, 45000);
Car bmw4 = new Car(bmw, "4er", 250, 39000);
Car bmwM4 = new Car(bmw, "M4", 350, 60000);
Car bmw5 = new Car(bmw, "5er", 230, 46000);
CarProducer porsche = new CarProducer(R.mipmap.porsche, "Porsche");
Car porsche911 = new Car(porsche, "911", 280, 45000);
Car porscheCayman = new Car(porsche, "Cayman", 330, 52000);
Car porscheCaymanGT4 = new Car(porsche, "Cayman GT4", 385, 86000);
CAR_LIST.add(audiA3);
CAR_LIST.add(audiA1);
CAR_LIST.add(porscheCayman);
CAR_LIST.add(audiA7);
CAR_LIST.add(audiA8);
CAR_LIST.add(audiA4);
CAR_LIST.add(bmwX3);
CAR_LIST.add(porsche911);
CAR_LIST.add(bmw1);
CAR_LIST.add(audiA6);
CAR_LIST.add(audiA5);
CAR_LIST.add(bmwM4);
CAR_LIST.add(bmw5);
CAR_LIST.add(porscheCaymanGT4);
CAR_LIST.add(bmw3);
CAR_LIST.add(bmw4);
}
}
主activity布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="de.codecrafters.tableviewexample.MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primary"
custom:title="@string/title_activity_main"
custom:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<de.codecrafters.tableviewexample.SortableCarTableView
android:id="@+id/tableView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar"
custom:columnCount="4"
custom:headerElevation="10"
custom:headerColor="@color/primary" />
</RelativeLayout>
SortableCarTableView
public class SortableCarTableView extends SortableTableView<Car> {
//构造函数
public SortableCarTableView(Context context) {
this(context, null);
}
//构造函数
public SortableCarTableView(Context context, AttributeSet attributes) {
this(context, attributes, 0);
}
//构造函数
public SortableCarTableView(Context context, AttributeSet attributes, int styleAttributes) {
super(context, attributes, styleAttributes);
//首行菜单初始化,public SimpleTableHeaderAdapter(Context context, String... headers)
SimpleTableHeaderAdapter simpleTableHeaderAdapter = new SimpleTableHeaderAdapter(context, "Hersteller", "Bezeichung", "Leistung", "Preis");
//设置字体颜色
simpleTableHeaderAdapter.setTextColor(context.getResources().getColor(R.color.table_header_text));
setHeaderAdapter(simpleTableHeaderAdapter);
//每一个item颜色差异化的操作
int rowColorEven = context.getResources().getColor(R.color.table_data_row_even);
int rowColorOdd = context.getResources().getColor(R.color.table_data_row_odd);
setDataRowColoriser(TableDataRowColorizers.alternatingRows(rowColorEven, rowColorOdd));
setHeaderSortStateViewProvider(SortStateViewProviders.brightArrows());
//每一列在菜单的权重
setColumnWeight(0, 2);
setColumnWeight(1, 3);
setColumnWeight(2, 3);
setColumnWeight(3, 2);
//每一列的点击业务逻辑 public void setColumnComparator(int columnIndex, Comparator<T> columnComparator) ,所以之后需要创建一个class来实现具体的业务逻辑
setColumnComparator(0, CarComparators.getCarProducerComparator());
setColumnComparator(1, CarComparators.getCarNameComparator());
setColumnComparator(2, CarComparators.getCarPowerComparator());
setColumnComparator(3, CarComparators.getCarPriceComparator());
}
}
适配器CarTableDataAdapter
public class CarTableDataAdapter extends TableDataAdapter<Car> {
//字体大小
private static final int TEXT_SIZE = 14;
//格式刷
private static final NumberFormat PRICE_FORMATTER = NumberFormat.getNumberInstance();
public CarTableDataAdapter(Context context, List<Car> data) {
super(context, data);
}
@Override
public View getCellView(int rowIndex, int columnIndex, ViewGroup parentView) {
//获取每一行的对象
Car car = getRowData(rowIndex);
View renderedView = null;
//获取列的具体位置,参考XLS,CSV的理解,每一行的样式都可以不同,都可以自定义
switch (columnIndex) {
case 0:
renderedView = renderProducerLogo(car, parentView);
break;
case 1:
renderedView = renderCatName(car);
break;
case 2:
renderedView = renderPower(car, parentView);
break;
case 3:
renderedView = renderPrice(car);
break;
}
return renderedView;
}
private View renderPrice(Car car) {
String priceString = PRICE_FORMATTER.format(car.getPrice()) + " €";
TextView textView = new TextView(getContext());
textView.setText(priceString);
textView.setPadding(20, 10, 20, 10);
textView.setTextSize(TEXT_SIZE);
if (car.getPrice() < 50000) {
textView.setTextColor(0xFF2E7D32);
} else if (car.getPrice() > 100000) {
textView.setTextColor(0xFFC62828);
}
return textView;
}
private View renderPower(Car car, ViewGroup parentView) {
View view = getLayoutInflater().inflate(R.layout.table_cell_power, parentView, false);
TextView kwView = (TextView) view.findViewById(R.id.kw_view);
TextView psView = (TextView) view.findViewById(R.id.ps_view);
kwView.setText(car.getKw() + " kW");
psView.setText(car.getPs() + " PS");
return view;
}
private View renderCatName(Car car) {
return renderString(car.getName());
}
private View renderProducerLogo(Car car, ViewGroup parentView) {
View view = getLayoutInflater().inflate(R.layout.table_cell_image, parentView, false);
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
imageView.setImageResource(car.getProducer().getLogo());
return view;
}
private View renderString(String value) {
TextView textView = new TextView(getContext());
textView.setText(value);
textView.setPadding(20, 10, 20, 10);
textView.setTextSize(TEXT_SIZE);
return textView;
}
}
排序用CarComparators
每一列可实现的业务逻辑在此编写,但是你也可以不变写这部分,根据你的具体业务需要
public class CarComparators {
public static Comparator<Car> getCarProducerComparator() {
return new CarProducerComparator();
}
public static Comparator<Car> getCarPowerComparator() {
return new CarPowerComparator();
}
public static Comparator<Car> getCarNameComparator() {
return new CarNameComparator();
}
public static Comparator<Car> getCarPriceComparator() {
return new CarPriceComparator();
}
private static class CarProducerComparator implements Comparator<Car> {
@Override
public int compare(Car car1, Car car2) {
return car1.getProducer().getName().compareTo(car2.getProducer().getName());
}
}
private static class CarPowerComparator implements Comparator<Car> {
@Override
public int compare(Car car1, Car car2) {
return car1.getPs() - car2.getPs();
}
}
private static class CarNameComparator implements Comparator<Car> {
@Override
public int compare(Car car1, Car car2) {
return car1.getName().compareTo(car2.getName());
}
}
private static class CarPriceComparator implements Comparator<Car> {
@Override
public int compare(Car car1, Car car2) {
if (car1.getPrice() < car2.getPrice()) return -1;
if (car1.getPrice() > car2.getPrice()) return 1;
return 0;
}
}
}
源码地址:http://yunpan.cn/cmZb5Z6UhR5KN 访问密码 ad37