Android实现Excel表格,且表格能左右、上下滑动
1、自定义实现一个水平滚动控件HorizontalScrollView
import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.widget.HorizontalScrollView; /** * Created by StephenHe on 2016/8/4. * 实现两个HorizontalScrollView同步滚动 */ public class SyncHorizontalScrollView extends HorizontalScrollView { private View mView; public SyncHorizontalScrollView(Context context) { super(context); } public SyncHorizontalScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public SyncHorizontalScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); if (mView != null) { mView.scrollTo(l, t); } } public void setScrollView(View view) { mView = view; } }
2、自定义实现ListView让其能跟着ScrollView滚动
import android.content.Context; import android.util.AttributeSet; import android.widget.ListView; public class NoscrollListView extends ListView { public NoscrollListView(Context context) { super(context); } public NoscrollListView(Context context, AttributeSet attrs) { super(context, attrs); } public NoscrollListView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
3、ListView的每一项布局。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:id="@+id/lin_content" android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <TextView android:id="@+id/tv_data" android:layout_width="100dp" android:layout_height="50dp" android:layout_gravity="center" android:gravity="center" android:text="1"/> <View android:layout_width="1px" android:layout_height="50dp" android:background="#cccccc"/> <TextView android:layout_width="100dp" android:layout_height="50dp" android:layout_gravity="center" android:gravity="center" android:text="2"/> <View android:layout_width="1px" android:layout_height="50dp" android:background="#cccccc"/> <TextView android:layout_width="100dp" android:layout_height="50dp" android:layout_gravity="center" android:gravity="center" android:text="3"/> <View android:layout_width="1px" android:layout_height="50dp" android:background="#cccccc"/> <TextView android:layout_width="100dp" android:layout_height="50dp" android:layout_gravity="center" android:gravity="center" android:text="4"/> <View android:layout_width="1px" android:layout_height="50dp" android:background="#cccccc"/> <TextView android:layout_width="100dp" android:layout_height="50dp" android:layout_gravity="center" android:gravity="center" android:text="5"/> <View android:layout_width="1px" android:layout_height="50dp" android:background="#cccccc"/> </LinearLayout> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="vertical"> <TextView android:id="@+id/tv_left" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:gravity="center" android:text="第1行"/> </LinearLayout> </LinearLayout>
4、主Activity布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <LinearLayout android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="1" android:orientation="vertical"></LinearLayout> <View android:layout_width="1px" android:layout_height="50dp" android:background="#cccccc"/> <LinearLayout android:id="@+id/lin_header_content" android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="3" android:orientation="vertical"> <com.exceldemo.SyncHorizontalScrollView android:id="@+id/header_horizontal" android:layout_width="match_parent" android:layout_height="50dp" android:overScrollMode="never" android:scrollbars="none"> <LinearLayout android:id="@+id/lin_year_title" android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <TextView android:id="@+id/tv_data" android:layout_width="100dp" android:layout_height="50dp" android:layout_gravity="center" android:gravity="center" android:text="第1列数据"/> <View android:layout_width="1px" android:layout_height="50dp" android:background="#cccccc"/> <TextView android:layout_width="100dp" android:layout_height="50dp" android:layout_gravity="center" android:gravity="center" android:text="第2列数据"/> <View android:layout_width="1px" android:layout_height="50dp" android:background="#cccccc"/> <TextView android:layout_width="100dp" android:layout_height="50dp" android:layout_gravity="center" android:gravity="center" android:text="第3列数据"/> <View android:layout_width="1px" android:layout_height="50dp" android:background="#cccccc"/> <TextView android:layout_width="100dp" android:layout_height="50dp" android:layout_gravity="center" android:gravity="center" android:text="第4列数据"/> <View android:layout_width="1px" android:layout_height="50dp" android:background="#cccccc"/> <TextView android:layout_width="100dp" android:layout_height="50dp" android:layout_gravity="center" android:gravity="center" android:text="第5列数据"/> <View android:layout_width="1px" android:layout_height="50dp" android:background="#cccccc"/> </LinearLayout> </com.exceldemo.SyncHorizontalScrollView> </LinearLayout> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1px" android:background="#cccccc"/> <ScrollView android:id="@+id/scroll_content" android:layout_width="match_parent" android:layout_height="match_parent" android:overScrollMode="never" android:scrollbars="none"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentTop="true" android:orientation="horizontal"> <com.exceldemo.NoscrollListView android:id="@+id/lv_left" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:overScrollMode="never" android:scrollbars="none"/> <View android:layout_width="1px" android:layout_height="match_parent" android:background="#cccccc"/> <LinearLayout android:id="@+id/lin_data_content" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="3" android:orientation="vertical"> <com.exceldemo.SyncHorizontalScrollView android:id="@+id/data_horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:overScrollMode="never" android:scrollbars="none"> <com.exceldemo.NoscrollListView android:id="@+id/lv_data" android:layout_width="match_parent" android:layout_height="match_parent" android:overScrollMode="never" android:scrollbars="none"/> </com.exceldemo.SyncHorizontalScrollView> </LinearLayout> </LinearLayout> </ScrollView> </LinearLayout>
5、主Activity代码
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.LinearLayout; import android.widget.TextView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private NoscrollListView mLeft; private LeftAdapter mLeftAdapter; private NoscrollListView mData; private DataAdapter mDataAdapter; private SyncHorizontalScrollView mHeaderHorizontal; private SyncHorizontalScrollView mDataHorizontal; private List<String> mListData; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView(){ mLeft = (NoscrollListView) findViewById(R.id.lv_left); mData = (NoscrollListView) findViewById(R.id.lv_data); mDataHorizontal = (SyncHorizontalScrollView) findViewById(R.id.data_horizontal); mHeaderHorizontal = (SyncHorizontalScrollView) findViewById(R.id.header_horizontal); mDataHorizontal.setScrollView(mHeaderHorizontal); mHeaderHorizontal.setScrollView(mDataHorizontal); mListData = new ArrayList<>(); mListData.add("1"); mListData.add("2"); mListData.add("3"); mListData.add("4"); mListData.add("5"); mListData.add("6"); mListData.add("7"); mListData.add("8"); mListData.add("9"); mListData.add("10"); mListData.add("11"); mListData.add("12"); mListData.add("13"); mLeftAdapter= new LeftAdapter(); mLeft.setAdapter(mLeftAdapter); mDataAdapter = new DataAdapter(); mData.setAdapter(mDataAdapter); } class LeftAdapter extends BaseAdapter { @Override public int getCount() { return mListData.size(); } @Override public Object getItem(int position) { return mListData.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { holder = new ViewHolder(); convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_left, null); holder.tvLeft = (TextView) convertView.findViewById(R.id.tv_left); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.tvLeft.setText("第" + position + "行"); return convertView; } class ViewHolder { TextView tvLeft; } } class DataAdapter extends BaseAdapter { @Override public int getCount() { return mListData.size(); } @Override public Object getItem(int position) { return mListData.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if(convertView == null){ holder = new ViewHolder(); convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_data, null); holder.tvData = (TextView) convertView.findViewById(R.id.tv_data); holder.linContent = (LinearLayout) convertView.findViewById(R.id.lin_content); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } return convertView; } class ViewHolder { TextView tvData; LinearLayout linContent; } } }
6、效果图,实现左侧表头能上下滑动,上面表头能左右滑动,数据部分能左右上下滑动.
posted on 2016-09-26 21:34 StephenHe 阅读(25537) 评论(7) 编辑 收藏 举报