Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 表單庫SortableTableView詳解,詳細講解,幫您上手就用

android 表單庫SortableTableView詳解,詳細講解,幫您上手就用

編輯:關於Android編程

 

現在各種公司財務流程走到了手機端啊pad這一些產品上面,安卓畫類似xls的庫並不是太多(可能我見識少),看到一個Git上的庫,覺得蠻好用的,嘗試性的移植到了產品中去,寫一下使用的心得

項目結構
這裡寫圖片描述
實現效果:
這裡寫圖片描述

Gradle:

  compile 'de.codecrafters.tableview:tableview:0.9.5'

最低Minimum SDK-Version: 11 最新版本 0.9.5


如何使用這樣的一個tableview?

table:columnCount是你表單的列數,當然需要引入xmlns:table=”http://schemas.android.com/apk/res-auto”

還有其他一些屬性,在attributes.xml文件裡定義


    
        
        
        
    

我拿Demo包的代碼來作為解釋的例子

MainActivity

public class MainActivity extends AppCompatActivity {
    //放置數據的容器,每一個對象其實就是等於一行
    private static List 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 {

        @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布局



    

    


SortableCarTableView

public class SortableCarTableView extends SortableTableView {

    //構造函數
    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 columnComparator) ,所以之後需要創建一個class來實現具體的業務邏輯
        setColumnComparator(0, CarComparators.getCarProducerComparator());
        setColumnComparator(1, CarComparators.getCarNameComparator());
        setColumnComparator(2, CarComparators.getCarPowerComparator());
        setColumnComparator(3, CarComparators.getCarPriceComparator());
    }

}

適配器CarTableDataAdapter

public class CarTableDataAdapter extends TableDataAdapter {
    //字體大小
    private static final int TEXT_SIZE = 14;
    //格式刷
    private static final NumberFormat PRICE_FORMATTER = NumberFormat.getNumberInstance();


    public CarTableDataAdapter(Context context, List 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 getCarProducerComparator() {
        return new CarProducerComparator();
    }

    public static Comparator getCarPowerComparator() {
        return new CarPowerComparator();
    }

    public static Comparator getCarNameComparator() {
        return new CarNameComparator();
    }

    public static Comparator getCarPriceComparator() {
        return new CarPriceComparator();
    }


    private static class CarProducerComparator implements Comparator {

        @Override
        public int compare(Car car1, Car car2) {
            return car1.getProducer().getName().compareTo(car2.getProducer().getName());
        }
    }

    private static class CarPowerComparator implements Comparator {

        @Override
        public int compare(Car car1, Car car2) {
            return car1.getPs() - car2.getPs();
        }
    }

    private static class CarNameComparator implements Comparator {

        @Override
        public int compare(Car car1, Car car2) {
            return car1.getName().compareTo(car2.getName());
        }
    }

    private static class CarPriceComparator implements Comparator {

        @Override
        public int compare(Car car1, Car car2) {
            if (car1.getPrice() < car2.getPrice()) return -1;
            if (car1.getPrice() > car2.getPrice()) return 1;
            return 0;
        }
    }
}

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved