在没给大家分享代码之前,先给大家展示下效果图:
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 ListmHScrollViews = new ArrayList<>();// 装入所有的 HScrollView private HashMap 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 newList) { list.clear(); list.addAll(newList); } @Override public void initViews(String[] cols, List
2.view
/** * 采购需求 * Created by Administrator on 2016/10/13. */ public interface PurchaseRequireView { void refreshLV(ListnewList); void initViews(String[] cols, List
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, "正在发送请求,请稍后..."); APIListTRequestrequest = new APIListTRequest<>( url, null, PPOrderRequireMobModel.class, new Response.Listener >() { @Override public void onResponse(List
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 newList) { ArrayList nameList = new ArrayList<>();// 货物 ArrayList CargoIDList = new ArrayList<>();// 货物ID ArrayList 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 () { @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
4.PurchaseRequireAdapter
/** * 采购需求 * Created by Administrator on 2016/10/13. */ public class PurchaseRequireAdapter extends BaseAdapter { private List extends Map> datas; private int res; private String[] from; private Context context; protected List mHScrollViews = new ArrayList<>(); private HashMap mColumnControls; private ListView mListView; public PurchaseRequireAdapter(Context context, List extends Map > data, int resource, String[] from, HashMap mColumnControls, List 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 = "" + cargoStr + "" + "
" + "存: " + "" + numberStr_1 + " " + ",需: " + "" + numberStr_2 + " "; ((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
7.row_title_edit_view
8.row_item_edit_view [html] view plain copy 在CODE上查看代码片派生到我的代码片
9.row_item_edit
10.bg_edittext
11.bg_edittext_focused [html] view plain copy 在CODE上查看代码片派生到我的代码片
12.bg_edittext_normal
13.bg_edittext_normal_title
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 ListItems = 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 getItems() { return Items; } public void setItems(List items) { Items = items; } }
好了,代码到此结束。如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!