Ok... so I was bored so....

Uses spreadsheet.xml which contains a LinearLayout with the id
layout_spreadsheet. There are some tweaks that could be made like
making the top left cell outside the left scroll but I'm sure you can
figure that out. Allowing for dynamic sized cols/rows might be an
interesting challenge for you.

But other than that - here is a working implementation of a
spreadsheet view. Enjoy.

import java.text.DecimalFormat;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class Launcher extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.spreadsheet);

        final int ROW_HEIGHT = 50;
        final int COL_WIDTH = 80;
        final int NUM_COLS_AND_ROWS = 15; //keeping it square just
because i'm lazy

        String[] cols = new String[NUM_COLS_AND_ROWS];
        String[] rows = new String[NUM_COLS_AND_ROWS];
        String[][] data = new String[NUM_COLS_AND_ROWS]
[NUM_COLS_AND_ROWS];
        DecimalFormat twoPlaces = new DecimalFormat("0.00");
        for(int i = 0; i < NUM_COLS_AND_ROWS; i++)
        {
          cols[i] = "Col" + i;
          rows[i] = "Row" + i;
          for(int j = 0; j < NUM_COLS_AND_ROWS; j++)
          {
            data[i][j] = twoPlaces.format(Math.random() * 1000);
          }
        }


        LinearLayout layout =
(LinearLayout)findViewById(R.id.layout_spreadsheet);

        //setup left column with row labels
        LinkedScrollView lsvLeftCol = new LinkedScrollView(this);
        lsvLeftCol.setVerticalScrollBarEnabled(false); //this one will
look wrong
        TableLayout tlLeftCol = new TableLayout(this);
        TableLayout.LayoutParams tlLeftColParams = new
TableLayout.LayoutParams();
        tlLeftColParams.width= COL_WIDTH;
        tlLeftCol.setLayoutParams(tlLeftColParams);
        for(int i = -1; i < rows.length; i++)
        {
          TableRow tr = new TableRow(this);
          TextView tv = new TextView(this);
          if(i >= 0) //-1 is the blank top left cell - this should
really be outside the scroll to look right
          {
            tv.setText(rows[i]);
          }
          tr.addView(tv);
          tr.setMinimumHeight(ROW_HEIGHT);
          tlLeftCol.addView(tr);
        }
        lsvLeftCol.addView(tlLeftCol);

        //add the main horizontal scroll
        HorizontalScrollView hsvMainContent = new
HorizontalScrollView(this);
        hsvMainContent.setHorizontalScrollBarEnabled(false); //you
could probably leave this one enabled if you want

        LinearLayout llMainContent = new LinearLayout(this); //Scroll
view needs a single child
        llMainContent.setOrientation(LinearLayout.VERTICAL);

        //add the headings
        TableLayout tlColHeadings = new TableLayout(this);
        TableRow trHeading = new TableRow(this);
        trHeading.setMinimumHeight(ROW_HEIGHT);
        for(int i = 0; i < cols.length; i++)
        {
          TextView tv = new TextView(this);
          tv.setText(rows[i]);
          tv.setMinWidth(COL_WIDTH);
          trHeading.addView(tv);
        }

        tlColHeadings.addView(trHeading);
        llMainContent.addView(tlColHeadings);

        //now lets add the main content
        LinkedScrollView lsvMainVertical = new LinkedScrollView(this);
        lsvMainVertical.setVerticalScrollBarEnabled(false); //this
will not be visible most of the time anyway

        TableLayout tlMainContent = new TableLayout(this);

        for(int i = 0; i < rows.length; i++)
        {
          TableRow tr = new TableRow(this);
          tr.setMinimumHeight(ROW_HEIGHT);
          for(int j = 0; j < cols.length; j++)
          {
            TextView tv = new TextView(this);
            tv.setText(data[i][j]);
            tv.setMinWidth(COL_WIDTH);
            tr.addView(tv);
          }
          tlMainContent.addView(tr);
        }

        lsvMainVertical.addView(tlMainContent);

        llMainContent.addView(lsvMainVertical);

        hsvMainContent.addView(llMainContent);

        layout.addView(lsvLeftCol);
        layout.addView(hsvMainContent);

        //the magic
        lsvMainVertical.others.add(lsvLeftCol);
        lsvLeftCol.others.add(lsvMainVertical);
    }

    private class LinkedScrollView extends ScrollView
    {
      public boolean cascadeScroll = true;
      public ArrayList<LinkedScrollView> others = new
ArrayList<LinkedScrollView>();

      public LinkedScrollView(Context context)
      {
        super(context);
      }

      @Override
      protected void onScrollChanged(int l, int t, int oldl, int oldt)
      {
        super.onScrollChanged(l, t, oldl, oldt);

        if(cascadeScroll)
        {
          for(int i = 0; i < others.size(); i++)
          {
            others.get(i).cascadeScroll = false;
            others.get(i).scrollTo(l, t);
            others.get(i).cascadeScroll = true;
          }
        }
      }
    }
}

On Aug 31, 5:20 am, Bret Foreman <[email protected]> wrote:
> I don't think the memory load will be as bad as you imagine. I plan to
> give the user a pick-list of which columns they want to see in the
> table. In practice, they will rarely choose more than a handful.
> Likewise, there are rarely more than a few dozen rows. So total memory
> footprint is <o>200Kbytes.
>
> For now I'll just use a GridView and continue to think about my
> options.
>
> IMHO, this is a pretty glaring omission from the GridView
> functionality, since 9 times out of 10 people need to understand their
> orientation in the grid - what rows and columns they are viewing.
>
> On Aug 30, 10:58 am, Mark Murphy <[email protected]> wrote:
>
>
>
> > On Mon, Aug 30, 2010 at 1:48 PM, Bret 
> > Foremanbegin_of_the_skype_highlighting     end_of_the_skype_highlighting<[email protected]>
> >  wrote:
> > > What do you think about the difficulty of synchronizing the scrolling
> > > of the ListViews?
>
> > I think it will be difficult. I also think your solution will be
> > memory-intensive for large numbers of columns.
>
> > The "right" answer is for this to be implemented at a lower level,
> > perhaps using the code from GridView as a basis. What you really want
> > is a GridView that supports horizontal scrolling (up to a stated
> > number of columns) and pinning of certain rows/columns. Done properly,
> > the memory consumption would be limited to the visible cells -- your
> > HorizontalScrollView-of-ListViews will require memory proportional to
> > the total number of columns, not just the visible columns.
>
> > However, to paraphrase Thomas Hobbes, any solution for this will be
> > nasty, brutish, and long. GridView is ~2,000 lines of code, and you
> > want a superset of GridView capabilities.
>
> > --
> > Mark Murphy (a Commons 
> > Guy)http://commonsware.com|http://github.com/commonsguyhttp://commonsware.com/blog|http://twitter.com/commonsguy
>
> > _The Busy Coder's Guide to *Advanced* Android Development_ Version 1.9
> > Available!

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to