表格类似Excel

只是很简单的实现表格,使用GridView控件——>可以上下左右滚动,但是不能合并


直接上代码:
1、主要布局
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6. <TextView
  7. android:id="@+id/textview"
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:text="测试表格"/>
  11. <HorizontalScrollView
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content">
  14. <LinearLayout
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content"
  17. android:orientation="vertical" >
  18. <GridView
  19. android:id="@+id/table_grid"
  20. android:layout_width="match_parent"
  21. android:layout_height="match_parent"
  22. android:horizontalSpacing="1dp"
  23. android:verticalSpacing="1dp"
  24. android:numColumns="12">
  25. </GridView>
  26. </LinearLayout>
  27. </HorizontalScrollView>
  28. </LinearLayout>
2、表格中一个item的布局
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="80dp"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6. <TextView
  7. android:id="@+id/text_item"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:gravity="center"
  11. android:layout_gravity="center"
  12. android:textSize="15dp"
  13. android:textColor="#000000"></TextView>
  14. </LinearLayout>

3、Adapter代码
  1. package com.example.achartenginetest.adapter;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import android.content.Context;
  5. import android.graphics.Color;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.BaseAdapter;
  10. import android.widget.TextView;
  11. import com.example.achartenginetest.R;
  12. public class TableAdapter extends BaseAdapter {
  13. private final static int NUM_COLUMN = 12;
  14. private LayoutInflater mInflater;
  15. private List<double[]> mOriginalData;
  16. private List<String> mData;
  17. private Context mContext;
  18. public TableAdapter(Context context,List<double[]> mData){
  19. this.mInflater = LayoutInflater.from(context);
  20. this.mOriginalData = mData;
  21. this.mContext = context;
  22. toAllData();
  23. }
  24. private void toAllData(){
  25. mData = new ArrayList<String>();
  26. String[] tableTitle= new String[]{"日期","早餐前","早餐后1小时","早餐后2小时",
  27. "午餐前","午餐后1小时","午餐后2小时",
  28. "晚餐前","晚餐后1小时","晚餐后2小时",
  29. "睡前","凌晨2点"};
  30. for(int i = 0; i<tableTitle.length;++i){
  31. mData.add(tableTitle[i]);
  32. }
  33. for(int i = 0; i<mOriginalData.size();++i){
  34. double[] fixedData = fixData(mOriginalData.get(i));
  35. mData.add(""+i);
  36. for(int j = 0; j<fixedData.length; ++j){
  37. mData.add(""+fixedData[j]);
  38. }
  39. }
  40. }
  41. /**
  42. * 检查数据长度是否为11个取值点
  43. * @param data
  44. * @return 0:11个取值点,1:大于11个取值点,-1:小于11个取值点
  45. */
  46. private int checkData(double[] data){
  47. return data.length == NUM_COLUMN-1?0:(data.length<NUM_COLUMN-1?-1:1);
  48. }
  49. /**
  50. * 对数据进行修正,保证数据长度的正确性
  51. * @param data
  52. * @return
  53. */
  54. private double[] fixData(double[] data){
  55. int checkResult = checkData(data);
  56. if(checkResult == 0){
  57. return data;
  58. }else{
  59. double[] result = new double[NUM_COLUMN-1];
  60. if(checkResult == -1){
  61. for(int i =0; i<data.length;++i){
  62. result[i]=data[i];
  63. }
  64. for(int i = data.length; i<NUM_COLUMN-1; ++i){
  65. result[i] = 0;
  66. }
  67. }else{
  68. for(int i =0; i<NUM_COLUMN-1;++i){
  69. result[i]=data[i];
  70. }
  71. }
  72. return result;
  73. }
  74. }
  75. public int getDataCount() {
  76. return mOriginalData.size();
  77. }
  78. @Override
  79. public int getCount() {
  80. return mData.size();
  81. }
  82. @Override
  83. public Object getItem(int position) {
  84. return mData.get(position);
  85. }
  86. @Override
  87. public long getItemId(int position) {
  88. return position;
  89. }
  90. @Override
  91. public View getView(int position, View convertView, ViewGroup parent) {
  92. ViewHolder holder = null;
  93. if (convertView == null) {
  94. holder = new ViewHolder();
  95. convertView = mInflater.inflate(R.layout.table_item, null);
  96. holder.vText = (TextView)convertView.findViewById(R.id.text_item);
  97. convertView.setTag(holder);
  98. }else {
  99. holder = (ViewHolder)convertView.getTag();
  100. }
  101. holder.vText.setText(mData.get(position));
  102. if(position % NUM_COLUMN == 0){
  103. convertView.setBackgroundColor(Color.WHITE);
  104. }else if(position % NUM_COLUMN == 1 || position % NUM_COLUMN == 2 || position % NUM_COLUMN == 3){
  105. convertView.setBackgroundColor(Color.YELLOW);
  106. }else if(position % NUM_COLUMN == 4 || position % NUM_COLUMN == 5 || position % NUM_COLUMN == 6){
  107. convertView.setBackgroundColor(Color.GREEN);
  108. }else if(position % NUM_COLUMN == 7 || position % NUM_COLUMN == 8 || position % NUM_COLUMN == 9){
  109. convertView.setBackgroundColor(Color.BLUE);
  110. }else if(position % NUM_COLUMN == 10){
  111. convertView.setBackgroundColor(Color.rgb(97,76,32));
  112. }else if(position % NUM_COLUMN == 11){
  113. convertView.setBackgroundColor(Color.GRAY);
  114. }
  115. return convertView;
  116. }
  117. private class ViewHolder{
  118. public TextView vText = null;
  119. }
  120. }
4、Activity代码
  1. package com.example.achartenginetest.charts;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import android.app.Activity;
  5. import android.os.Bundle;
  6. import android.util.DisplayMetrics;
  7. import android.widget.GridView;
  8. import android.widget.LinearLayout;
  9. import com.example.achartenginetest.R;
  10. import com.example.achartenginetest.adapter.TableAdapter;
  11. public class TableChart extends Activity{
  12. private GridView mGridTable;
  13. private TableAdapter mAdapter;
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. this.setContentView(R.layout.activity_table);
  18. mGridTable = (GridView)this.findViewById(R.id.table_grid);
  19. List<double[]> vlist = initList();
  20. mAdapter = new TableAdapter(this,vlist);
  21. mGridTable.setAdapter(mAdapter);
  22. DisplayMetrics dm = new DisplayMetrics();
  23. getWindowManager().getDefaultDisplay().getMetrics(dm);
  24. float density = dm.density;
  25. int gridviewWidth = (int) (90 * 12* density);
  26. int itemWidth = (int) (80 * density);
  27. LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(gridviewWidth , LinearLayout.LayoutParams.MATCH_PARENT);
  28. mGridTable.setLayoutParams(params);
  29. mGridTable.setColumnWidth(itemWidth);
  30. mGridTable.setStretchMode(GridView.NO_STRETCH);
  31. mGridTable.setNumColumns(12);
  32. }
  33. private List<double[]> initList(){
  34. List<double[]> vList = new ArrayList<double[]>();
  35. vList.add(new double[]{8.5,11.2,10.3,8.2,15,10.2,8.1,12,9.8,8.6,8.5});
  36. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  37. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  38. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  39. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  40. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  41. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  42. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  43. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  44. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  45. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  46. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  47. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  48. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  49. vList.add(new double[]{8.5,11.2,10.3,8.2,15,10.2,8.1,12,9.8,8.6,8.5});
  50. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  51. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  52. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  53. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  54. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  55. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  56. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  57. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  58. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  59. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  60. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  61. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  62. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  63. vList.add(new double[]{8.5,11.2,10.3,8.2,15,10.2,8.1,12,9.8,8.6,8.5});
  64. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  65. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  66. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  67. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  68. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  69. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  70. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  71. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  72. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  73. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  74. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  75. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  76. vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
  77. return vList;
  78. }
  79. }








posted @ 2016-01-04 12:58  风倾清凌  阅读(522)  评论(0编辑  收藏  举报