<strong><span style="font-size:24px;">数据准备:</span></strong>
[
[
"颜色",
"序号",
"商机阶段",
"商机数量",
"商机转化率"
],
[
"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); } }
布局文件:
<?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"> <!-- 统计信息表格展示控件 --> <HorizontalScrollView android:id="@+id/layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:fillViewport="true" android:visibility="invisible"> <com.xzjmyk.pm.activity.ui.erp.view.ListViewInScroller android:id="@+id/lv_grid_dispaly" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:cacheColorHint="#00000000" android:divider="@drawable/scanframeline" /> </HorizontalScrollView> </LinearLayout>
适配器类:
适配器的布局文件没有元素:
R.layout.list_item_empty
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ly_top_view" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > </LinearLayout>
public class HListViewAdapter extends BaseAdapter { private Context ct; private LayoutInflater inflater; private ArrayList<ArrayList<String>> lists; public HListViewAdapter(Context context, ArrayList<ArrayList<String>> 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<String> 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<ArrayList<String>> gridlists = new ArrayList<ArrayList<String>>(); 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);
展示效果: