Android实现仿excel数据表格效果
程序员文章站
2024-03-04 14:40:11
在没给大家分享代码之前,先给大家展示下效果图:
1.activity
/**
* 采购需求
* created by administrator o...
在没给大家分享代码之前,先给大家展示下效果图:
1.activity
/** * 采购需求 * created by administrator on 2016/10/13. */ public class purchaserequireactivity extends baseactivity implements purchaserequireview { @bind(r.id.apptitle) textview apptitle; @bind(r.id.scrolllinearlayout) linearlayout titlelinearlayout; @bind(r.id.scroll_list) listview mlistview; @bind(r.id.item_scroll_title) chtablescrollview headerscroll; public horizontalscrollview mtouchview; private list<chtablescrollview> mhscrollviews = new arraylist<>();// 装入所有的 hscrollview private hashmap<string, textview> mcolumncontrols = new hashmap<>(); private pporderrequirepresenter presenter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); try { setcontentview(r.layout.activity_purchase_require); butterknife.bind(this); init();// 初始化 } catch (exception e) { e.printstacktrace(); } } /** * 初始化 */ private void init() { presenter = new pporderrequirepresenter(this, this); presenter.gettoday(); } @onclick({r.id.backrlot}) public void onclick(view view) { switch (view.getid()) { case r.id.backrlot: finish(); break; } } public void scrollto_0_0() {// 全部列表滑动到开始位置 for (final chtablescrollview hscrollview : mhscrollviews) { mlistview.post(new runnable() { @override public void run() { //当listview刷新完成之后,把该条移动到最终位置 hscrollview.scrollto(0, 0); } }); } } public void onscrollchanged(int l, int t) { for (chtablescrollview scrollview : mhscrollviews) { if (mtouchview != scrollview) scrollview.smoothscrollto(l, t);//防止重复滑动 } } public void onclicktext(string str, int position) {// 点击的事件 system.out.println(str + "-" + position); } @override public void refreshlv(list<pporderrequiremobmodel> newlist) { list.clear(); list.addall(newlist); } @override public void initviews(string[] cols, list<map<string, string>> datas) { // 全部列表滑动到开始位置 scrollto_0_0(); // 清空所有的view titlelinearlayout.removeallviews(); //table title for (int i = 0; i < cols.length; i++) { if (i != 0) { view linearlay = presenter.newview(purchaserequireactivity.this, r.layout.row_title_edit_view, cols[i]); textview et = (textview) linearlay.findviewbyid(r.id.teveditview); if (cols[i].contains("-")) {// settextcolor if (cols[i].substring(cols[i].length() - 1, cols[i].length()).equals("2")) {// 2专柜:红色,1买断:黑色 et.settextcolor(color.red); } else { et.settextcolor(color.dkgray); } } else { et.settextcolor(color.dkgray); } et.settext(cols[i].substring(0, cols[i].length() - 2));//设置每一列顶表格数据 titlelinearlayout.addview(linearlay); } } // 添加头滑动事件,先清空列表 mhscrollviews.clear(); mhscrollviews.add(headerscroll); mlistview.setdividerheight(0);//设置分割线高度 mcolumncontrols.clear(); for (int i = 0; i < cols.length; i++) {// 门店名称+现金 or 门店名称+外欠 if (i != 0) {//预留第一列 edittext etitem1 = new edittext(purchaserequireactivity.this); etitem1.setwidth(50);// 设置宽度 etitem1.settextcolor(color.dkgray); etitem1.setgravity(gravity.center); mcolumncontrols.put(cols[i], etitem1); } } baseadapter adapter = new purchaserequireadapter(this, datas, r.layout.row_item_edit, cols, mcolumncontrols, mhscrollviews, mlistview); mlistview.setadapter(adapter); } }
2.view
/** * 采购需求 * created by administrator on 2016/10/13. */ public interface purchaserequireview { void refreshlv(list<pporderrequiremobmodel> newlist); void initviews(string[] cols, list<map<string, string>> datas); }
3.pporderrequirepresenter
/** * 采购需求 * created by administrator on 2016/10/13. */ public class pporderrequirepresenter { private purchaserequireview view; private context context; public pporderrequirepresenter(context context, purchaserequireview view) { this.context = context; this.view = view; } /** * 获取数据 */ public void gettoday() { string billdate = getstartbilldate();//freshroadutil.gettodaydate_normal().substring(0, 10); int dimen = integer.parseint(getdimen());// 默认条件:日期=当天 、维度=按门店 string url = interfacenameutil.pporderrequiregetrequire(billdate, dimen); log.e("url", url); final sweetalertdialog dialog = apputil.showloading(context, "正在发送请求,请稍后..."); apilisttrequest<pporderrequiremobmodel> request = new apilisttrequest<>( url, null, pporderrequiremobmodel.class, new response.listener<list<pporderrequiremobmodel>>() { @override public void onresponse(list<pporderrequiremobmodel> list) { if (list != null) {// && list.size() > 0 view.refreshlv(list); dosomethingfrolist(list); view.refreshverticallv(getpnamefromdb(list));// 刷新一下导航条 } dialog.dismiss(); } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { apputil.toerror(dialog, error); } }); request.setaddtokentoheader(false); volleyutil.start(context, request); } /** * 生成用于显示excel的数据源 * * @param newlist newlist */ public void dosomethingfrolist(list<pporderrequiremobmodel> newlist) { arraylist<string> namelist = new arraylist<>();// 货物 arraylist<long> cargoidlist = new arraylist<>();// 货物id arraylist<string> colslist = new arraylist<>();// 门店名称-现金 or 门店名称-外欠 for (pporderrequiremobmodel bean : newlist) { string cargoname = bean.getcargoname(); if (bean.getcargoname().length() > 6) cargoname = cargoname.substring(0, 6); namelist.add(cargoname + "\n存:" + freshroadutil.doubletrans(bean.getqtyinventory()) + ",需:" + freshroadutil.doubletrans(bean.getqtyrequire())); cargoidlist.add(bean.getcargoid()); for (pporderrequireitemmobmodel itembean : bean.getitems()) { if (!ishave(colslist, itembean.getname(), itembean.getnaturetype())) { colslist.add(itembean.getname() + "-" + itembean.getnaturetype()); } } } // 排序,先1.买断、再2.专柜 collections.sort(colslist, new comparator<string>() { @override public int compare(string left, string right) { return (left.substring(left.length() - 1, left.length())).compareto(right.substring(right.length() - 1, right.length())); //return (right.substring(right.length() - 1, right.length())).compareto(left.substring(left.length() - 1, left.length())); } }); colslist.add(0, "品名规格/店名");// excel控件的位于首位置的“提示”单元格文本 //column string[] cols = colslist.toarray(new string[colslist.size()]); string[] name = namelist.toarray(new string[namelist.size()]); list<map<string, string>> datas = new arraylist<>(); map<string, string> data; for (int i = 0; i < name.length; i++) { data = new hashmap<>(); data.put(cols[0], name[i]);//设置每一行头表格数据 for (int j = 1; j < cols.length; j++) { data.put(cols[j], getshowtext(newlist, cargoidlist.get(i), colslist.get(j))); } datas.add(data); } view.initviews(cols, datas); } /** * 列表中是否已经存在相应文本(外面 是 cargoid 是主键,里面是:name+naturetype是唯一键) * * @param colslist colslist * @param name name * @param naturetype naturetype * @return 是否 */ public boolean ishave(arraylist<string> colslist, string name, int naturetype) { for (string itemstr : colslist) { if (itemstr.equals(name + "-" + naturetype)) { return true; } } return false; } /** * 获取要显示的文本 * * @param list list * @param cargoid cargoid * @param name name * @return 显示的文本 */ public string getshowtext(list<pporderrequiremobmodel> list, long cargoid, string name) { for (pporderrequiremobmodel bean : list) { for (pporderrequireitemmobmodel itembean : bean.getitems()) { if (bean.getcargoid() == cargoid && (itembean.getname() + "-" + itembean.getnaturetype()).equals(name)) { return freshroadutil.doubletrans(itembean.getqtyrequire()); } } } return " "; } /** * 新增一个view * * @param context context * @param res_id res_id * @param tag_name tag_name * @return view */ public view newview(context context, int res_id, string tag_name) { view itemview = layoutinflater.from(context).inflate(res_id, null); itemview.settag(tag_name); return itemview; } }
4.purchaserequireadapter
/** * 采购需求 * created by administrator on 2016/10/13. */ public class purchaserequireadapter extends baseadapter { private list<? extends map<string, ?>> datas; private int res; private string[] from; private context context; protected list<chtablescrollview> mhscrollviews = new arraylist<>(); private hashmap<string, textview> mcolumncontrols; private listview mlistview; public purchaserequireadapter(context context, list<? extends map<string, ?>> data, int resource, string[] from, hashmap<string, textview> mcolumncontrols, list<chtablescrollview> mhscrollviews, listview mlistview) { this.context = context; this.datas = data; this.res = resource; this.from = from; this.mcolumncontrols = mcolumncontrols; this.mhscrollviews = mhscrollviews; this.mlistview = mlistview; } @override public int getcount() { return datas.size(); } @override public object getitem(int position) { return datas.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { view v = convertview; if (v == null) { v = layoutinflater.from(context).inflate(res, null); //第一次初始化的时候装进来 mcolumncontrols.put("title", (textview) v.findviewbyid(r.id.item_title)); view chsv = v.findviewbyid(r.id.item_scroll); linearlayout ll = (linearlayout) chsv.findviewbyid(r.id.item_scroll_layout); view[] views = new view[from.length]; for (int i = 0; i < from.length; i++) { if (i == 0) { views[0] = v.findviewbyid(r.id.item_title); continue; } view linearlay = newview(context, r.layout.row_item_edit_view, from[i]); textview td = (textview) linearlay.findviewbyid(r.id.ieveditview); td.settag(position); td.setonclicklistener(clicklistener);// 文本的点击事件 ll.addview(linearlay); views[i] = td; } v.settag(views); addhviews((chtablescrollview) chsv); } view[] holders = (view[]) v.gettag(); int len = holders.length; for (int i = 0; i < len; i++) { if (i == 0) { string oldstr = this.datas.get(position).get(from[i]).tostring();// 西瓜\n存:12,需:32 string cargostr = oldstr.substring(0, oldstr.indexof("\n"));// 西瓜 string numberstr_1 = oldstr.substring(oldstr.indexof(":") + 1, oldstr.indexof(","));// 12 string numberstr_2 = oldstr.substring(oldstr.lastindexof(":") + 1, oldstr.length());// 32 string stext = "<font color=\"#000000\">" + cargostr + "</font>" + "<br>" + "<font color=\"#999999\">存:</font> " + "<font color=\"#000000\">" + numberstr_1 + "</font> " + "<font color=\"#999999\">,需:</font> " + "<font color=\"#000000\">" + numberstr_2 + "</font> "; ((textview) holders[i]).settext(html.fromhtml(stext)); } else { ((textview) holders[i]).settext(this.datas.get(position).get(from[i]).tostring()); //((textview) holders[i]).settextcolor(color.parsecolor("#ffa500")); } } return v; } private view newview(context context, int res_id, string tag_name) { view itemview = layoutinflater.from(context).inflate(res_id, null); itemview.settag(tag_name); return itemview; } public void addhviews(final chtablescrollview hscrollview) { if (!mhscrollviews.isempty()) { int size = mhscrollviews.size(); chtablescrollview scrollview = mhscrollviews.get(size - 1); final int scrollx = scrollview.getscrollx(); //第一次满屏后,向下滑动,有一条数据在开始时未加入 if (scrollx != 0) { mlistview.post(new runnable() { @override public void run() { //当listview刷新完成之后,把该条移动到最终位置 hscrollview.scrollto(scrollx, 0); } }); } } mhscrollviews.add(hscrollview); } // 点击的事件 private view.onclicklistener clicklistener = new view.onclicklistener() { @override public void onclick(view v) { ((purchaserequireactivity) context).onclicktext(((textview) v).gettext().tostring(), (int) v.gettag()); } }; }
5.chtablescrollview
/** * excel 布局 * created by administrator on 2016/10/13. */ public class chtablescrollview extends horizontalscrollview { purchaserequireactivity activity; public chtablescrollview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); activity = (purchaserequireactivity) context; } public chtablescrollview(context context, attributeset attrs) { super(context, attrs); activity = (purchaserequireactivity) context; } public chtablescrollview(context context) { super(context); activity = (purchaserequireactivity) context; } @override public boolean ontouchevent(motionevent ev) { //进行触摸赋值 activity.mtouchview = this; return super.ontouchevent(ev); } @override protected void onscrollchanged(int l, int t, int oldl, int oldt) { //当当前的chscrollview被触摸时,滑动其它 if (activity.mtouchview == this) { activity.onscrollchanged(l, t); } else { super.onscrollchanged(l, t, oldl, oldt); } } }
6.activity_purchase_require
<?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" android:background="#ffffff" android:orientation="vertical"> <include layout="@layout/default_app_head3" /> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/bg_light_gray" android:baselinealigned="false" android:orientation="horizontal"> <relativelayout android:id="@+id/shop_mission_ll" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2"> <relativelayout android:id="@+id/shop_mission_delete_ll" android:layout_width="50dp" android:layout_height="35dp" android:layout_alignparentright="true"> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerinparent="true" android:layout_marginright="15dp" android:contentdescription="@string/app_name" android:src="@drawable/x_clear_icon" /> </relativelayout> <textview android:id="@+id/shop_mission_search_tv" android:layout_width="match_parent" android:layout_height="35dp" android:layout_toleftof="@id/shop_mission_delete_ll" android:gravity="center" android:text="点击这里,设置查询条件" android:textcolor="@color/black_text" android:textcolorhint="@color/light_gray_text" android:textsize="@dimen/font_small" /> </relativelayout> <relativelayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"> <textview android:id="@+id/purchase_select_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centervertical="true" android:singleline="true" android:text="" android:textsize="12sp" /> <relativelayout android:id="@+id/purchase_tv_delete_ll" android:layout_width="45dp" android:layout_height="35dp" android:layout_alignparentright="true"> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerinparent="true" android:layout_marginright="15dp" android:contentdescription="@string/app_name" android:src="@drawable/x_clear_icon" /> </relativelayout> </relativelayout> </linearlayout> <relativelayout android:layout_width="match_parent" android:layout_height="match_parent"> <listview android:id="@+id/order_vertical_lv" android:layout_width="35dp" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_centervertical="true" android:divider="@null" android:scrollbars="none" /> <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_toleftof="@id/order_vertical_lv" android:orientation="vertical"> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="fill_vertical" android:minheight="40dip" android:orientation="horizontal"> <textview android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="3" android:background="@drawable/bg_edittext_normal_s" android:enabled="false" android:gravity="center" android:maxlines="1" android:maxwidth="@dimen/scroll_tab_column_width" android:minwidth="@dimen/scroll_tab_column_width" android:singleline="true" android:text="品名规格/店名" android:textcolor="@android:color/black" android:textsize="12sp" /> <com.co_insight.freshroad.business.widget.chtablescrollview android:id="@+id/item_scroll_title" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:scrollbars="none"> <linearlayout android:id="@+id/scrolllinearlayout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" /> </com.co_insight.freshroad.business.widget.chtablescrollview> </linearlayout> <listview android:id="@+id/scroll_list" android:layout_width="match_parent" android:layout_height="match_parent" android:cachecolorhint="@android:color/transparent" android:scrollbars="none" /> </linearlayout> <com.co_insight.freshroad.views.widgets.horizontallistview android:id="@+id/shop_mission_item_hlv" android:layout_width="60dp" android:layout_height="40dp" android:layout_centerhorizontal="true" android:layout_margintop="40dp" android:background="@drawable/fresh_road_bg_button_normal" android:visibility="gone" /> </relativelayout> </linearlayout>
7.row_title_edit_view
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="@dimen/scroll_tab_column_width" android:layout_height="match_parent" android:minheight="50dip" android:orientation="vertical"> <textview android:id="@+id/teveditview" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/bg_edittext_normal_title" android:editable="false" android:enabled="false" android:gravity="center" android:maxlines="2" android:maxwidth="@dimen/scroll_tab_column_width" android:minwidth="@dimen/scroll_tab_column_width" android:paddingleft="3dp" android:paddingright="3dp" android:textcolor="@android:color/black" android:textsize="12sp" /> </linearlayout> 8.row_item_edit_view [html] view plain copy 在code上查看代码片派生到我的代码片 <?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="@dimen/scroll_tab_column_width" android:layout_height="match_parent" android:minheight="50dip" android:orientation="vertical"> <textview android:id="@+id/ieveditview" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/bg_edittext" android:gravity="center" android:maxlines="2" android:maxwidth="@dimen/scroll_tab_column_width" android:minwidth="@dimen/scroll_tab_column_width" android:textsize="12sp" android:textcolor="@android:color/black" /> </linearlayout>
9.row_item_edit
<?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="20dp" android:maxheight="20dp" android:minheight="20dp" android:orientation="horizontal"> <textview android:id="@+id/item_title" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="3" android:background="@drawable/bg_edittext_normal_s" android:enabled="false" android:gravity="center" android:maxlines="2" android:maxwidth="@dimen/scroll_tab_column_width" android:minwidth="@dimen/scroll_tab_column_width" android:text="测试" android:textcolor="@android:color/black" android:textsize="12sp" /> <com.co_insight.freshroad.business.widget.chtablescrollview android:id="@+id/item_scroll" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:scrollbars="none"> <linearlayout android:id="@+id/item_scroll_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" /> </com.co_insight.freshroad.business.widget.chtablescrollview> </linearlayout>
10.bg_edittext
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/bg_edittext_normal" android:state_window_focused="false"/> <item android:drawable="@drawable/bg_edittext_focused" android:state_focused="true"/> </selector> 11.bg_edittext_focused [html] view plain copy 在code上查看代码片派生到我的代码片 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#ffffff" /> <corners android:radius="0dip" /> <stroke android:width="0.1dp" android:color="@color/nav_selected" /> </shape>
12.bg_edittext_normal
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#ffffff" /> <corners android:radius="0dip" /> <stroke android:width="0.1dp" android:color="@color/nav_selected" /> </shape>
13.bg_edittext_normal_title
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <!--北京填充色--> <solid android:color="#00ffffff" /> <!--角的度数--> <corners android:radius="0dip" /> <!-- 边框 --> <stroke android:width="0.1dp" android:color="@color/nav_selected" />
14.pporderrequiremobmodel
package com.co_insight.freshroad.business.bean; import java.util.arraylist; import java.util.list; /** * 采购需求 * created by administrator on 2016/10/13. */ public class pporderrequiremobmodel { private long cargoid;// 货物id private string cargoname;// 货物名称 private long unitid;// 单位id private string unitname;// 单位 private long pnameid;// 小类 private long categoryid;// 品名属性id private string categoryname;// 品名属性 private double qtyrequire;// 需求量 private double qtyinventory;// 库存量 private long id; private string createdate; private string updatedate; private list<pporderrequireitemmobmodel> items = new arraylist<>();// 需求明细 public long getcargoid() { return cargoid; } public void setcargoid(long cargoid) { this.cargoid = cargoid; } public string getcargoname() { return cargoname; } public void setcargoname(string cargoname) { this.cargoname = cargoname; } public long getunitid() { return unitid; } public void setunitid(long unitid) { this.unitid = unitid; } public string getunitname() { return unitname; } public void setunitname(string unitname) { this.unitname = unitname; } public long getpnameid() { return pnameid; } public void setpnameid(long pnameid) { this.pnameid = pnameid; } public long getcategoryid() { return categoryid; } public void setcategoryid(long categoryid) { this.categoryid = categoryid; } public string getcategoryname() { return categoryname; } public void setcategoryname(string categoryname) { this.categoryname = categoryname; } public double getqtyrequire() { return qtyrequire; } public void setqtyrequire(double qtyrequire) { this.qtyrequire = qtyrequire; } public double getqtyinventory() { return qtyinventory; } public void setqtyinventory(double qtyinventory) { this.qtyinventory = qtyinventory; } public long getid() { return id; } public void setid(long id) { this.id = id; } public string getcreatedate() { return createdate; } public void setcreatedate(string createdate) { this.createdate = createdate; } public string getupdatedate() { return updatedate; } public void setupdatedate(string updatedate) { this.updatedate = updatedate; } public list<pporderrequireitemmobmodel> getitems() { return items; } public void setitems(list<pporderrequireitemmobmodel> items) { items = items; } }
好了,代码到此结束。如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
上一篇: Tomcat+JDK安装和配置教程
推荐阅读
-
Android 自定View实现仿QQ运动步数圆弧及动画效果
-
Android中使用TextView实现高仿京东淘宝各种倒计时效果
-
Android仿今日头条APP实现下拉导航选择菜单效果
-
Android 自定View实现仿QQ运动步数圆弧及动画效果
-
Android中使用TextView实现高仿京东淘宝各种倒计时效果
-
Android仿今日头条APP实现下拉导航选择菜单效果
-
Android App中ListView仿QQ实现滑动删除效果的要点解析
-
Android App中ListView仿QQ实现滑动删除效果的要点解析
-
Android波纹扩散效果之仿支付宝咻一咻功能实现波纹扩散特效
-
Android 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果(推荐)