实现表格的左列和表头固定

 

  如今不少时候都用手机来看一些工做的文档表格什么的,本身要如何实现呢,就想到了tablelayout,但毕竟手机的屏幕大小有限,而tablelayout是不支持滑动的,为了能看的完整就必须在外层嵌套scrollView和HorizontalScrollView布局进行左右滑动。但是又有另外一个问题产生了,就是当表格较大时,滑动后会发现你看到的全是数据,可是你并不清楚这些数据是属于哪一行的或是哪一列,而后你又得滑到最左边或最顶上,显得很麻烦,也容易看错。因此就有的表格的左列和表头固定的想法,这里有个关键就是固定的左列是左右滑动不移动,但能够跟随着上下滑动的,一样的固定的表头上下滑动不移动,但能够跟随着左右滑动的。下面就是解决的思路: html


<ScrollView     xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

            <HorizontalScrollView
                android:id="@+id/scroll1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >

                <TableLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:background="#ffffffff" 
                    android:id="@+id/table">
                </TableLayout>
            </HorizontalScrollView>
        
    </ScrollView>

这是一个实现了左右滑动的表格的布局,想实现左列固定只上下滑动,只要在HorizontaScrollView外ScrollView内绘制左列的布局遮住表格的左列就能够了以下: android


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:id="@+id/mFrame">

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
            <HorizontalScrollView
                android:id="@+id/scroll1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >

                <TableLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:background="#ffffffff" 
                    android:id="@+id/table">
                </TableLayout>
            </HorizontalScrollView>
            <TableLayout android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/left">
    </ScrollView>
</FrameLayout>

若是只单独实现左列固定,或是表头固定用这个方式就能够了,若是要同时都实现,好比还想实现表头固定,实现的思路就是绘制一个表头布局覆盖表格的表头,并实现表格和覆盖的表头同步左右滑动。看下这篇文章的说明就明白了http://www.cnblogs.com/devinzhang/archive/2012/07/13/2590222.html。关键就是重写HorizontaScrollView,使滑动时互相调用对方的onScrollChanged方法达到同时滑动。 布局

相信看了以后你们是可以明白该如何实现的 spa

相关文章
相关标签/搜索