參考教程筆記_第1頁
參考教程筆記_第2頁
參考教程筆記_第3頁
參考教程筆記_第4頁
參考教程筆記_第5頁
已閱讀5頁,還剩28頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、一、 商鋪界面1 界面分析1.1 整體布局1.2 BottomSheetLayout 的布局1.3 購物車布局1.4 引入 BottomSheet 庫/底部彈出窗體compile com.flipboamons:1.5.1compile com.flipboard:bottomsheet-core:1.5.12 初始化標題信息/獲取商家的 id Bundundle = getent().getExtras(); seller = (Seller) bundle.getSerializable(seller);/設置標題 tvTitle.setText(seller.getName(); /設置

2、配送費 tvSendPrietText(另需配送費+seller.gedPrice()+¥); /設置起送金額 tvDeliveryFee.setText(¥+seller.gedPrice()+元起送);3 TabLayout 添加 Tabprivate String tabTitles = new String商品, 評價, 商家;/* * 創(chuàng)建 Tab */private void createTabs() for ( i = 0; i tabTitles.length; i+) tabs.addTab(tabs.newTab().setText(tabTitlesi);4 Vieger

3、 增加頁面注意:整個項目整體采用的是V4 支持包的 Fragment 一套 APIFragmentActivityFragmentFragmentPagerAdapter/準備 Viger 的數(shù)據(jù)List listFragment = createFragmentList();BusinesivityVpAdapter vpAdapter = new BusinesivityVpAdapter(getSupportFragmentManager(), listFragment);vp.setAdapter(vpAdapter);public List listFragment = new Ar

4、rayList();/* * 創(chuàng)建 Fragment */privaist createFragmentList() for ( i = 0; i tabTitles.length; i+) Fragment fragment = null;switch (i) case 0:fragment = new GoodsFragment();break;case 1:fragment = new CommentFragment();break;case 2:fragment = new BusinessFragment();break;Bundle args = new Bundle();args

5、.putString(title, tabTitlesi);args.putSerializable(seller,seller);fragment.setArguments(args);listFragment.add(fragment);return listFragment;5 TableLayout 和 Vieger 進行關聯(lián)tabs.setupWithVieger(vp);注意FragmentPagerAdapter 一定要重寫Overridereturn listFragment.get(ition).getArguments().getString(title);6 頁面滑動處理

6、vp.addOnPageChangeListener(new MyOnPageChangeListener();private class MyOnPageChangeListener implements Vieger.OnPageChangeListener public CharSequence getPageTitle(ition) Override itionOffsetPixels) Overrideif (ition = 0) bottom.setVisibility(View.VISIBLE); else bottom.setVisibility(View.GONE);Over

7、ridepublic void onPageScrollSeChanged( se) 二、 聯(lián)網(wǎng)獲取商鋪的商品信息1 創(chuàng)建 GoodsFragment 的布局/帶浮動分欄的 ListViewcompile se.emilsjolander:stickylistheaders:2.7.0public void onPageSelected(ition) public void onPageScrolled(ition, float itionOffset, InjectView(R.id.rv_goods_type) RecyclerView rvGoodsType; InjectView(R.

8、id.slhlv) StickyListHeadersListView slhlv;private RecyclerView.LayoutManager typeLm; Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstane) View view = inflater.inflate(R.layout.fragment_goods, container, false);ButterKnife.inject(this, view);/設置布局管理器type

9、Lm = new LinearLayoutManager(getActivity();rvGoodsType.setLayoutManager(typeLm);/添加RecyclerView 的分割線rvGoodsType.addItemDecoration(new RecycleViewDivider(this.getContext(),LinearLayoutManager.HORIZONTAL);return view;2 使用 Dagger 分離 GoodsFragment 的業(yè)務2.1 創(chuàng)建 GoodsFragmengPresenterpublic class GoodsFragme

10、ntPresenter extends BasePresenterprivate GoodsFragment goodsFragment;Injectpublic GoodsFragmentPresenter(GoodsFragment goodsFragment) this.goodsFragment = goodsFragment;2.2 在 GoodsFragment 里面注入public class GoodsFragment extends BaseFragment Injectpublic GoodsFragmentPresenter goodsFragmentPresenter;

11、2.3 創(chuàng)建 GoodsFragmentModuleModulepublic class GoodsFragmentModule GoodsFragment goodsFragment;public GoodsFragmentModule(GoodsFragment goodsFragment) this.goodsFragment = goodsFragment;ProvidesGoodsFragment providerGoodsFragment()return goodsFragment;2.4 創(chuàng)建ponentComponent(modules = GoodsFragmentModul

12、e.class)publicerfaceponet void in(GoodsFragment goodsFragment);2.5 Build- ReBuild Project2.6 在 GoodsFragment 里面編寫代碼protected voidponet() Daggponet.builder().goodsFragmentModule(new GoodsFragmentModule(this).build().his);3 根據(jù) SellerId 獲取商品的商品信息Overridepublic void getData() Call responseInfoCall = res

13、ponseInfoApi.getBussinessInfo(business,goodsFragment.seller.getId();responseInfoCall.enqueue(callback);Overrideprotected void parseDestInfo(String json) BusinessInfo businessInfo = gson.fromJson(json,BusinessInfo.class);dealData(businessInfo.getList();4 根據(jù)返回的數(shù)據(jù)類型設計 bean 模型4.1 BusinessInfopublic clas

14、s BusinessInfo privaist list; 4.2 GoodsTypeInfopublic class GoodsTypeInfo String name;/商品類型名稱String info;/特價信息List list;/商品列表private typeCount; 4.3 GoodsInfopublic class GoodsInfo String name;/商品名稱String icon;/商品String form;/組成float newPrice;/新價oldPrice;/ isNew;/是否是新產(chǎn)品 bargainPrice;/特價 monthSaleNum;

15、/月銷售量 id;/商品 id id;/商品類型 idprivate number;private typeId;private String typeName;private sellerId; 5數(shù)據(jù)封裝數(shù)據(jù)成集合public List businessInfos;public List goodsInfos;public void dealData(List businessInfos)this.businessInfos = businessInfos;goodsInfos = new ArrayList();for (GoodsTypeInfo info:businessInfos)

16、 List spList = info.getList();for (GoodsInfo goods:spList)goods.setSellerId(goodsFragment.seller.getId();goods.setTypeId(info.getId();goods.setTypeName(info.getName();goodsInfos.addAll(spList);/的數(shù)量6 處理商品類型列表6.1 GoodsFragmentTypeAdapterpublic GoodsFragmentTypeAdapter typeAdapter;typeAdapter = new Goo

17、dsFragmentTypeAdapter(this, getActivity(), null);rvGoodsType.setAdapter(typeAdapter);使用 Dagger后 Fragment 只會負責界面的修改部分,所以 FragmentPresenter 直接給Adapter 設置數(shù)據(jù)。goodsFragment.typeAdapter.setBusinessInfos(businessInfos);goodsFragment.goodsAdapter.setInfos(goodsInfos);6.2 選擇的條目變灰ViewHolder 里面的操作public select

18、edition = 0;public void setition(final ition)/設置條目點擊事件 itemView.setOnClickListener(new View.OnClickListener() Overridepublic void onClick(View v) GoodsFragmentTypeAdapter.this.notifyDataSetChanged(););onBindViewHolder(RecyclerView.ViewHolder holder, finalition)if(selectedition =ition)viewHolder.item

19、View.setBackgroundColor(Color.WHITE);viewHolder.type.setTypeface(Typeface.DEFAULT_BOLD);/粗體elseviewHolder.itemView.setBackgroundColor(Color.parseColor(#b9dedcdc);viewHolder.type.setTypeface(Typeface.DEFAULT);/正常字體7 商品列表7.1 界面selectedition =ition;7.2 頭布局7.3 條目布局holder.tvOldprice.getPa().setFlags(Pa.

20、STRIKE_THRU_TEXT_FLAG );7.4 點擊類型列表條目商品條碼移動至頂部fragment.onClickType(typeId);/告知 Fragment/* * 商品類型條目點擊 * param typeId */public void onClickType( typeId) slhlv.setSelection(ition);/* * 通過商品類型id 獲取第一條商品類型的 id * param typeId * return */private getGoodsition( typeId) for (i = 0; i goodsFragmentPresenter.go

21、odsInfos.size(); i+) ition = getGoodsition(typeId);if (goodsFragmentPresenter.goodsInfos.get(i).getTypeId() = typeId) return i;return 0;7.5 滑動商品條目滑動商品條目到新的商品類型,讓商品類型條目自動選中,并且自動滑動思路:1 設置滑動2 滑動的時候獲取第一個條目的類型 id3 獲取類型條目當前選中的類型 id4 兩個類型 id 進行對比5 當兩個類型 id 不相同的時候,去修改類型列表的 selectedition6 刷新界面/* * 成功獲取數(shù)據(jù)后設置

22、*/public void setListener() /設置滑動slhlv.setOnScrollListener(new MyOnScrollListener();private class MyOnScrollListener implements AbsListView.OnScrollListener Overridepublic void onScrollSeChanged(AbsListView view, scrollSe) Overridepublic void onScroll(AbsListView view, VisibleItem, visibleItemCount,

23、 totalItemCount) if (typeId != selectTypeId) selectedition = getTypeListition(typeId); selectTypeId = getTypeListSelectedTypeId(); typeId = goodsFragmentPresenter.goodsInfos.get(VisibleItem).getTypeId();typeAdapter.notifyDataSetChanged();/滑動到要選擇的條目rvGoodsType.smoothScrollToition(selectedition);/通過類型

24、id 獲取應該選擇類型的下標private getTypeListition( typeId) for ( i = 0; i ReBuild1.6 在 Businesivity 編寫/設置組件private voidponet() DaggerBponet.builder().businesivityModule(newBusinesivityModule(this).build().his);2 更新購物車選購商品數(shù)量商品列表條目的+號和-號會影響購物車的數(shù)量。(Businesivity)fragment.getActivity().businesivityPresenter.updateC

25、artUI();/* * 更新購物車界面 */public void updateCartUI()GoodsFragment goodsFragment = (GoodsFragment) businesivity.listFragment.get(0);List goodsInfos = goodsFragment.goodsFragmentPresenter.goodsInfos;double countPrice = 0;for (GoodsInfo info:goodsInfos)if(info.getNumber() != 0)countPrice+=info.getNumber()

26、*info.getNewPrice();countGoods+=info.getNumber();businesivity.updateCartUI(countGoods,countPrice);/* * 更新購物車界面 */public void updateCartUI( countGoods,double countPrice)if(countGoods = 0)tvSelectNum.setVisibility(View.GONE);tvSubmit.setVisibility(View.GONE); countGoods = 0;tvCountPrietText(¥0);elsetv

27、SelectNum.setVisibility(View.VISIBLE);tvSelectNum.setText(countGoods+);if(countPrice = startDispatchPrice)tvSubmit.setVisibility(View.VISIBLE);tvSendPrietVisibility(View.GONE);tvCountPrietText(NumberFormatUtils.formatDigits(countPrice);public claumberFormatUtils private NumberFormatUtils()/格式化數(shù)據(jù)保留兩位

28、小數(shù)并且?guī)Вublic sic String formatDigits(double data)NumberFormat nf = NumberFormat.getCurrencyInstance();nf.setumFractionDigits(2);return nf.format(data);tvSendPrietVisibility(View.VISIBLE);3 購物車列表界面加載public View bottomSheet;public void showBottomSheet() if (bottomSheet = null) bottomSheet = createBott

29、omSheet();if(bottomSheetLayout.isSheetShowing()bottomSheetLayout.dismissSheet();elseif(businesivityPresenter.getCartGoodsInfo().size() != 0)bottomSheetLayout.showWithSheetView(bottomSheet);private View createBottomSheet() View view = LayoutInflater.from(this).inflate(R.layoart, (ViewGroup) getWindow

30、().getDecorView(), false);return view;4 購物車數(shù)據(jù)的獲取/* * 獲取購物車里面的數(shù)據(jù) * return */public List getCartGoodsInfo()List infos = new ArrayList();List goodsInfos = (GoodsFragment) businesivity.listFragment.get(0).goodsFragmentPresenter.goodsInfos;if(goodsInfos != null)for ( i =0; igoodsInfos.size();i+)GoodsInfo

31、 info = goodsInfos.get(i);if(info != null & info.getNumber() != 0)infos.add(info);return infos;5 購物車列表顯示RecyclerView rvCart = (RecyclerView) view.findViewById(R.id.rvCart); rvCart.setLayoutManager(new LinearLayoutManager(this);rvCart.addItemDecoration(new RecycleViewDivider(this,LinearLayoutManager.

32、HORIZONTAL);cartAdapter = new CartAdapter(this,businesivityPresenter.getCartGoodsInfo();rvCart.setAdapter(cartAdapter);public class CartAdapter extends RecyclerView.Adapter private Context context;privaist infos;6 清空購物車TextView tvClear = (TextView) view.findViewById(R.id.tvClear);tvClear.setOnClickL

33、istener(new View.OnClickListener() Overridepublic void onClick(View v) new AlertDialog.Builder(Businesivity.this).setMessage(清空購物車).setNegativeButton(取消,null)Overridepublic void onClick(Dialogerface dialog, which) businesivityPresenter.clearCart();).show(););/* * 清空購物車 */public void clearCart()Goods

34、Fragment goodsFragment = (GoodsFragment) businesivity.listFragment.get(0);List goodsInfos = goodsFragment.goodsFragmentPresenter.goodsInfos;for( i =0;igoodsInfos.size();i+)GoodsInfo info = goodsInfos.get(i);if(info.getNumber() != 0)info.setNumber(0);businesivity.hideCartList();goodsFragment.typeAdap

35、ter.clearTypeNumber();goodsFragment.goodsAdapter.clearSelectedGoodsNumber();updateCartUI();7 購物車列表的+號和 -號操作class ViewHolder extends RecyclerView.ViewHolder ition;.setitiveButton(確定, new Dialogerface.OnClickListener() this.ition = ition;ViewHolder(View view) super(view);ButterKnife.inject(this, view)

36、;OnClick(R.id.ib_minus, R.id.ib_add)public void onClick(View view) switch (view.getId() case R.id.ib_minus:doMinus(ition);operator = Constant.MINUSOPERATOR;break;case R.id.ib_add:doAdd(ition);operator = Constant.ADDOPERATOR;break;GoodsFragment goodsFragment = (GoodsFragment) (Businesivity) context).

37、listFragment.get(0);goodsFragment.selectedTypeDhange(typeId,operator);goodsFragment.goodsAdapter.notifyDataSetChanged();private void doMinus(ition)GoodsInfo goodsInfo = infos.get(ition);goodsInfo.setNumber(goodsInfo.getNumber()-1);Businesivity activity = (Businesivity)context;MyApplication app = (My

38、Application) activity.getApplication(); typeId = infos.get(ition).getTypeId(); operator = 0;public void setition(ition) if(goodsInfo.getNumber() = 0)if(getItemCount() = 1)/隱藏購物車列表activity.hideCartList();private void doAdd(ition)GoodsInfo goodsInfo = infos.get(ition);goodsInfo.setNumber(goodsInfo.get

39、Number()+1);Businesivity activity = (Businesivity)context;MyApplication app = (MyApplication) activity.getApplication();四、 選購商品內存緩存1 選擇緩存的位置public class MyApplication extends Application/解決迭代刪除異常public CopyOnWriteArrayList cacheSelectedInfos;Overridepublic void onCreate() super.onCreate();cacheSelec

40、tedInfos = new CopyOnWriteArrayList();/* 獲取在該商家選購的商品數(shù)量* param sellerId* return*/public getCacheSelectedCountBySellerId( sellerId)count = 0;CacheSelectedInfo cacheSelectedInfo = cacheSelectedInfos.get(i);if(cacheSelectedInfo.getSellerId() = sellerId)count += cacheSelectedInfo.getCount();return count;

41、public getCacheSelectedGoodsTypeCountBySellerIdAndTypeId( sellerId, typeId)for ( i = 0; i cacheSelectedInfos.size(); i+) CacheSelectedInfo cacheSelectedInfo = cacheSelectedInfos.get(i);if(cacheSelectedInfo.getSellerId() = sellerId & cacheSelectedInfo.getGoodsTypeId() = typeId)count += cacheSelectedInfo.getCount();return count;public getCacheSelectedGoodsCountByGoodsId( sellerId, typeId, goodsId)for ( i = 0; i cacheSelectedInfos.size(); i+) Ca

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論