免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
實(shí)現(xiàn)美團(tuán)、餓了么購(gòu)物車(chē)效果,并本地存儲(chǔ)相關(guān)數(shù)據(jù)
分類(lèi):

版權(quán)聲明:轉(zhuǎn)載請(qǐng)標(biāo)明轉(zhuǎn)載地址!

目錄(?)[+]

前言

圣誕節(jié)快到了,在這里先祝大家圣誕節(jié)快樂(lè)!


這篇文章就當(dāng)我送給大家的節(jié)日禮物了(可能寫(xiě)的博客技術(shù)含量不是很高或者有些許錯(cuò)誤,希望大家諒解)。

效果圖

分析

有很多時(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)。

  1. 對(duì)于左側(cè)菜單和右側(cè)內(nèi)容部分聯(lián)動(dòng)的效果——采用兩個(gè)RecyclerView控件
  2. 添加購(gòu)物車(chē)動(dòng)畫(huà)的實(shí)現(xiàn)——采用TranslateAnimation動(dòng)畫(huà)
  3. 數(shù)據(jù)的存儲(chǔ)、獲取以及刪除——采用xutils框架

是不是感覺(jué)簡(jiǎn)單分析之后,思路清晰多了,實(shí)現(xiàn)起來(lái)也知道該從哪里入手了。

代碼實(shí)現(xiàn)

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

主布局相對(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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

菜單布局左右兩側(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)行查看。
  • 1
  • 1

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);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

RecyclerViewContentAdapter.java

左側(cè)內(nèi)容填充器則為普通的內(nèi)容沒(méi)有什么特殊的地方
  • 1
  • 1

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) {}     });}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

當(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());
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

首先調(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);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • imgphoto代表我們點(diǎn)擊的view,通過(guò)imgphoto.getLocationInWindow(start_location)得帶我們點(diǎn)擊view的橫坐標(biāo)和縱坐標(biāo),并存在start_location數(shù)組里面;
  • imgcar代表結(jié)束位置的view,也是通過(guò)mImgcar.getLocationInWindow(end_location)得到結(jié)束位置的橫坐標(biāo)和縱坐標(biāo),并存在end_location數(shù)組里面。
/** *開(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);        }    });}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63

在這堆復(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)
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

在我們的手機(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 + '\'' +                '}';    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

新建數(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);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

編寫(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);   }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

在實(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();    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151

對(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);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

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);           }       });   }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

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();}});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

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); }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

全部主代碼 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);        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191

由于牽扯到3個(gè)知識(shí)點(diǎn),所以邏輯可能有些亂,所以大家可以查看我編寫(xiě)的代碼,里面注釋十分詳細(xì),有意見(jiàn)或者疑問(wèn)的可以共同商討。

源碼下載地址

3
0

我的同類(lèi)文章

參考知識(shí)庫(kù)

猜你在找
查看評(píng)論
8樓 yedongguan 2016-07-13 15:35發(fā)表 [回復(fù)]
請(qǐng)問(wèn)一下,如何從這個(gè)Demo中獲取數(shù)據(jù)傳到下一個(gè)界面里面?
Re: Mr豐 2016-07-14 09:12發(fā)表 [回復(fù)]
回復(fù)yedongguan:數(shù)據(jù)是使用xutils數(shù)據(jù)庫(kù)存儲(chǔ)的,所以在下一個(gè)界面從數(shù)據(jù)庫(kù)中取出就可以
7樓 qq_21864809 2016-06-30 14:36發(fā)表 [回復(fù)]
點(diǎn)快了會(huì)從右側(cè)上面飄下來(lái)
6樓 baidu_35332706 2016-06-20 14:26發(fā)表 [回復(fù)]
可以把jar包發(fā)給我嗎?
Re: Mr豐 2016-06-30 17:53發(fā)表 [回復(fù)]
回復(fù)baidu_35332706:jar包?
5樓 永恒ii 2016-05-06 14:15發(fā)表 [回復(fù)]
現(xiàn)在用xutils的不多吧??
Re: Mr豐 2016-05-06 15:31發(fā)表 [回復(fù)]
回復(fù)qq_26936889:最近常用的存儲(chǔ)方式是什么,推薦一下吧!
4樓 mennaomiqiaoyi8800 2016-03-24 01:14發(fā)表 [回復(fù)]
有一個(gè)問(wèn)題,購(gòu)物車(chē)顯示的商品總量和總價(jià)只對(duì)應(yīng)一個(gè)左側(cè)item,切換一個(gè)item購(gòu)物車(chē)就重新開(kāi)始了,不過(guò)每個(gè)左側(cè)item對(duì)應(yīng)的購(gòu)物車(chē)總量和總價(jià)互補(bǔ)干擾,求問(wèn)大神怎么解決?~~希望購(gòu)物車(chē)顯示的是真正的商品總量和總價(jià)~~
3樓 mennaomiqiaoyi8800 2016-03-24 01:14發(fā)表 [回復(fù)]
有一個(gè)問(wèn)題,購(gòu)物車(chē)顯示的商品總量和總價(jià)只對(duì)應(yīng)一個(gè)左側(cè)item,切換一個(gè)item購(gòu)物車(chē)就重新開(kāi)始了,不過(guò)每個(gè)左側(cè)item對(duì)應(yīng)的購(gòu)物車(chē)總量和總價(jià)互補(bǔ)干擾,求問(wèn)大神怎么解決?~~希望購(gòu)物車(chē)顯示的是真正的商品總量和總價(jià)~~
2樓 山寨戴王 2015-12-29 10:37發(fā)表 [回復(fù)]
好東西。慢慢研究。
1樓 lv910822 2015-12-26 15:39發(fā)表 [回復(fù)]
你好,請(qǐng)問(wèn)GoodsAnimUtil中的YoYo和Techniques這兩個(gè)類(lèi)是引用的哪個(gè)jar包啊
Re: Mr豐 2015-12-26 15:44發(fā)表 [回復(fù)]
回復(fù)lv910822:添加依賴(lài)
compile 'com.daimajia.androidanimations:library:1.1.3@aar'
Re: lv910822 2015-12-26 15:46發(fā)表 [回復(fù)]
你寫(xiě)的是Maven項(xiàng)目?
Re: Mr豐 2015-12-26 15:48發(fā)表 [回復(fù)]
回復(fù)lv910822:Android項(xiàng)目 使用AndroidStudio開(kāi)發(fā)的。。。。
Re: lv910822 2015-12-26 16:03發(fā)表 [回復(fù)]
1025202464,這個(gè)是我的QQ,求幫助啊,大神
Re: lv910822 2015-12-26 15:50發(fā)表 [回復(fù)]
我用eclipse開(kāi)發(fā)的,怎么搞阿。。。
Re: Mr豐 2015-12-26 15:57發(fā)表 [回復(fù)]
回復(fù)lv910822:我找了一個(gè),你可以試一下
https://github.com/fengmaolian/Eclipse-AndroidViewAnimations-1.14.3
發(fā)表評(píng)論
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶(hù)發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
java實(shí)現(xiàn)運(yùn)用HashMap充當(dāng)購(gòu)物車(chē),goodbean充當(dāng)存放數(shù)據(jù)
Android游戲開(kāi)發(fā)----動(dòng)畫(huà)SurfaceView詳解
Android
RecyclerView適配器的超省寫(xiě)法
2.5.0 構(gòu)建一個(gè)可復(fù)用的自定義BaseAdapter | 菜鳥(niǎo)教程
memcached java client 源代碼分析
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服