android 带表头,左右两个联动的ListView_i++

    1. package com.rytong.mylist;  
    2.   
    3. import java.util.ArrayList;  
    4. import java.util.HashMap;  
    5. import java.util.List;  
    6. import java.util.Random;  
    7.   
    8. import android.app.Activity;  
    9. import android.content.Context;  
    10. import android.graphics.Color;  
    11. import android.graphics.Rect;  
    12. import android.os.Bundle;  
    13. import android.view.MotionEvent;  
    14. import android.view.View;  
    15. import android.view.ViewGroup;  
    16. import android.widget.AbsListView;  
    17. import android.widget.BaseAdapter;  
    18. import android.widget.HorizontalScrollView;  
    19. import android.widget.LinearLayout;  
    20. import android.widget.LinearLayout.LayoutParams;  
    21. import android.widget.ListView;  
    22. import android.widget.TextView;  
    23.   
    24. public class SuperTables extends Activity {  
    25.   
    26.     public static final LayoutParams FULL = new LayoutParams(  
    27.             LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);  
    28.     public static final LayoutParams DEFINDELP = new LayoutParams(100200);  
    29.     public static final LayoutParams TEXTLP = new LayoutParams(10050);  
    30.     public static final int TEXTSIZE = 20;  
    31.     private LinearLayout mainLly;  
    32.     private ListView leftLst;  
    33.     private LinearLayout leftLly;  
    34.   
    35.     private HorizontalScrollView rightHsv;  
    36.     private LinearLayout rightLly;  
    37.     private ListView rightLst;  
    38.   
    39.     @Override  
    40.     protected void onCreate(Bundle savedInstanceState) {  
    41.         // TODO Auto-generated method stub  
    42.         super.onCreate(savedInstanceState);  
    43.         mainLly = new LinearLayout(this);  
    44.         mainLly.setLayoutParams(FULL);  
    45.         mainLly.setOrientation(LinearLayout.HORIZONTAL);  
    46.         initLeft();  
    47.         initRight();  
    48.         setCascading();  
    49.         setContentView(mainLly);  
    50.     }  
    51.   
    52.     private void initLeft() {  
    53.         leftLly = new LinearLayout(this);  
    54.         leftLly.setOrientation(LinearLayout.VERTICAL);  
    55.         leftLly.setLayoutParams(DEFINDELP);  
    56.         leftLst = new ListView(this);  
    57.         leftLst.setLayoutParams(FULL);  
    58.         // 添加ListView头部视图  
    59.         LinearLayout leftHead = new LinearLayout(this);  
    60.         // leftHead.setLayoutParams(TEXTLP);  
    61.         TextView tv = new TextView(this);  
    62.         tv.setText("title");  
    63.         tv.setTextSize(TEXTSIZE);  
    64.         tv.setLayoutParams(TEXTLP);  
    65.         leftHead.addView(tv);  
    66.         leftLly.addView(leftHead);  
    67.         // leftLst.addHeaderView(leftHead);  
    68.         leftLst.setAdapter(new LeftLstAdapter(thisnew String[] { "0.0.0.0",  
    69.                 "1.1.1.1""2.2.2.2""3.3.3.3""4.4.4.4""5.5.5.5",  
    70.                 "6.6.6.6""7.7.7.7""8.8.8.8""9.9.9.9" }));  
    71.         //leftLst.setScrollBarStyle();  
    72.         //leftLst.setScrollBarStyle(0);  
    73.         leftLst.setCacheColorHint(Color.TRANSPARENT);  
    74.         leftLst.setAlwaysDrawnWithCacheEnabled(true);  
    75.         //隐藏滚动条  
    76.         leftLst.setVerticalScrollBarEnabled(false);  
    77.         leftLly.addView(leftLst);  
    78.         leftLly.setBackgroundColor(Color.WHITE);  
    79.         mainLly.addView(leftLly);  
    80.         getWindow().getDecorView();  
    81.     }  
    82.   
    83.     public class LeftLstAdapter extends BaseAdapter {  
    84.         private Context context_;  
    85.         private String[] data_;  
    86.   
    87.         public LeftLstAdapter(Context context, String[] data) {  
    88.             // TODO Auto-generated constructor stub  
    89.             this.context_ = context;  
    90.             this.data_ = data;  
    91.         }  
    92.   
    93.         @Override  
    94.         public int getCount() {  
    95.             // TODO Auto-generated method stub  
    96.             return data_.length;  
    97.         }  
    98.   
    99.         @Override  
    100.         public Object getItem(int position) {  
    101.             // TODO Auto-generated method stub  
    102.             return data_[position];  
    103.         }  
    104.   
    105.         @Override  
    106.         public long getItemId(int position) {  
    107.             // TODO Auto-generated method stub  
    108.             return position;  
    109.         }  
    110.   
    111.         @Override  
    112.         public View getView(int position, View convertView, ViewGroup parent) {  
    113.             // TODO Auto-generated method stub  
    114.             LinearLayout lly = new LinearLayout(context_);  
    115.             TextView tv = new TextView(context_);  
    116.             tv.setLayoutParams(TEXTLP);  
    117.             tv.setText(data_[position]);  
    118.             tv.setTextSize(TEXTSIZE);  
    119.             lly.addView(tv);  
    120.             return lly;  
    121.         }  
    122.   
    123.     }  
    124.   
    125.     public static final String[] TITLES = new String[] { "name""sex""age",  
    126.             "school""salary""spouse" };  
    127.   
    128.     private void initRight() {  
    129.         rightHsv = new HorizontalScrollView(this);  
    130.         rightHsv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,  
    131.                 200));  
    132.         rightLly = new LinearLayout(this);  
    133.         rightLly.setOrientation(LinearLayout.VERTICAL);  
    134.         rightLly.setLayoutParams(FULL);  
    135.         // 右侧ListVie标题  
    136.         LinearLayout lly = new LinearLayout(this);  
    137.         lly.setLayoutParams(new LayoutParams(100 * TITLES.length, 50));  
    138.         lly.setOrientation(LinearLayout.HORIZONTAL);  
    139.         TextView t = null;  
    140.         LinearLayout ly = null;  
    141.         for (int i = 0; i < TITLES.length; i++) {  
    142.             ly = new LinearLayout(this);  
    143.             ly.setLayoutParams(TEXTLP);  
    144.             t = new TextView(this);  
    145.             t.setText(TITLES[i]);  
    146.             t.setTextSize(TEXTSIZE);  
    147.             ly.addView(t);  
    148.             // t.setLayoutParams(TEXTLP);  
    149.             lly.addView(ly);  
    150.         }  
    151.         rightLly.addView(lly);  
    152.         rightLst = new ListView(this);  
    153.         rightLst.setAdapter(new RightAdapter(this, getData()));  
    154.         rightLly.addView(rightLst);  
    155.         rightHsv.addView(rightLly);  
    156.         rightHsv.setPadding(20000);  
    157.         mainLly.addView(rightHsv);  
    158.     }  
    159.   
    160.     // 设置联动  
    161.     private void setCascading() {  
    162.         rightLst.setOnTouchListener(new View.OnTouchListener() {  
    163.             @Override  
    164.             public boolean onTouch(View v, MotionEvent event) {  
    165.                 // TODO Auto-generated method stub  
    166.                 leftLst.dispatchTouchEvent(event);  
    167.                 return false;  
    168.             }  
    169.         });  
    170.   
    171.         leftLst.setOnScrollListener(new AbsListView.OnScrollListener() {  
    172.   
    173.             @Override  
    174.             public void onScrollStateChanged(AbsListView view, int scrollState) {  
    175.                 // TODO Auto-generated method stub  
    176.             }  
    177.               
    178.             @Override  
    179.             public void onScroll(AbsListView view, int firstVisibleItem,  
    180.                     int visibleItemCount, int totalItemCount) {  
    181.                 if (leftLst.getChildAt(0) != null) {  
    182.                     Rect r = new Rect();  
    183.                     leftLst.getChildVisibleRect(leftLst.getChildAt(0), r, null);  
    184.                     rightLst.setSelectionFromTop(  
    185.                             leftLst.getFirstVisiblePosition(), r.top);  
    186.                 }  
    187.   
    188.             }  
    189.         });  
    190.   
    191.     }  
    192.   
    193.     public List<HashMap<String, String>> getData() {  
    194.         List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();  
    195.         HashMap<String, String> dataItem = null;  
    196.         Random rd = new Random();  
    197.         for (int i = 0; i < 9; i++) {  
    198.             dataItem = new HashMap<String, String>();  
    199.             for (int j = 0; j < TITLES.length; j++) {  
    200.                 dataItem.put(TITLES[j],  i + "");//rd.nextInt(10000)  
    201.             }  
    202.             data.add(dataItem);  
    203.         }  
    204.   
    205.         return data;  
    206.     }  
    207.   
    208.     public class RightAdapter extends BaseAdapter {  
    209.   
    210.         Context context_;  
    211.         List<HashMap<String, String>> data_;  
    212.   
    213.         public RightAdapter(Context context, List<HashMap<String, String>> data) {  
    214.             // TODO Auto-generated constructor stub  
    215.             this.context_ = context;  
    216.             this.data_ = data;  
    217.         }  
    218.   
    219.         @Override  
    220.         public int getCount() {  
    221.             // TODO Auto-generated method stub  
    222.             return data_.size();  
    223.         }  
    224.   
    225.         @Override  
    226.         public Object getItem(int position) {  
    227.             // TODO Auto-generated method stub  
    228.             return data_.get(position);  
    229.         }  
    230.   
    231.         @Override  
    232.         public long getItemId(int position) {  
    233.             // TODO Auto-generated method stub  
    234.             return position;  
    235.         }  
    236.   
    237.         @Override  
    238.         public View getView(int position, View convertView, ViewGroup parent) {  
    239.             LinearLayout lly = new LinearLayout(context_);  
    240.             lly.setOrientation(LinearLayout.HORIZONTAL);  
    241.             TextView tv = null;  
    242.             for (int i = 0, size = data_.get(position).size(); i < size; i++) {  
    243.                 tv = new TextView(context_);  
    244.                 tv.setLayoutParams(TEXTLP);  
    245.                 tv.setText(data_.get(position).get(TITLES[i]));  
    246.                 tv.setTextSize(TEXTSIZE);  
    247.                 lly.addView(tv);  
    248.             }  
    249.             return lly;  
    250.         }  
    251.   
    252.     }  
    253.   
    254. }
    255.   总结:用此方法来实现存在较大弊端,在高版本手机中会明显出现跳格现象,有时并不准确,所以慎用之!!!!!!!!!