歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android中實現Spreadsheet

Android中實現Spreadsheet

日期:2017/3/1 10:52:45   编辑:Linux編程

Android中要實現類似excel一樣的spreadsheet,一般的思路是想到重寫ListView控件,但經過嘗試以後發現挺復雜的。想到TableLayout也有無限擴展的功能,就嘗試用TableLayout來實現。TableLayout自身是不能滑動的,一般都是和ScrollView一起使用。橫向的滑動則使用HorizontalScrollView來實現。 經過這三個組件的搭配,就輕松的實現了spreadsheet。

源代碼:

  1. package com.SpreadSheet;
  2. import java.util.ArrayList;
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.os.Bundle;
  6. import android.widget.HorizontalScrollView;
  7. import android.widget.LinearLayout;
  8. import android.widget.ScrollView;
  9. import android.widget.TableLayout;
  10. import android.widget.TableRow;
  11. import android.widget.TextView;
  12. public class test extends Activity {
  13. /** Called when the activity is first created. */
  14. @Override
  15. public void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.spreadsheet);
  18. final int NUM_COLS_AND_ROWS = 30;
  19. String[] cols = new String[NUM_COLS_AND_ROWS];
  20. String[] rows = new String[NUM_COLS_AND_ROWS];
  21. LinearLayout layout = (LinearLayout) findViewById(R.id.layout_spreadsheet);
  22. // setup left column with row labels
  23. LinkedScrollView lsvLeftCol = new LinkedScrollView(this);
  24. lsvLeftCol.setVerticalScrollBarEnabled(false);
  25. TableLayout tlLeftCol = new TableLayout(this);
  26. TableLayout.LayoutParams tlLeftColParams = new TableLayout.LayoutParams();
  27. tlLeftCol.setLayoutParams(tlLeftColParams);
  28. for (int i = -1; i < rows.length; i++) {
  29. TableRow tr = new TableRow(this);
  30. TextView tv = new TextView(this);
  31. tv.setBackgroundResource(R.drawable.spreadsheetbg);
  32. if (i >= 0) // -1 is the blank top left cell - this should really be
  33. // outside the scroll to look right
  34. {
  35. tv.setText(rows[i]);
  36. }
  37. tr.addView(tv);
  38. tlLeftCol.addView(tr);
  39. }
  40. lsvLeftCol.addView(tlLeftCol);
  41. // add the main horizontal scroll
  42. HorizontalScrollView hsvMainContent = new HorizontalScrollView(this);
  43. hsvMainContent.setHorizontalScrollBarEnabled(false);
  44. LinearLayout llMainContent = new LinearLayout(this);
  45. llMainContent.setOrientation(LinearLayout.VERTICAL);
  46. // add the headings
  47. TableLayout tlColHeadings = new TableLayout(this);
  48. TableRow trHeading = new TableRow(this);
  49. for (int i = 0; i < cols.length; i++) {
  50. TextView tv = new TextView(this);
  51. tv.setBackgroundResource(R.drawable.spreadsheetbg);
  52. tv.setText(cols[i]);
  53. trHeading.addView(tv);
  54. }
  55. tlColHeadings.addView(trHeading);
  56. llMainContent.addView(tlColHeadings);
  57. // now lets add the main content
  58. LinkedScrollView lsvMainVertical = new LinkedScrollView(this);
  59. lsvMainVertical.setVerticalScrollBarEnabled(false);
  60. TableLayout tlMainContent = new TableLayout(this);
  61. for (int i = 0; i < rows.length; i++) {
  62. TableRow tr = new TableRow(this);
  63. for (int j = 0; j < cols.length; j++) {
  64. TextView tv = new TextView(this);
  65. tv.setBackgroundResource(R.drawable.spreadsheetbg);
  66. tv.setText("test");
  67. tr.addView(tv);
  68. }
  69. tlMainContent.addView(tr);
  70. }
  71. lsvMainVertical.addView(tlMainContent);
  72. llMainContent.addView(lsvMainVertical);
  73. hsvMainContent.addView(llMainContent);
  74. layout.addView(lsvLeftCol);
  75. layout.addView(hsvMainContent);
  76. lsvMainVertical.others.add(lsvLeftCol);
  77. lsvLeftCol.others.add(lsvMainVertical);
  78. }
  79. private class LinkedScrollView extends ScrollView {
  80. public boolean cascadeScroll = true;
  81. public ArrayList<LinkedScrollView> others = new ArrayList<LinkedScrollView>();
  82. public LinkedScrollView(Context context) {
  83. super(context);
  84. }
  85. @Override
  86. protected void onScrollChanged(int l, int t, int oldl, int oldt) {
  87. super.onScrollChanged(l, t, oldl, oldt);
  88. if (cascadeScroll) {
  89. for (int i = 0; i < others.size(); i++) {
  90. others.get(i).cascadeScroll = false;
  91. others.get(i).scrollTo(l, t);
  92. others.get(i).cascadeScroll = true;
  93. }
  94. }
  95. }
  96. }
  97. }

xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/layout_spreadsheet"></LinearLayout>
Copyright © Linux教程網 All Rights Reserved