版權(quán)聲明:轉(zhuǎn)載請(qǐng)標(biāo)明轉(zhuǎn)載地址!
圣誕節(jié)快到了,在這里先祝大家圣誕節(jié)快樂(lè)!
有很多時(shí)候我們會(huì)得到各式各樣的效果和功能,在了解了任務(wù)之后我不建議大家馬上去百度或者編寫(xiě)代碼。應(yīng)該先分析任務(wù)的邏輯,并思考每部分利用什么控件或者技術(shù)實(shí)現(xiàn),哪種實(shí)現(xiàn)效果能使我們的邏輯更清晰,即使代碼編寫(xiě)錯(cuò)誤當(dāng)我們修改的時(shí)候也不需要改動(dòng)太大或者重新進(jìn)行編寫(xiě)(遵循開(kāi)閉原則),避免浪費(fèi)我們的時(shí)間。其實(shí)就是我們面向接口編程的思想,先抽象后實(shí)現(xiàn)。
是不是感覺(jué)簡(jiǎn)單分析之后,思路清晰多了,實(shí)現(xiàn)起來(lái)也知道該從哪里入手了。
1. RecyclerView控件實(shí)現(xiàn)左側(cè)菜單和右側(cè)內(nèi)容聯(lián)動(dòng)效果
activity_main.xml
<LinearLayout android:layout_below="@+id/include" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/view" android:orientation="horizontal"> <android.support.v7.widget.RecyclerView android:id="@+id/m_list_menu" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="5" android:scrollbars="none"> </android.support.v7.widget.RecyclerView> <android.support.v7.widget.RecyclerView android:id="@+id/m_list_content" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="2" android:scrollbars="none"> </android.support.v7.widget.RecyclerView></LinearLayout>
主布局相對(duì)來(lái)說(shuō)簡(jiǎn)單多了,就不進(jìn)行詳細(xì)的講解了。
item_menu.xml
<LinearLayout android:id="@+id/black_lay" android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center" android:orientation="horizontal"> <View android:id="@+id/item_menu_view_red" android:layout_width="match_parent" android:layout_height="wrap_content" android:visibility="gone" android:layout_weight="30" android:background="@color/title_color_ff0000"/> <TextView android:id="@+id/item_menu_text" android:layout_width="match_parent" android:gravity="center_horizontal" android:layout_height="wrap_content" android:layout_weight="0.5" android:textSize="16sp" android:textColor="@android:color/black" android:text="好評(píng)榜"/> <View android:id="@+id/item_menu_view_v" android:layout_width="0.1dp" android:layout_height="50dp" android:background="@color/color_menu_lines"/></LinearLayout>
菜單布局左右兩側(cè)分別有一條豎著的黑線(xiàn)和紅線(xiàn),當(dāng)我們進(jìn)行點(diǎn)擊item操作的時(shí)候,需要進(jìn)行的隱藏和顯示效果來(lái)達(dá)到該item處于選中狀態(tài)。
item_menu_content.xml
item_menu_content就是一個(gè)簡(jiǎn)單的布局,由于代碼量太大就不進(jìn)行展示了,大家可以下載我的Demo進(jìn)行查看。
RecyclerViewMenuAdapter.java
左側(cè)菜單的數(shù)據(jù)填充器,主要的部分就是在onBindViewHolder方法里面,先讓所有的item顯示為默認(rèn)狀態(tài),在根據(jù)點(diǎn)擊item對(duì)應(yīng)的position來(lái)改變?cè)搃tem的狀態(tài)即可:
//綁定ViewHolder@Overridepublic void onBindViewHolder(final ViewHolder holder, final int position) { holder.mTextView.setText(DemoData.ListMenu_STYLE[position]); //設(shè)置所有的item顯示為默認(rèn)狀態(tài) holder.mLinearLayout.setBackgroundResource(R.color.color_menu_back); holder.viewRed.setVisibility(View.GONE); holder.viewV.setVisibility(View.VISIBLE); //根據(jù)點(diǎn)擊item對(duì)應(yīng)的position來(lái)改變?cè)搃tem的狀態(tài) if (holder.getPosition() == MainActivity.SELECTPOSITION) { holder.mLinearLayout.setBackgroundResource(R.color.white); holder.viewRed.setVisibility(View.VISIBLE); holder.viewV.setVisibility(View.GONE); } setOnListtener(holder);}
RecyclerViewContentAdapter.java
左側(cè)內(nèi)容填充器則為普通的內(nèi)容沒(méi)有什么特殊的地方
MainActivity.java
/** * 菜單列表 數(shù)據(jù)填充 */private void setMenuCommonadapter() { mRecyclerViewMenuCommonadapter = new RecyclerViewMenuAdapter(mContext, stringMenuList); mRecyclerMenu.setAdapter(mRecyclerViewMenuCommonadapter); mRecyclerViewMenuCommonadapter.setOnItemClickListener(new RecyclerViewMenuAdapter.OnItemClickListener() { @Override public void onItemClick(View v, int position) { SELECTPOSITION = position; Log.e("TAG", "SELECTPOSITION:" + SELECTPOSITION); mRecyclerViewMenuCommonadapter.notifyDataSetChanged(); mRecyclerViewContentCommonadapter.notifyDataSetChanged(); } @Override public void onItemLongClick(View v, int position) {} });}
當(dāng)點(diǎn)擊item的時(shí)候,改變SELECTPOSITION 的值,再刷新兩個(gè)recyclerview即可。
效果圖如下:
由于數(shù)據(jù)全部都是一樣的所以可能看起來(lái),左側(cè)內(nèi)容部分沒(méi)有什么改變,但其實(shí)數(shù)據(jù)已經(jīng)刷新了。
模擬器和錄制gif圖工具有些問(wèn)題,所以分割線(xiàn)顯示不全并且有色差,大家如果有什么好的工具可以給我推薦推薦,在這里謝謝大家了!
2.添加購(gòu)物車(chē)動(dòng)畫(huà)的實(shí)現(xiàn)
在做添加購(gòu)物車(chē)動(dòng)畫(huà)的時(shí)候,首先想到的就是translateanimation平移動(dòng)畫(huà),公司同事做過(guò)類(lèi)似的動(dòng)畫(huà)所以拿過(guò)來(lái)稍加修改并封裝成了工具類(lèi),使用起來(lái)杠杠滴。
/** 動(dòng)畫(huà) */GoodsAnimUtil.setAnim(MainActivity.this, holder.mImgJia, mCarLay);GoodsAnimUtil.setOnEndAnimListener(new onEndAnim());
首先調(diào)用工具類(lèi)里面的setAnim()方法,就可以設(shè)置和開(kāi)始動(dòng)畫(huà)了;
再監(jiān)聽(tīng)動(dòng)畫(huà),當(dāng)動(dòng)畫(huà)結(jié)束時(shí)進(jìn)行相關(guān)操作即可。
public static void setAnim(Activity activity , View imgphoto, View imgcar){ mActivity = activity; mImgcar = imgcar; // 一個(gè)整型數(shù)組,用來(lái)存儲(chǔ)按鈕的在屏幕的X、Y坐標(biāo) int[] start_location = new int[2]; // 這是獲取購(gòu)買(mǎi)按鈕的在屏幕的X、Y坐標(biāo)(這也是動(dòng)畫(huà)開(kāi)始的坐標(biāo)) imgphoto.getLocationInWindow(start_location); int[] start_location1 = new int[]{start_location[0], start_location[1]}; // buyImg是動(dòng)畫(huà)的圖片,我的是一個(gè)小球(R.drawable.sign) ImageView buyImg = new ImageView(mActivity); // 設(shè)置buyImg的圖片 buyImg.setImageResource(R.mipmap.aii); // 開(kāi)始執(zhí)行動(dòng)畫(huà) startAnim(buyImg, start_location1);}
/** *開(kāi)始動(dòng)畫(huà) */private static void startAnim(final View v, int[] start_location) { anim_mask_layout = null; anim_mask_layout = createAnimLayout(); anim_mask_layout.addView(v);//把動(dòng)畫(huà)小球添加到動(dòng)畫(huà)層 view = addViewToAnimLayout(anim_mask_layout, v,start_location); int[] end_location = new int[2];// 這是用來(lái)存儲(chǔ)動(dòng)畫(huà)結(jié)束位置的X、Y坐標(biāo) mImgcar.getLocationInWindow(end_location);// shopCart是那個(gè)購(gòu)物車(chē) int width = getWindowsWidth(mActivity); // 計(jì)算位移 int endY = end_location[1] - start_location[1];// 動(dòng)畫(huà)位移的y坐標(biāo) int endX = 0 - start_location[0] + (mImgcar.getWidth() / 2);// 動(dòng)畫(huà)位移的X坐標(biāo) TranslateAnimation translateAnimationX = new TranslateAnimation(0,endX, 0, 0); translateAnimationX.setInterpolator(new LinearInterpolator()); translateAnimationX.setRepeatCount(0);// 動(dòng)畫(huà)重復(fù)執(zhí)行的次數(shù) translateAnimationX.setFillAfter(true); TranslateAnimation translateAnimationY = new TranslateAnimation(0, 0,0, endY); translateAnimationY.setInterpolator(new AccelerateInterpolator()); translateAnimationY.setRepeatCount(0);// 動(dòng)畫(huà)重復(fù)執(zhí)行的次數(shù) translateAnimationX.setFillAfter(true); ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.3f, 1.0f, 0.3f); scaleAnimation.setInterpolator(new AccelerateInterpolator()); scaleAnimation.setRepeatCount(0);// 動(dòng)畫(huà)重復(fù)執(zhí)行的次數(shù) scaleAnimation.setFillAfter(true); scaleAnimation.setDuration(300); final AnimationSet set = new AnimationSet(false); set.setFillAfter(false); set.addAnimation(translateAnimationY); set.addAnimation(translateAnimationX); //set.setStartOffset(300); set.setDuration(800);// 動(dòng)畫(huà)的執(zhí)行時(shí)間 view.startAnimation(set); // 動(dòng)畫(huà)監(jiān)聽(tīng)事件 set.setAnimationListener(new Animation.AnimationListener() { // 動(dòng)畫(huà)的開(kāi)始 @Override public void onAnimationStart(Animation animation) { v.setVisibility(View.VISIBLE); } @Override public void onAnimationRepeat(Animation animation) {} // 動(dòng)畫(huà)的結(jié)束 @Override public void onAnimationEnd(Animation animation) { v.setVisibility(View.GONE); anim_mask_layout.removeAllViews(); YoYo.with(Techniques.Bounce).withListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) {} @Override public void onAnimationEnd(Animator animation) { mEndAnimListener.onEndAnim(); } @Override public void onAnimationCancel(Animator animation) {} @Override public void onAnimationRepeat(Animator animation) {} }).interpolate(new BounceInterpolator()).duration(400).playOn(mImgcar); } });}
在這堆復(fù)雜的代碼中,我們只需要關(guān)心這兩句代碼即可:
// 計(jì)算位移int endY = end_location[1] - start_location[1];// 動(dòng)畫(huà)位移的y坐標(biāo)int endX = end_location[0] - start_location[0] + (mImgcar.getWidth() / 2);// 動(dòng)畫(huà)位移的X坐標(biāo)
在我們的手機(jī)界面里,左上角為(0,0),所以從上向下是正,從左向右是正,所以:
結(jié)束位置的縱坐標(biāo)減去開(kāi)始位置的縱坐標(biāo)作為動(dòng)畫(huà)位移的y距離;
結(jié)束位置的橫坐標(biāo)減去開(kāi)始位置的橫坐標(biāo)再加上結(jié)束位置的view的一半作為動(dòng)畫(huà)移動(dòng)的x距離。
動(dòng)畫(huà)效果就實(shí)現(xiàn)了,當(dāng)然如果你想實(shí)現(xiàn)其他效果只需要修改GoodsAnimUtil類(lèi)即可,而主代碼中的代碼不需要改變,這就體現(xiàn)出了低耦合的重要性;
3.采用xutils框架實(shí)現(xiàn)本地?cái)?shù)據(jù)庫(kù)
GoodsBean .java
@Table(name = "goods")public class GoodsBean extends GoodsBase{ @Column(column = "menupos") private int menupos; @Column(column = "goodsid") private int goodsid; @Column(column = "goodsnum") private String goodsnum; @Column(column = "goodsprice") private String goodsprice; public int getMenupos() { return menupos; } public void setMenupos(int menupos) { this.menupos = menupos; } public int getGoodsid() { return goodsid; } public void setGoodsid(int goodsid) { this.goodsid = goodsid; } public String getGoodsnum() { return goodsnum; } public void setGoodsnum(String goodsnum) { this.goodsnum = goodsnum; } public String getGoodsprice() { return goodsprice; } public void setGoodsprice(String goodsprice) { this.goodsprice = goodsprice; } @Override public String toString() { return "GoodsBean{" + "menupos='" + menupos + '\'' + ", goodsid='" + goodsid + '\'' + ", goodsnum='" + goodsnum + '\'' + ", goodsprice='" + goodsprice + '\'' + '}'; }}
新建數(shù)據(jù)庫(kù)goods,菜單欄每項(xiàng)對(duì)應(yīng)的menupos、商品的goodsid、存儲(chǔ)數(shù)量goodsnum和每個(gè)商品對(duì)應(yīng)的價(jià)格goodsprice這幾個(gè)字段是我們?cè)谶M(jìn)行數(shù)據(jù)庫(kù)操作的時(shí)候需要使用到的字段;
分析效果圖:
a.點(diǎn)擊加號(hào),根據(jù)菜單欄的menupos和對(duì)應(yīng)的商品的goodsid進(jìn)行存儲(chǔ)數(shù)據(jù);
b.點(diǎn)擊減號(hào),根據(jù)菜單欄的menupos和對(duì)應(yīng)的商品的goodsid進(jìn)行存儲(chǔ)數(shù)據(jù);
c.點(diǎn)擊菜單欄的item,根據(jù)菜單欄的menupos得到二級(jí)每個(gè)商品已經(jīng)存儲(chǔ)的數(shù)量;
d.點(diǎn)擊菜單欄的item,根據(jù)菜單欄的menupos得到二級(jí)所有商品的存儲(chǔ)數(shù)量;
e.點(diǎn)擊菜單欄的item,根據(jù)菜單欄的menupos得到二級(jí)所有商品的存儲(chǔ)價(jià)格;
f.刪除所有的存儲(chǔ)數(shù)據(jù)。
根據(jù)分析結(jié)果,編寫(xiě)接口以及我們需要實(shí)現(xiàn)的方法:
GoodsDataBaseInterface .java
public interface GoodsDataBaseInterface { /** 添加和刪除購(gòu)物的數(shù)量 */ int saveGoodsNumber(Context context, int menupos, int goodsid, String goodsnum, String goodsprice); /** 根據(jù)下標(biāo)得到 第二級(jí)對(duì)應(yīng)購(gòu)物的數(shù)量 */ int getSecondGoodsNumber(Context context, int menupos, int goodsid); /** 根據(jù)第一級(jí)的下標(biāo) 得到第二級(jí)的所有購(gòu)物數(shù)量 */ int getSecondGoodsNumberAll(Context context, int menupos); /** 根據(jù)第一級(jí)的下標(biāo) 得到第二級(jí)的所有購(gòu)物的價(jià)格 */ int getSecondGoodsPriceAll(Context context, int menupos); /** 刪除所有的購(gòu)物數(shù)據(jù) */ void deleteAll(Context context);}
編寫(xiě)實(shí)現(xiàn)類(lèi)OperateGoodsDataBase繼承GoodsDataBaseInterface 接口:
public class OperateGoodsDataBase implements GoodsDataBaseInterface{ private static OperateGoodsDataBase instance = new OperateGoodsDataBase(); public static OperateGoodsDataBase getInstance(){ return instance; } private OperateGoodsDataBase(){} /** *添加和刪除商品數(shù)量,并得到商品數(shù)量 */ @Override public int saveGoodsNumber(Context context, int menupos, int goodsid, String goodsnum , String goodsprice) { return OperateGoodsDataBaseStatic.saveGoodsNumber(context , menupos , goodsid , goodsnum ,goodsprice); } /** *根據(jù)下標(biāo)得到 第二級(jí)對(duì)應(yīng)購(gòu)物的數(shù)量 */ @Override public int getSecondGoodsNumber(Context context, int menupos, int goodsid) { return OperateGoodsDataBaseStatic.getSecondGoodsNumber(context, menupos, goodsid); } /** * 根據(jù)第一級(jí)的下標(biāo) 得到第二級(jí)的所有購(gòu)物數(shù)量 */ @Override public int getSecondGoodsNumberAll(Context context, int menupos) { return OperateGoodsDataBaseStatic.getSecondGoodsNumberAll(context, menupos); } /** *據(jù)第一級(jí)的下標(biāo) 得到第二級(jí)的所有購(gòu)物的價(jià)格 */ @Override public int getSecondGoodsPriceAll(Context context, int menupos) { return OperateGoodsDataBaseStatic.getSecondGoodsPriceAll(context, menupos); } /** *刪除所有的購(gòu)物數(shù)據(jù) */ @Override public void deleteAll(Context context) { OperateGoodsDataBaseStatic.deleteAll(context); }}
在實(shí)現(xiàn)類(lèi)里面寫(xiě)了一個(gè)單例,這樣在其他類(lèi)里面調(diào)用該單例只會(huì)有一個(gè)實(shí)現(xiàn)類(lèi),減少了內(nèi)存的消耗;
OperateGoodsDataBaseStatic.java
public class OperateGoodsDataBaseStatic{ /** *添加和刪除商品數(shù)量 */ public static int saveGoodsNumber(Context context, int menupos, int goodsid, String goodsnum , String goodsprice) { DbUtils utils = DbUtils.create(context); GoodsBean goodsBean = null; goodsBean =new GoodsBean(); goodsBean.setMenupos(menupos); goodsBean.setGoodsid(goodsid); goodsBean.setGoodsnum(goodsnum); goodsBean.setGoodsprice(goodsprice); try { GoodsBean bean = utils.findFirst(Selector.from(GoodsBean.class).where("menupos" , "=" , menupos).and("goodsid", "=", goodsid)); //如果有這條數(shù)據(jù),數(shù)量直接加1;否則就插入表里面 if(bean == null){ Log.e("TAG", "還沒(méi)有該商品"); utils.save(goodsBean); Log.e("TAG" , "該商品已經(jīng)存儲(chǔ)"); return getSecondGoodsNumber(context , menupos , goodsid); }else{ Log.e("TAG" , "已經(jīng)有該商品"); //返回添加商品之后的商品總數(shù) return updateNum(context, menupos, goodsid, goodsnum); } } catch (DbException e) { e.printStackTrace(); } Log.e("TAG" , "添加商品失敗"); utils.close(); return 0; } /**修改數(shù)量,直接傳入數(shù)量**/ public static int updateNum(Context context , int menupos , int goodsid , String goodsnum){ DbUtils utils = DbUtils.create(context); try { GoodsBean bean = utils.findFirst(Selector.from(GoodsBean.class).where("menupos", "=", menupos).and("goodsid", "=", goodsid)); bean.setGoodsnum(goodsnum); utils.update(bean); Log.e("TAG", "該商品數(shù)量改變?yōu)椋? + getSecondGoodsNumber(context, menupos, goodsid)); return getSecondGoodsNumber(context , menupos , goodsid); } catch (DbException e) { e.printStackTrace(); } utils.close(); return 0; } /** *根據(jù)下標(biāo)得到 第二級(jí)對(duì)應(yīng)購(gòu)物的數(shù)量 */ public static int getSecondGoodsNumber(Context context , int menupos , int goodsid) { DbUtils utils = DbUtils.create(context); if(utils == null){ Log.e("TAG" , "還沒(méi)有該數(shù)據(jù)庫(kù)"); return 0; } try { GoodsBean bean = utils.findFirst(Selector.from(GoodsBean.class).where("menupos", "=", menupos).and("goodsid", "=", goodsid)); if(bean == null){ Log.e("TAG" , "還沒(méi)有該存儲(chǔ)商品"); return 0; }else{ Log.e("TAG" , "獲取商品數(shù)量成功:" + Integer.parseInt(bean.getGoodsnum())); return Integer.parseInt(bean.getGoodsnum()); } } catch (DbException e) { e.printStackTrace(); } utils.close(); Log.e("TAG", "獲取商品數(shù)量失敗"); return 0; } /** * 根據(jù)第一級(jí)的下標(biāo) 得到第二級(jí)的所有購(gòu)物數(shù)量 */ public static int getSecondGoodsNumberAll(Context context, int menupos) { DbUtils utils = DbUtils.create(context); int mSecondGoodsNum = 0; ArrayList<GoodsBean> mGoodsBeanList = null; mGoodsBeanList = getSecondGoodsTypeList(context); if(mGoodsBeanList == null){ Log.e("TAG" , "獲取商品類(lèi)型總數(shù)失敗"); return 0; } for(int i = 0 ; i < mGoodsBeanList.size() ; i++){ if(mGoodsBeanList.get(i).getMenupos() == menupos){ mSecondGoodsNum += Integer.parseInt(mGoodsBeanList.get(i).getGoodsnum()); } } Log.e("TAG", "根據(jù)第一級(jí)的下標(biāo) 得到第二級(jí)的所有購(gòu)物數(shù)量成功:" + mSecondGoodsNum); utils.close(); return mSecondGoodsNum; } /** *根據(jù)第一級(jí)的下標(biāo) 得到第二級(jí)商品所有商品類(lèi)型集合 */ public static ArrayList<GoodsBean> getSecondGoodsTypeList(Context context){ DbUtils utils = DbUtils.create(context); ArrayList<GoodsBean> list = null; try { list = (ArrayList<GoodsBean>) DbUtils.create(context).findAll(GoodsBean.class); if(list == null){ Log.e("TAG" , "該二級(jí)商品還沒(méi)有存儲(chǔ)數(shù)據(jù)"); return null; }else{ Log.e("TAG" , "根據(jù)第一級(jí)的下標(biāo) 得到第二級(jí)商品類(lèi)型總數(shù)成功:" + list.size()); return list; } } catch (DbException e) { e.printStackTrace(); } Log.e("TAG" , "根據(jù)第一級(jí)的下標(biāo) 得到第二級(jí)商品類(lèi)型總數(shù)失敗"); return null; } /** *據(jù)第一級(jí)的下標(biāo) 得到第二級(jí)的所有購(gòu)物的價(jià)格 */ public static int getSecondGoodsPriceAll(Context context, int menupos) { DbUtils utils = DbUtils.create(context); int mSecondGoodsPrice = 0; ArrayList<GoodsBean> mGoodsBeanList = null; mGoodsBeanList = getSecondGoodsTypeList(context); if(mGoodsBeanList == null){ Log.e("TAG" , "獲取商品類(lèi)型總數(shù)失敗"); return 0; } for(int i = 0 ; i < mGoodsBeanList.size(); i++){ if(mGoodsBeanList.get(i).getMenupos() == menupos){ mSecondGoodsPrice += Integer.parseInt(mGoodsBeanList.get(i).getGoodsnum()) * Integer.parseInt(mGoodsBeanList.get(i).getGoodsprice()); } } Log.e("TAG" , "根據(jù)第一級(jí)的下標(biāo) 得到第二級(jí)的所有購(gòu)物的價(jià)格成功:" + mSecondGoodsPrice); utils.close(); Log.e("TAG" , "根據(jù)第一級(jí)的下標(biāo) 得到第二級(jí)的所有購(gòu)物的價(jià)格失敗"); return mSecondGoodsPrice; } /** *刪除所有的購(gòu)物數(shù)據(jù) */ public static void deleteAll(Context context) { DbUtils utils = DbUtils.create(context); try { List<GoodsBean> records = utils.findAll(GoodsBean.class); utils.deleteAll(records); } catch (DbException e) { e.printStackTrace(); } utils.close(); }}
對(duì)于數(shù)據(jù)庫(kù)的操作,沒(méi)有什么復(fù)雜的邏輯,只需要記住基本的代碼就可以了,所以就不進(jìn)行講解了,里面?zhèn)渥?xiě)的也十分詳細(xì);
RecyclerViewContentAdapter.java
a.在onBindViewHolder()方法里面根據(jù)點(diǎn)擊的一級(jí)菜單的position和商品id,判斷數(shù)據(jù)庫(kù)里面是否有該商品,有該商品則顯示減號(hào)圖標(biāo)和對(duì)應(yīng)的存儲(chǔ)數(shù)量;
//綁定ViewHolder@Overridepublic void onBindViewHolder(final ViewHolder holder, final int position) { holder.mImageView.setImageResource(DemoData.ListMenu_PIMAGES[position]); holder.mTitle.setText(DemoData.ListMenu_PTITLE[position]); holder.mYueSale.setText("月售" + DemoData.ListMenu_NUMBER[position]); holder.mPrice.setText(DemoData.ListMenu_PPRICE[position]); holder.mRatingBar.setRating(Float.parseFloat(DemoData.ListMenu_STAR[position])); holder.mRatingBar.getRating(); /** 獲取存儲(chǔ)的商品數(shù)量 */ if (mGoodsDataBaseInterface.getSecondGoodsNumber(mContext, MainActivity.SELECTPOSITION , DemoData.ListMenu_GOODSID[holder.getPosition()]) == 0) { holder.mNumber.setText(""); holder.mNumber.setVisibility(View.GONE); holder.mImgJian.setVisibility(View.GONE); } else { holder.mNumber.setText("" + mGoodsDataBaseInterface.getSecondGoodsNumber(mContext, MainActivity.SELECTPOSITION , DemoData.ListMenu_GOODSID[holder.getPosition()])); holder.mNumber.setVisibility(View.VISIBLE); holder.mImgJian.setVisibility(View.VISIBLE); } setOnListtener(holder);}
b.自定義item點(diǎn)擊、長(zhǎng)點(diǎn)擊、添加商品和減少商品的監(jiān)聽(tīng)事件;
//觸發(fā)protected void setOnListtener(final ViewHolder holder){ if(mOnItemClickListener != null){ holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mOnItemClickListener.onItemClick(holder); } }); holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { mOnItemClickListener.onItemLongClick(holder); return true; } }); holder.mImgJia.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mOnItemClickListener.onItemJiaClick(holder); } }); holder.mImgJian.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mOnItemClickListener.onItemJianClick(holder); } }); }}
c.點(diǎn)擊加號(hào)和減號(hào)的時(shí)候,進(jìn)行相關(guān)的操作;
MainActivity.java
mRecyclerViewContentCommonadapter.setOnItemClickListener(new RecyclerViewContentAdapter.OnItemClickListener() {@Overridepublic void onItemClick(RecyclerViewContentAdapter.ViewHolder holder) {}@Overridepublic void onItemLongClick(RecyclerViewContentAdapter.ViewHolder holder) {}/** 添加 */@Overridepublic void onItemJiaClick(RecyclerViewContentAdapter.ViewHolder holder) { String numText = holder.mNumber.getText().toString().trim(); /** 點(diǎn)擊加號(hào)之前還沒(méi)有數(shù)據(jù)的時(shí)候 */ if (numText.isEmpty() || numText.equals("0")) { Log.e("TAG", "點(diǎn)擊獲取信息:SELECTPOSITION--" + SELECTPOSITION + " DemoData.ListMenu_GOODSID[position]--" + DemoData.ListMenu_GOODSID[holder.getPosition()]); holder.mImgJian.setVisibility(View.VISIBLE); holder.mNumber.setText(mGoodsDataBaseInterface.saveGoodsNumber(mContext, SELECTPOSITION, DemoData.ListMenu_GOODSID[holder.getPosition()], "1", DemoData.ListMenu_PPRICE[holder.getPosition()]) + ""); holder.mNumber.setVisibility(View.VISIBLE); }/** 點(diǎn)擊加號(hào)之前有數(shù)據(jù)的時(shí)候 */ else { holder.mNumber.setText(mGoodsDataBaseInterface.saveGoodsNumber(mContext, SELECTPOSITION, DemoData.ListMenu_GOODSID[holder.getPosition()], String.valueOf(Integer.parseInt(numText) + 1), DemoData.ListMenu_PPRICE[holder.getPosition()]) + ""); } /** 動(dòng)畫(huà) */ GoodsAnimUtil.setAnim(MainActivity.this, holder.mImgJia, mCarLay); GoodsAnimUtil.setOnEndAnimListener(new onEndAnim()); /** 統(tǒng)計(jì)購(gòu)物總數(shù)和購(gòu)物總價(jià) */}/** 減少 */@Overridepublic void onItemJianClick(RecyclerViewContentAdapter.ViewHolder holder) { String numText = holder.mNumber.getText().toString().trim(); holder.mNumber.setText(mGoodsDataBaseInterface.saveGoodsNumber(mContext, SELECTPOSITION, DemoData.ListMenu_GOODSID[holder.getPosition()], String.valueOf(Integer.parseInt(numText) - 1), DemoData.ListMenu_PPRICE[holder.getPosition()]) + ""); numText = holder.mNumber.getText().toString().trim(); /** 減完之后 數(shù)據(jù)為0 */ if (numText.equals("0")) { holder.mNumber.setVisibility(View.GONE); holder.mImgJian.setVisibility(View.GONE); } setAll();}});
d.在點(diǎn)擊item的點(diǎn)擊事件、點(diǎn)擊加號(hào)監(jiān)聽(tīng)動(dòng)畫(huà)結(jié)束和點(diǎn)擊減號(hào)的時(shí)候都要調(diào)用setAll()方法進(jìn)行商品總數(shù)和商品價(jià)格的數(shù)據(jù)更新;
/** * 點(diǎn)擊加號(hào)和減號(hào)的時(shí)候設(shè)置總數(shù)和總價(jià)格 */private void setAll() { //設(shè)置所有購(gòu)物數(shù)量 if (mGoodsDataBaseInterface.getSecondGoodsNumberAll(mContext, SELECTPOSITION) == 0) { mListAllNum.setVisibility(View.GONE); mListAllPrice.setText("¥0"); mListAllNum.setText("0"); } else { mListAllPrice.setText("¥" + mGoodsDataBaseInterface.getSecondGoodsPriceAll(mContext, SELECTPOSITION) + ""); mListAllNum.setText(mGoodsDataBaseInterface.getSecondGoodsNumberAll(mContext, SELECTPOSITION) + ""); mListAllNum.setVisibility(View.VISIBLE); }}
全部主代碼 MainActivity.java
public class MainActivity extends BaseActivity { /** * 標(biāo)題 */ @InjectView(R.id.m_fml_title_tv) TextView mTitle; /** * 側(cè)邊欄菜單RecyclerView */ @InjectView(R.id.m_list_menu) RecyclerView mRecyclerMenu; /** * 內(nèi)容RecyclerView */ @InjectView(R.id.m_list_content) RecyclerView mRecyclerContent; /** * 商品總價(jià) */ @InjectView(R.id.m_list_all_price) TextView mListAllPrice; /** * 物品總數(shù)量 */ @InjectView(R.id.m_list_num) TextView mListAllNum; /** * 側(cè)邊欄菜單數(shù)據(jù)填充器 */ private RecyclerViewMenuAdapter mRecyclerViewMenuCommonadapter = null; /** * 內(nèi)容數(shù)據(jù)填充器 */ private RecyclerViewContentAdapter mRecyclerViewContentCommonadapter = null; private Context mContext; /** * 存儲(chǔ)數(shù)據(jù)list */ private List<String> stringMenuList = new ArrayList<String>(); private List<String> stringContentList = new ArrayList<String>(); public static int SELECTPOSITION = 0; /** * 數(shù)據(jù)操作接口 */ GoodsDataBaseInterface mGoodsDataBaseInterface = null; /** * 購(gòu)物車(chē)布局 */ @InjectView(R.id.m_list_car_lay) RelativeLayout mCarLay; @Override protected void onCreate(Bundle arg0) { super.onCreate(arg0); setContentView(R.layout.activity_main); ButterKnife.inject(MainActivity.this); mContext = this; initView(); setRecyclerView(); initHttp(); } private void initView() { mGoodsDataBaseInterface = OperateGoodsDataBase.getInstance(); //清空數(shù)據(jù)庫(kù)緩存 mGoodsDataBaseInterface.deleteAll(mContext); mTitle.setText("列表菜單"); } /** * 模擬網(wǎng)絡(luò)請(qǐng)求數(shù)據(jù) */ private void initHttp() { for (int i = 0; i < 10; i++) { stringMenuList.add("1111"); } for (int i = 0; i < 4; i++) { stringContentList.add("2222"); } setMenuCommonadapter(); setContentCommonadapter(); } @OnClick({R.id.m_fml_title_back, R.id.m_list_submit}) void onClick(View v) { switch (v.getId()) { case R.id.m_fml_title_back: finish(); break; case R.id.m_list_submit: Toast.makeText(mContext, "提交", Toast.LENGTH_SHORT).show(); break; default: break; } } /** * 設(shè)置RecyclerView的布局方式 */ private void setRecyclerView() { //垂直listview顯示方式 mRecyclerMenu.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false)); mRecyclerMenu.addItemDecoration(new DividerItemDecoration(mContext, LinearLayoutManager.VERTICAL)); mRecyclerContent.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false)); } /** * 菜單列表 數(shù)據(jù)填充 */ private void setMenuCommonadapter() { mRecyclerViewMenuCommonadapter = new RecyclerViewMenuAdapter(mContext, stringMenuList); mRecyclerMenu.setAdapter(mRecyclerViewMenuCommonadapter); mRecyclerViewMenuCommonadapter.setOnItemClickListener(new RecyclerViewMenuAdapter.OnItemClickListener() { @Override public void onItemClick(View v, int position) { SELECTPOSITION = position; Log.e("TAG", "SELECTPOSITION:" + SELECTPOSITION); mRecyclerViewMenuCommonadapter.notifyDataSetChanged(); mRecyclerViewContentCommonadapter.notifyDataSetChanged(); setAll(); } @Override public void onItemLongClick(View v, int position) {} }); } /** * 商品種類(lèi)列表 數(shù)據(jù)填充 */ private void setContentCommonadapter() { mRecyclerViewContentCommonadapter = new RecyclerViewContentAdapter(mContext, stringContentList); mRecyclerContent.setAdapter(mRecyclerViewContentCommonadapter); mRecyclerViewContentCommonadapter.setOnItemClickListener(new RecyclerViewContentAdapter.OnItemClickListener() { @Override public void onItemClick(RecyclerViewContentAdapter.ViewHolder holder) {} @Override public void onItemLongClick(RecyclerViewContentAdapter.ViewHolder holder) {} /** 添加 */ @Override public void onItemJiaClick(RecyclerViewContentAdapter.ViewHolder holder) { String numText = holder.mNumber.getText().toString().trim(); /** 點(diǎn)擊加號(hào)之前還沒(méi)有數(shù)據(jù)的時(shí)候 */ if (numText.isEmpty() || numText.equals("0")) { Log.e("TAG", "點(diǎn)擊獲取信息:SELECTPOSITION--" + SELECTPOSITION + " DemoData.ListMenu_GOODSID[position]--" + DemoData.ListMenu_GOODSID[holder.getPosition()]); holder.mImgJian.setVisibility(View.VISIBLE); holder.mNumber.setText(mGoodsDataBaseInterface.saveGoodsNumber(mContext, SELECTPOSITION, DemoData.ListMenu_GOODSID[holder.getPosition()], "1", DemoData.ListMenu_PPRICE[holder.getPosition()]) + ""); holder.mNumber.setVisibility(View.VISIBLE); }/** 點(diǎn)擊加號(hào)之前有數(shù)據(jù)的時(shí)候 */ else { holder.mNumber.setText(mGoodsDataBaseInterface.saveGoodsNumber(mContext, SELECTPOSITION, DemoData.ListMenu_GOODSID[holder.getPosition()], String.valueOf(Integer.parseInt(numText) + 1), DemoData.ListMenu_PPRICE[holder.getPosition()]) + ""); } /** 動(dòng)畫(huà) */ GoodsAnimUtil.setAnim(MainActivity.this, holder.mImgJia, mCarLay); GoodsAnimUtil.setOnEndAnimListener(new onEndAnim()); /** 統(tǒng)計(jì)購(gòu)物總數(shù)和購(gòu)物總價(jià) */ } /** 減少 */ @Override public void onItemJianClick(RecyclerViewContentAdapter.ViewHolder holder) { String numText = holder.mNumber.getText().toString().trim(); holder.mNumber.setText(mGoodsDataBaseInterface.saveGoodsNumber(mContext, SELECTPOSITION, DemoData.ListMenu_GOODSID[holder.getPosition()], String.valueOf(Integer.parseInt(numText) - 1), DemoData.ListMenu_PPRICE[holder.getPosition()]) + ""); numText = holder.mNumber.getText().toString().trim(); /** 減完之后 數(shù)據(jù)為0 */ if (numText.equals("0")) { holder.mNumber.setVisibility(View.GONE); holder.mImgJian.setVisibility(View.GONE); } setAll(); } }); } /** * 動(dòng)畫(huà)結(jié)束后,更新所有數(shù)量和所有價(jià)格 */ class onEndAnim implements GoodsAnimUtil.OnEndAnimListener { @Override public void onEndAnim() { setAll(); } } /** * 點(diǎn)擊加號(hào)和減號(hào)的時(shí)候設(shè)置總數(shù)和總價(jià)格 */ private void setAll() { //設(shè)置所有購(gòu)物數(shù)量 if (mGoodsDataBaseInterface.getSecondGoodsNumberAll(mContext, SELECTPOSITION) == 0) { mListAllNum.setVisibility(View.GONE); mListAllPrice.setText("¥0"); mListAllNum.setText("0"); } else { mListAllPrice.setText("¥" + mGoodsDataBaseInterface.getSecondGoodsPriceAll(mContext, SELECTPOSITION) + ""); mListAllNum.setText(mGoodsDataBaseInterface.getSecondGoodsNumberAll(mContext, SELECTPOSITION) + ""); mListAllNum.setVisibility(View.VISIBLE); } }}
由于牽扯到3個(gè)知識(shí)點(diǎn),所以邏輯可能有些亂,所以大家可以查看我編寫(xiě)的代碼,里面注釋十分詳細(xì),有意見(jiàn)或者疑問(wèn)的可以共同商討。
聯(lián)系客服