博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 表格HorizontalScrollView+ListView
阅读量:6303 次
发布时间:2019-06-22

本文共 6142 字,大约阅读时间需要 20 分钟。

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qingfeng812/article/details/51880722

数据准备:
[    [        "颜色",        "序号",        "商机阶段",        "商机数量",        "商机转化率"    ],    [        "1",        "胜芳",        "152,002",        "1223",        "22"    ],    [        "2",        "B2B平台",        "8,775",        "22",        "323"    ],    [        "3",        "峰闵",        "2,000",        "232",        "43242"    ],    [        "4",        "扬宇",        "11",        "12312",        "232"    ]]
自定义listview(保证在scroller中能展示,解决冲突)

public class ListViewInScroller extends ListView {   public ListViewInScroller(Context context) {      super(context);   }      public ListViewInScroller(Context context, AttributeSet attrs,         int defStyleAttr) {      super(context, attrs, defStyleAttr);   }   public ListViewInScroller(Context context, AttributeSet attrs) {      super(context, attrs);   }      /**注释:解决只显示一行的问题 */   @Override   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {       int expandSpec = MeasureSpec.makeMeasureSpec(                      Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);         super.onMeasure(widthMeasureSpec, expandSpec);   }}

布局文件:

适配器类:

适配器的布局文件没有元素:

R.layout.list_item_empty

public class HListViewAdapter extends BaseAdapter {    private Context ct;    private LayoutInflater inflater;    private ArrayList
> lists; public HListViewAdapter(Context context, ArrayList
> lists) { super(); this.lists = lists; this.ct = context; inflater = LayoutInflater.from(context); } @Override public int getCount() { return lists != null ? lists.size() : 0; } @Override public Object getItem(int arg0) { return arg0; } @Override public long getItemId(int arg0) { return arg0; } @SuppressWarnings("deprecation") @Override public View getView(int index, View view, ViewGroup arg2) { ArrayList
list = lists.get(index); TextView[] views = new TextView[list.size()]; if (view == null) { view = inflater.inflate(R.layout.list_item_empty, null); LinearLayout topview = (LinearLayout) view.findViewById(R.id.ly_top_view); //根部布局 LinearLayout ly_grid = new LinearLayout(ct); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); ly_grid.setLayoutParams(lp); ly_grid.setOrientation(LinearLayout.HORIZONTAL); //水平线条 View horizontal = new View(ct); ViewGroup.LayoutParams hp = new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, CommonUtil.dip2px(ct, 1)); horizontal.setLayoutParams(hp); horizontal.setBackground(ct.getResources().getDrawable(R.drawable.filemanagermenubg_w)); /**@注释:创建textview */ for (int i = 0; i < list.size(); i++) { //垂直线条 View vertical = new View(ct); ViewGroup.LayoutParams vp = new ViewGroup.LayoutParams(CommonUtil.dip2px(ct, 1), ViewGroup.LayoutParams.MATCH_PARENT); vertical.setLayoutParams(vp); vertical.setBackground(ct.getResources().getDrawable(R.drawable.domo_toolbar_bg)); TextView tView = new TextView(ct); LinearLayout.LayoutParams tp = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); if (i == 0) { tView.setWidth(CommonUtil.dip2px(ct, 55)); } else { tView.setWidth(CommonUtil.dip2px(ct, 70)); } tView.setMaxLines(8); //tView.setLines(1); //tView.setBackgroundColor(ct.getResources().getColor(R.color.red)); tView.setLayoutParams(tp); tView.setGravity(Gravity.CENTER); tView.setPadding(CommonUtil.dip2px(ct, 10), CommonUtil.dip2px(ct, 10), CommonUtil.dip2px(ct, 10), CommonUtil.dip2px(ct, 10)); tView.setTextColor(ct.getResources().getColor(R.color.black)); tView.setTextSize(CommonUtil.dip2px(ct, 8)); views[i] = tView; //ly_grid.setGravity(Gravity.CENTER); ly_grid.addView(vertical); ly_grid.addView(tView); } topview.addView(ly_grid); topview.addView(horizontal); view.setTag(views); } else { views = (TextView[]) view.getTag(); } view.setBackgroundColor(Color.WHITE); for (int i = 0; i < views.length; i++) { views[i].setText(list.get(i)); views[i].setTextColor(ct.getResources().getColor(R.color.black)); } if (index == 0) { //view.setBackgroundResource(R.color.head_bg); } else { if (index % 2 != 0) { view.setBackgroundColor(Color.argb(250, 255, 255, 255)); } else { view.setBackgroundColor(Color.argb(250, 224, 243, 250)); } } return view; }}

Activity 核心代码:

@ViewInject(R.id.layout)private HorizontalScrollView layout;@ViewInject(R.id.lv_grid_dispaly)private ListViewInScroller lv_grid_dispaly;

private HListViewAdapter hl_adapter;private ArrayList
> gridlists = new ArrayList
>();private String gridData = "[[\"颜色\",\"序号\",\"商机阶段\",\"商机数量\",\"商机转化率\"],[\"1\",\"胜芳\",\"152,002\",\"1223\",\"22\"],[\"2\",\"B2B平台\",\"8,775\",\"22\",\"323\"],[\"3\",\"峰闵\",\"2,000\",\"232\",\"43242\"],[\"4\",\"扬宇\",\"11\",\"12312\",\"232\"]]";

gridlists = (ArrayList) JSON.parseArray(gridData, ArrayList.class);hl_adapter = new HListViewAdapter(this, gridlists);lv_grid_dispaly.setAdapter(hl_adapter);

展示效果:

你可能感兴趣的文章
Android--自定义加载框
查看>>
LINUX下 lamp安装及配置
查看>>
BZOJ3105 [cqoi2013]新Nim游戏
查看>>
困惑的前置操作与后置操作
查看>>
SDNU 1269.整数序列(水题)
查看>>
BZOJ 2118 Dijkstra
查看>>
Go语言基础之结构体
查看>>
SpringCloud:Eureka Client项目搭建(Gradle项目)
查看>>
jqueryValidate
查看>>
ATL使用IE控件,并且屏蔽右键
查看>>
Jenkins
查看>>
linux下使用screen和ping命令对网络质量进行监控
查看>>
数据库设计技巧
查看>>
css定位概述
查看>>
C# 动态修改配置文件 (二)
查看>>
BOM:文档对象模型 --树模型
查看>>
我的Android进阶之旅------>WindowManager.LayoutParams介绍
查看>>
segment
查看>>
获取鼠标的原始移动值
查看>>
Linux信号 编程
查看>>