android 带表头,左右两个联动的ListView
转载
- 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) {
- // TODO Auto-generated method stub
- 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);
- // 添加ListView头部视图
- LinearLayout leftHead = new LinearLayout(this);
- // leftHead.setLayoutParams(TEXTLP);
- TextView tv = new TextView(this);
- tv.setText("title");
- tv.setTextSize(TEXTSIZE);
- tv.setLayoutParams(TEXTLP);
- leftHead.addView(tv);
- leftLly.addView(leftHead);
- // leftLst.addHeaderView(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.setScrollBarStyle();
- //leftLst.setScrollBarStyle(0);
- 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) {
- // TODO Auto-generated constructor stub
- this.context_ = context;
- this.data_ = data;
- }
-
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return data_.length;
- }
-
- @Override
- public Object getItem(int position) {
- // TODO Auto-generated method stub
- return data_[position];
- }
-
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return position;
- }
-
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- // TODO Auto-generated method stub
- 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);
- // 右侧ListVie标题
- 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);
- // t.setLayoutParams(TEXTLP);
- 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) {
- // TODO Auto-generated method stub
- leftLst.dispatchTouchEvent(event);
- return false;
- }
- });
-
- leftLst.setOnScrollListener(new AbsListView.OnScrollListener() {
-
- @Override
- public void onScrollStateChanged(AbsListView view, int scrollState) {
- // TODO Auto-generated method stub
- }
-
- @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 + "");//rd.nextInt(10000)
- }
- 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) {
- // TODO Auto-generated constructor stub
- this.context_ = context;
- this.data_ = data;
- }
-
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return data_.size();
- }
-
- @Override
- public Object getItem(int position) {
- // TODO Auto-generated method stub
- return data_.get(position);
- }
-
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- 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;
- }
-
- }
-
- }
- 总结:用此方法来实现存在较大弊端,在高版本手机中会明显出现跳格现象,有时并不准确,所以慎用之!!!!!!!!!
本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。