1.前面部分的內容就不過多說明了,就是OkGo請求資料然后封裝配接器加入資料源,先上代碼
先上配接器的代碼,注意這里使用的萬能配接器,加入依賴
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.0'
package com.example.review;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
public class MyFoodAdaptr extends BaseQuickAdapter<FoodBean.DataBean, BaseViewHolder> {
public MyFoodAdaptr(@Nullable List<FoodBean.DataBean> data) {
super(R.layout.item_food, data);
}
@Override
protected void convert(@NotNull BaseViewHolder baseViewHolder, FoodBean.DataBean dataBean) {
baseViewHolder.setText(R.id.item_text,dataBean.getTitle());
Glide.with(getContext()).load(dataBean.getPic()).into((ImageView) baseViewHolder.getView(R.id.item_img));
CheckBox box = baseViewHolder.getView(R.id.is_checked);
addChildClickViewIds(R.id.is_checked);
box.setChecked(dataBean.isDan_xuan());
if (dataBean.isTan_chu()){
box.setVisibility(View.VISIBLE);
}else {
box.setVisibility(View.GONE);
}
}
}
然后是子布局的頁面詳情
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_height="wrap_content">
<CheckBox
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/is_checked"></CheckBox>
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="@+id/item_img"></ImageView>
<TextView
android:gravity="center"
android:layout_width="200dp"
android:layout_height="200dp"
android:id="@+id/item_text"></TextView>
</LinearLayout>
接下來就是物體類以及OkGo框架的內容
FoodBean物體類,可以Gson決議,注意加入Gson依賴
package com.example.review;
import java.util.List;
public class FoodBean {
/**
* ret : 1
* data : [{"id":"8289","title":"油燜大蝦","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/9/8289.jpg","collect_num":"1670","food_str":"大蝦 蔥 生姜 植物油 料酒","num":1670},{"id":"2127","title":"四川回鍋肉","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2127.jpg","collect_num":"1591","food_str":"豬肉 青蒜 青椒 紅椒 姜片","num":1591},{"id":"30630","title":"超簡單芒果布丁","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/31/30630.jpg","collect_num":"1549","food_str":"QQ糖 牛奶 芒果","num":1549},{"id":"9073","title":"家常紅燒魚","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/10/9073.jpg","collect_num":"1426","food_str":"鮮魚 姜 蔥 蒜 花椒","num":1426},{"id":"10097","title":"家常煎豆腐","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10097.jpg","collect_num":"1419","food_str":"豆腐 新鮮紅椒 青椒 蔥花 油","num":1419},{"id":"10509","title":"水煮肉片","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10509.jpg","collect_num":"1342","food_str":"瘦豬肉 生菜 豆瓣醬 干辣椒 花椒","num":1342},{"id":"46968","title":"紅糖蘋果銀耳湯","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/47/46968.jpg","collect_num":"1253","food_str":"銀耳 蘋果 紅糖","num":1253},{"id":"10191","title":"麻婆豆腐","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10191.jpg","collect_num":"1224","food_str":"豆腐 肉末 生抽 白糖 芝麻油","num":1224},{"id":"2372","title":"皮蛋瘦肉粥","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2372.jpg","collect_num":"1151","food_str":"大米 皮蛋 豬肉 油條 香蔥","num":1151},{"id":"2166","title":"螞蟻上樹","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2166.jpg","collect_num":"1145","food_str":"紅薯粉 肉 姜 蒜 花椒","num":1145},{"id":"2262","title":"糖醋肉","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2262.jpg","collect_num":"1082","food_str":"豬肉 紅椒 黃椒 洋蔥 蛋清","num":1082},{"id":"9971","title":"魚香豆腐","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/10/9971.jpg","collect_num":"1010","food_str":"豆腐 木耳 胡蘿卜 香蔥 番茄醬","num":1010},{"id":"10172","title":"干煸四季豆","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10172.jpg","collect_num":"993","food_str":"四季豆 干辣椒 蒜頭 醬油 糖","num":993},{"id":"2685","title":"胡蘿卜肉末蒸蛋","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2685.jpg","collect_num":"929","food_str":"胡蘿卜 肉 蛋 生抽 鹽","num":929},{"id":"9972","title":"虎皮青椒","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/10/9972.jpg","collect_num":"892","food_str":"青辣椒 大蒜 香醋 白糖 生抽","num":892},{"id":"10437","title":"叉燒排骨","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10437.jpg","collect_num":"806","food_str":"排骨 李錦記叉燒醬 植物油 清水 油菜","num":806},{"id":"2892","title":"\u201c五行\u201d彩蔬湯","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2892.jpg","collect_num":"761","food_str":"黑木耳 玉米 牛蒡 胡蘿卜 西蘭花","num":761},{"id":"2348","title":"麻辣肉絲面","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2348.jpg","collect_num":"760","food_str":"面條 肉絲 淀粉 醬油 辣椒","num":760},{"id":"10044","title":"土豆燉翅根","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10044.jpg","collect_num":"758","food_str":"土豆 翅根 蔥 姜 料酒","num":758},{"id":"33783","title":"美人豆漿","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/34/33783.jpg","collect_num":"758","food_str":"黃豆 紅豆 綠豆 黑豆 黑米","num":758}]
*/
private int ret;
/**
* id : 8289
* title : 油燜大蝦
* pic : http://www.qubaobei.com/ios/cf/uploadfile/132/9/8289.jpg
* collect_num : 1670
* food_str : 大蝦 蔥 生姜 植物油 料酒
* num : 1670
*/
private List<DataBean> data;
public int getRet() {
return ret;
}
public void setRet(int ret) {
this.ret = ret;
}
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
private String id;
private String title;
private String pic;
private String collect_num;
private String food_str;
private int num;
//這三個控制著長按彈出以及全選還有單選
private boolean tan_chu = false;
private boolean quan_xuan = false;
private boolean dan_xuan = false;
public boolean isDan_xuan() {
return dan_xuan;
}
public void setDan_xuan(boolean dan_xuan) {
this.dan_xuan = dan_xuan;
}
public boolean isTan_chu() {
return tan_chu;
}
public void setTan_chu(boolean tan_chu) {
this.tan_chu = tan_chu;
}
public boolean isQuan_xuan() {
return quan_xuan;
}
public void setQuan_xuan(boolean quan_xuan) {
this.quan_xuan = quan_xuan;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
public String getCollect_num() {
return collect_num;
}
public void setCollect_num(String collect_num) {
this.collect_num = collect_num;
}
public String getFood_str() {
return food_str;
}
public void setFood_str(String food_str) {
this.food_str = food_str;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
}
然后是咱們的請求資料并且決議gson,實體化配接器加入資料源,在這里要注意初始化recyclerview的時候給他設定排列方式
rv = (RecyclerView) findViewById(R.id.rv);
rv.setLayoutManager(new LinearLayoutManager(this));
OkGo.<String>get("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1").execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
String json = response.body();
FoodBean foodBean = new Gson().fromJson(json, FoodBean.class);
List<FoodBean.DataBean> data = foodBean.getData();
list.addAll(data);
adaptr = new MyFoodAdaptr(list);
rv.setAdapter(adaptr);
}
重點來了
我們需要累加結算時候的價格,就需要子控制元件的點擊事件,剛好這個版本的萬能配接器可以加入子控制元件的點擊事件,我們在adapter里面加入這個方法
CheckBox box = baseViewHolder.getView(R.id.is_checked);
addChildClickViewIds(R.id.is_checked);
選中之后金錢的計算方法
private void count() {
num = 0;
for (int i = 0; i < list.size(); i++) {
if (list.get(i).isDan_xuan()) {
num += 1000;
}
}
money.setText(num + "");
}
接下來就是子控制元件的單點事件,具體步驟在注釋里面了
adaptr.setOnItemChildClickListener(new OnItemChildClickListener() {
@Override
public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
//判斷點擊的控制元件ID是否和配接器里加入的子控制元件ID一樣
if (view.getId()==R.id.is_checked){
//一樣的話就強轉成CheckBox型別
CheckBox box = (CheckBox) view;
//如果子控制元件box為選中狀態那么當前點擊的這個item里面的 Dan_Xuan 布林值變為和 子控制元件box的值一樣
if (box.isChecked()){
list.get(position).setDan_xuan(box.isChecked());
//coynt()就是金錢的計算方法
count();
}else {
list.get(position).setDan_xuan(box.isChecked());
count();
}
//重繪配接器
adapter.notifyDataSetChanged();
}
}
});
現在展示一下原效果

然后再展示一下點擊之后的效果

4.接下來就是全選了,大體的思想和上面的不變,給全選的id加一個監聽事件,使子控制元件里面的值跟隨全選按鈕的值變動而變動,話不多說,上代碼
allChecked.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
for (int i = 0; i < list.size(); i++) {
list.get(i).setDan_xuan(isChecked);
}
count();
adaptr.notifyDataSetChanged();
}
});
展示一下效果

Finally
這就是模擬購物車頁面全選單選的金額累加,寫的有點亂,下次改進!!!!!!!!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/274767.html
標籤:其他
上一篇:小程式 基礎知識(二)
