- package com.rytong.mylist;
-
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Random;
-
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.Color;
- import android.graphics.Rect;
- import android.os.Bundle;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AbsListView;
- import android.widget.BaseAdapter;
- import android.widget.HorizontalScrollView;
- import android.widget.LinearLayout;
- import android.widget.LinearLayout.LayoutParams;
- import android.widget.ListView;
- import android.widget.TextView;
-
- public class SuperTables extends Activity {
-
- public static final LayoutParams FULL = new LayoutParams(
- LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
- public static final LayoutParams DEFINDELP = new LayoutParams(100, 200);
- public static final LayoutParams TEXTLP = new LayoutParams(100, 50);
- public static final int TEXTSIZE = 20;
- private LinearLayout mainLly;
- private ListView leftLst;
- private LinearLayout leftLly;
-
- private HorizontalScrollView rightHsv;
- private LinearLayout rightLly;
- private ListView rightLst;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
-
- super.onCreate(savedInstanceState);
- mainLly = new LinearLayout(this);
- mainLly.setLayoutParams(FULL);
- mainLly.setOrientation(LinearLayout.HORIZONTAL);
- initLeft();
- initRight();
- setCascading();
- setContentView(mainLly);
- }
-
- private void initLeft() {
- leftLly = new LinearLayout(this);
- leftLly.setOrientation(LinearLayout.VERTICAL);
- leftLly.setLayoutParams(DEFINDELP);
- leftLst = new ListView(this);
- leftLst.setLayoutParams(FULL);
-
- LinearLayout leftHead = new LinearLayout(this);
-
- TextView tv = new TextView(this);
- tv.setText("title");
- tv.setTextSize(TEXTSIZE);
- tv.setLayoutParams(TEXTLP);
- leftHead.addView(tv);
- leftLly.addView(leftHead);
-
- leftLst.setAdapter(new LeftLstAdapter(this, new String[] { "0.0.0.0",
- "1.1.1.1", "2.2.2.2", "3.3.3.3", "4.4.4.4", "5.5.5.5",
- "6.6.6.6", "7.7.7.7", "8.8.8.8", "9.9.9.9" }));
-
-
- leftLst.setCacheColorHint(Color.TRANSPARENT);
- leftLst.setAlwaysDrawnWithCacheEnabled(true);
-
- leftLst.setVerticalScrollBarEnabled(false);
- leftLly.addView(leftLst);
- leftLly.setBackgroundColor(Color.WHITE);
- mainLly.addView(leftLly);
- getWindow().getDecorView();
- }
-
- public class LeftLstAdapter extends BaseAdapter {
- private Context context_;
- private String[] data_;
-
- public LeftLstAdapter(Context context, String[] data) {
-
- this.context_ = context;
- this.data_ = data;
- }
-
- @Override
- public int getCount() {
-
- return data_.length;
- }
-
- @Override
- public Object getItem(int position) {
-
- return data_[position];
- }
-
- @Override
- public long getItemId(int position) {
-
- return position;
- }
-
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
-
- LinearLayout lly = new LinearLayout(context_);
- TextView tv = new TextView(context_);
- tv.setLayoutParams(TEXTLP);
- tv.setText(data_[position]);
- tv.setTextSize(TEXTSIZE);
- lly.addView(tv);
- return lly;
- }
-
- }
-
- public static final String[] TITLES = new String[] { "name", "sex", "age",
- "school", "salary", "spouse" };
-
- private void initRight() {
- rightHsv = new HorizontalScrollView(this);
- rightHsv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
- 200));
- rightLly = new LinearLayout(this);
- rightLly.setOrientation(LinearLayout.VERTICAL);
- rightLly.setLayoutParams(FULL);
-
- LinearLayout lly = new LinearLayout(this);
- lly.setLayoutParams(new LayoutParams(100 * TITLES.length, 50));
- lly.setOrientation(LinearLayout.HORIZONTAL);
- TextView t = null;
- LinearLayout ly = null;
- for (int i = 0; i < TITLES.length; i++) {
- ly = new LinearLayout(this);
- ly.setLayoutParams(TEXTLP);
- t = new TextView(this);
- t.setText(TITLES[i]);
- t.setTextSize(TEXTSIZE);
- ly.addView(t);
-
- lly.addView(ly);
- }
- rightLly.addView(lly);
- rightLst = new ListView(this);
- rightLst.setAdapter(new RightAdapter(this, getData()));
- rightLly.addView(rightLst);
- rightHsv.addView(rightLly);
- rightHsv.setPadding(20, 0, 0, 0);
- mainLly.addView(rightHsv);
- }
-
-
- private void setCascading() {
- rightLst.setOnTouchListener(new View.OnTouchListener() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
-
- leftLst.dispatchTouchEvent(event);
- return false;
- }
- });
-
- leftLst.setOnScrollListener(new AbsListView.OnScrollListener() {
-
- @Override
- public void onScrollStateChanged(AbsListView view, int scrollState) {
-
- }
-
- @Override
- public void onScroll(AbsListView view, int firstVisibleItem,
- int visibleItemCount, int totalItemCount) {
- if (leftLst.getChildAt(0) != null) {
- Rect r = new Rect();
- leftLst.getChildVisibleRect(leftLst.getChildAt(0), r, null);
- rightLst.setSelectionFromTop(
- leftLst.getFirstVisiblePosition(), r.top);
- }
-
- }
- });
-
- }
-
- public List<HashMap<String, String>> getData() {
- List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
- HashMap<String, String> dataItem = null;
- Random rd = new Random();
- for (int i = 0; i < 9; i++) {
- dataItem = new HashMap<String, String>();
- for (int j = 0; j < TITLES.length; j++) {
- dataItem.put(TITLES[j], i + "");
- }
- data.add(dataItem);
- }
-
- return data;
- }
-
- public class RightAdapter extends BaseAdapter {
-
- Context context_;
- List<HashMap<String, String>> data_;
-
- public RightAdapter(Context context, List<HashMap<String, String>> data) {
-
- this.context_ = context;
- this.data_ = data;
- }
-
- @Override
- public int getCount() {
-
- return data_.size();
- }
-
- @Override
- public Object getItem(int position) {
-
- return data_.get(position);
- }
-
- @Override
- public long getItemId(int position) {
-
- return position;
- }
-
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- LinearLayout lly = new LinearLayout(context_);
- lly.setOrientation(LinearLayout.HORIZONTAL);
- TextView tv = null;
- for (int i = 0, size = data_.get(position).size(); i < size; i++) {
- tv = new TextView(context_);
- tv.setLayoutParams(TEXTLP);
- tv.setText(data_.get(position).get(TITLES[i]));
- tv.setTextSize(TEXTSIZE);
- lly.addView(tv);
- }
- return lly;
- }
-
- }
-
- }
android 带表头,左右两个联动的ListView,布布扣,bubuko.com
android 带表头,左右两个联动的ListView