mvp實現Xrecyclerview的上下拉和購物車功能(仿餓了麼)
阿新 • • 發佈:2018-12-21
首先先匯入咱們的依賴
implementation 'com.android.support:design:28.0.0' implementation 'com.google.code.gson:gson:2.8.5' implementation 'cn.bingoogolapple:bga-qrcode-zxing:1.2.5' implementation 'com.squareup.okhttp3:okhttp:3.12.0' implementation 'com.jcodecraeer:xrecyclerview:1.5.9' implementation 'com.github.bumptech.glide:glide:4.8.0' implementation 'com.android.support:recyclerview-v7:28.0.0' implementation 'com.youth.banner:banner:1.4.10' implementation 'com.squareup.okhttp3:logging-interceptor:3.12.0'
其次就是我們要用到的許可權
<uses-permission android:name="com.dingtao.week3.permission.JPUSH_MESSAGE"/> <uses-permission android:name="android.permission.RECEIVE_USER_PRESENT"/> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_SETTINGS"/> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
然後就開始寫 我們的主頁面了
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp" android:focusable="true" android:focusableInTouchMode="true"> <ImageView android:id="@+id/btn_back" android:layout_width="50dp" android:layout_height="50dp" android:layout_alignParentLeft="true" android:padding="15dp" android:src="@drawable/btn_back" /> <ImageView android:id="@+id/btn_layout" android:layout_width="50dp" android:layout_height="50dp" android:layout_alignParentRight="true" android:padding="15dp" android:src="@drawable/even_city_draw" /> <EditText android:id="@+id/edit_keywords" android:layout_width="match_parent" android:layout_height="36dp" android:layout_centerVertical="true" android:layout_marginRight="5dp" android:layout_toLeftOf="@+id/btn_layout" android:layout_toRightOf="@+id/btn_back" android:background="@drawable/search_edit_bg" android:hint="搜尋" android:paddingLeft="40dp" android:singleLine="true" android:text="手機" android:textColor="@color/custom_gray" android:textColorHint="@color/grayblack" android:textSize="16sp" /> <ImageView android:id="@+id/btn_search" android:layout_width="30dp" android:layout_height="30dp" android:layout_alignLeft="@+id/edit_keywords" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:src="@android:drawable/ic_search_category_default" /> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:layout_alignRight="@+id/edit_keywords" android:layout_centerVertical="true" android:layout_marginRight="10dp" android:src="@drawable/person_list_sound_up" /> </RelativeLayout> <ImageView android:layout_width="match_parent" android:layout_height="1dp" android:src="@android:color/darker_gray" /> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <TextView android:id="@+id/cate_text1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="綜合▲" android:textColor="@color/set_font_color" android:textSize="14sp"></TextView> <TextView android:id="@+id/cate_text2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="價格▲" android:textColor="@color/set_font_color" android:textSize="14sp"></TextView> <TextView android:id="@+id/cate_text3" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="銷量▲" android:textColor="@color/set_font_color" android:textSize="14sp"></TextView> <TextView android:id="@+id/cate_text4" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="篩選▲" android:textColor="@color/set_font_color" android:textSize="14sp"></TextView> </LinearLayout> <ImageView android:layout_width="match_parent" android:layout_height="1dp" android:src="@android:color/darker_gray" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <com.jcodecraeer.xrecyclerview.XRecyclerView android:id="@+id/list_goods" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_width="match_parent" android:layout_height="match_parent"> </com.jcodecraeer.xrecyclerview.XRecyclerView> <ImageView android:id="@+id/shop_car" android:layout_width="40dp" android:layout_height="40dp" android:layout_alignParentRight="true" android:layout_alignParentBottom="true" android:layout_margin="50dp" android:src="@drawable/gouwuc_r"/> </RelativeLayout> </LinearLayout>
佈局中有自己寫的shap還有自己的照片檔案記得匯入帶drawable-xhdpi下
而xml檔案則直接 匯入到drawable檔案下
先寫bean類
public class Goods implements Serializable {
// "bargainPrice": 111.99,
// "createtime": "2017-10-14T21:39:05",
// "detailUrl": "https:\/\/item.m.jd.com\/product\/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends",
// "images": "https:\/\/m.360buyimg.com\/n0\/jfs\/t9004\/210\/1160833155\/647627\/ad6be059\/59b4f4e1N9a2b1532.jpg!q70.jpg|https:\/\/m.360buyimg.com\/n0\/jfs\/t7504\/338\/63721388\/491286\/f5957f53\/598e95f1N7f2adb87.jpg!q70.jpg|https:\/\/m.360buyimg.com\/n0\/jfs\/t7441\/10\/64242474\/419246\/adb30a7d\/598e95fbNd989ba0a.jpg!q70.jpg",
// "itemtype": 1,
// "pid": 1,
// "price": 118.0,
// "pscid": 1,
// "salenum": 0,
// "sellerid": 17,
// "subhead": "每個中秋都不能簡單,無論身在何處,你總需要一塊餅讓生活更圓滿,京東月餅讓愛更圓滿京東自營,閃電配送,更多驚喜,快用手指戳一下",
// "title": "北京稻香村 稻香村中秋節月餅 老北京月餅禮盒655g"
private double bargainPrice;
private String createtime;
private String detailUrl;
private String images;
private int num;
private int pid;
private double price;
private int pscid;
private int selected;
private int sellerid;
private String subhead;
private String title;
private int count=1;
public void setCount(int count) {
this.count = count;
}
public int getCount() {
return count;
}
public double getBargainPrice() {
return bargainPrice;
}
public void setBargainPrice(double bargainPrice) {
this.bargainPrice = bargainPrice;
}
public String getCreatetime() {
return createtime;
}
public void setCreatetime(String createtime) {
this.createtime = createtime;
}
public String getDetailUrl() {
return detailUrl;
}
public void setDetailUrl(String detailUrl) {
this.detailUrl = detailUrl;
}
public String getImages() {
return images;
}
public void setImages(String images) {
this.images = images;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getPscid() {
return pscid;
}
public void setPscid(int pscid) {
this.pscid = pscid;
}
public int getSelected() {
return selected;
}
public void setSelected(int selected) {
this.selected = selected;
}
public int getSellerid() {
return sellerid;
}
public void setSellerid(int sellerid) {
this.sellerid = sellerid;
}
public String getSubhead() {
return subhead;
}
public void setSubhead(String subhead) {
this.subhead = subhead;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
public class Result<T> {
int code;
String msg;
T data;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
public class Shop {
List<Goods> list;
String sellerName;
String sellerid;
int textColor = 0xffffffff;
int background = R.color.grayblack;
boolean check;
public void setTextColor(int textColor) {
this.textColor = textColor;
}
public int getTextColor() {
return textColor;
}
public void setBackground(int background) {
this.background = background;
}
public int getBackground() {
return background;
}
public void setCheck(boolean check) {
this.check = check;
}
public boolean isCheck() {
return check;
}
public List<Goods> getList() {
return list;
}
public void setList(List<Goods> list) {
this.list = list;
}
public String getSellerName() {
return sellerName;
}
public void setSellerName(String sellerName) {
this.sellerName = sellerName;
}
public String getSellerid() {
return sellerid;
}
public void setSellerid(String sellerid) {
this.sellerid = sellerid;
}
}
core類
public interface DataCall<T> {
void success(T data);
void fail(Result result);
}
public abstract class BasePresenter {
DataCall dataCall;
public BasePresenter(DataCall dataCall){
this.dataCall = dataCall;
}
Handler mHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
Result result = (Result) msg.obj;
if (result.getCode()==0){
dataCall.success(result.getData());
}else{
dataCall.fail(result);
}
}
};
public void requestData(final Object...args){
new Thread(new Runnable() {
@Override
public void run() {
Message message = mHandler.obtainMessage();
message.obj = getData(args);
mHandler.sendMessage(message);
}
}).start();
}
protected abstract Result getData(Object...args);
public void unBindCall(){
this.dataCall = null;
}
}
public class DTApplication extends Application {
private static DTApplication instance;
private SharedPreferences mSharedPreferences;
@Override
public void onCreate() {
super.onCreate();
instance = this;
mSharedPreferences = getSharedPreferences("application",
Context.MODE_PRIVATE);
}
public static DTApplication getInstance() {
return instance;
}
public SharedPreferences getShare() {
return mSharedPreferences;
}
}
//寫完之後記得配置清單檔案
util層
public class HttpUtils {
public static String get(String urlString){
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(urlString).get().build();
try {
Response response = okHttpClient.newCall(request).execute();
String result = response.body().string();
Log.i("dt","請求結果:"+result);
return result;
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
public static String postForm(String url,String[] name,String[] value){
OkHttpClient okHttpClient = new OkHttpClient();
FormBody.Builder formBuild = new FormBody.Builder();
for (int i = 0; i < name.length; i++) {
formBuild.add(name[i],value[i]);
}
Request request = new Request.Builder().url(url).post(formBuild.build()).build();
try {
Response response = okHttpClient.newCall(request).execute();
String result = response.body().string();
Log.i("dt",result);
return result;
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
public static String postFile(String url,String[] name,String[] value,String fileParamName,File file){
OkHttpClient okHttpClient = new OkHttpClient();
MultipartBody.Builder requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM);
if(file != null){
// MediaType.parse() 裡面是上傳的檔案型別。
RequestBody body = RequestBody.create(MediaType.parse("image/*"), file);
String filename = file.getName();
// 引數分別為: 檔案引數名 ,檔名稱 , RequestBody
requestBody.addFormDataPart(fileParamName, "jpg", body);
}
if (name!=null) {
for (int i = 0; i < name.length; i++) {
requestBody.addFormDataPart(name[i], value[i]);
}
}
Request request = new Request.Builder().url(url).post(requestBody.build()).build();
try {
Response response = okHttpClient.newCall(request).execute();
if (response.code()==200) {
return response.body().string();
}
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
public static String postJson(String url,String jsonString){
OkHttpClient okHttpClient = new OkHttpClient();
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"),jsonString);
Request request = new Request.Builder().url(url).post(requestBody).build();
try {
Response response = okHttpClient.newCall(request).execute();
return response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
}
public class GlideImageLoader extends ImageLoader {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
/**
注意:
1.圖片載入器由自己選擇,這裡不限制,只是提供幾種使用方法
2.返回的圖片路徑為Object型別,由於不能確定你到底使用的那種圖片載入器,
傳輸的到的是什麼格式,那麼這種就使用Object接收和返回,你只需要強轉成你傳輸的型別就行,
切記不要胡亂強轉!
*/
//Glide 載入圖片簡單用法
Glide.with(context).load(path).into(imageView);
//Picasso 載入圖片簡單用法
// Picasso.with(context).load(path).into(imageView);
//用fresco載入圖片簡單用法,記得要寫下面的createImageView方法
// Uri uri = Uri.parse((String) path);
// imageView.setImageURI(uri);
}
//提供createImageView 方法,如果不用可以不重寫這個方法,主要是方便自定義ImageView的建立
// @Override
// public ImageView createImageView(Context context) {
// //使用fresco,需要建立它提供的ImageView,當然你也可以用自己自定義的具有圖片載入功能的ImageView
// SimpleDraweeView simpleDraweeView=new SimpleDraweeView(context);
// return simpleDraweeView;
// }
}
這個是設定圖片的東西
然後設定日誌
public class SpacingItemDecoration extends RecyclerView.ItemDecoration {
private int spacing;
public SpacingItemDecoration(int spacing) {
this.spacing = spacing;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
outRect.top = spacing / 2;
outRect.bottom = spacing / 2;
outRect.left = spacing / 2;
outRect.right = spacing / 2;
}
}
寫的自定義view
public class AddSubLayout extends LinearLayout implements View.OnClickListener {
private TextView mAddBtn,mSubBtn;
private TextView mNumText;
private AddSubListener addSubListener;
public AddSubLayout(Context context) {
super(context);
initView();
}
public AddSubLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public AddSubLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
@SuppressLint("NewApi")
public AddSubLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initView();
}
private void initView(){
//載入layout佈局,第三個引數ViewGroup一定寫成this
View view = View.inflate(getContext(),R.layout.car_add_sub_layout,this);
mAddBtn = view.findViewById(R.id.btn_add);
mSubBtn = view.findViewById(R.id.btn_sub);
mNumText = view.findViewById(R.id.text_number);
mAddBtn.setOnClickListener(this);
mSubBtn.setOnClickListener(this);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
int width = r-l;//getWidth();
int height = b-t;//getHeight();
}
@Override
public void onClick(View v) {
int number = Integer.parseInt(mNumText.getText().toString());
switch (v.getId()){
case R.id.btn_add:
number++;
mNumText.setText(number+"");
break;
case R.id.btn_sub:
if (number==0){
Toast.makeText(getContext(),"數量不能小於0",Toast.LENGTH_LONG).show();
return;
}
number--;
mNumText.setText(number+"");
break;
}
if (addSubListener!=null){
addSubListener.addSub(number);
}
}
public void setCount(int count) {
mNumText.setText(count+"");
}
public void setAddSubListener(AddSubListener addSubListener) {
this.addSubListener = addSubListener;
}
public interface AddSubListener{
void addSub(int count);
}
}
model層
public class GoodsListModel {
public static Result goodsList(String keywords, final String page) {
String resultString = HttpUtils.postForm("http://www.zhaoapi.cn/product/searchProducts",
new String[]{"keywords", "page"}, new String[]{keywords, page});
try {
Gson gson = new Gson();
Type type = new TypeToken<Result<List<Goods>>>() {
}.getType();
Result result = gson.fromJson(resultString, type);
// Result<List<Goods>> result = new Result<>();
// result.setCode(0);
// List<Goods> list = new ArrayList<>();
// for (int i = 0; i < 30; i++) {
// Goods goods = new Goods();
// goods.setImages("");
// goods.setTitle("手機"+i);
// list.add(goods);
// }
// result.setData(list);
return result;
} catch (Exception e) {
}
Result result = new Result();
result.setCode(-1);
result.setMsg("資料解析異常");
return result;
}
}
presenter層
public class GoodsListPresenter extends BasePresenter {
private int page=1;
private boolean isRefresh=true;
public GoodsListPresenter(DataCall dataCall) {
super(dataCall);
}
@Override
protected Result getData(Object... args) {
isRefresh = (boolean) args[0];//是否需要重新整理
if (isRefresh){//重新整理
page = 1;
}else{
page++;
}
Result result = GoodsListModel.goodsList((String)args[1],page+"");//呼叫網路請求獲取資料
return result;
}
public boolean isResresh(){
return isRefresh;
}
}
給主頁面的xrecycleview設定介面卡
public class GoodsListAdapter extends RecyclerView.Adapter<GoodsListAdapter.GoodsHodler> {
private List<Goods> mList = new ArrayList<>();//資料集合
private Context context;
public final static int LINEAR_TYPE = 0;//線性
public final static int GRID_TYPE = 1;//網格
private int viewType = LINEAR_TYPE;
private OnItemClickListener onItemClickListener;
private OnItemLongClickListener onItemLongClickListener;
public GoodsListAdapter(Context context) {
this.context = context;
}
@Override
public int getItemViewType(int position) {
return viewType;
}
//設定item的檢視型別
public void setViewType(int viewType) {
this.viewType = viewType;
}
@NonNull
@Override
public GoodsHodler onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
View view = null;
if (viewType == LINEAR_TYPE) {//通過第二個引數viewType判斷選用的檢視
view = View.inflate(viewGroup.getContext(), R.layout.goods_linear_item, null);//載入item佈局
} else {
view = View.inflate(viewGroup.getContext(), R.layout.goods_grid_item, null);//載入item佈局
}
GoodsHodler goodsHodler = new GoodsHodler(view);
return goodsHodler;
}
@Override
public void onBindViewHolder(@NonNull final GoodsHodler goodsHodler, final int position) {
final Goods goods = mList.get(position);//拿到商品,開始賦值
goodsHodler.itemView.setTag(mList.get(position));
//增加點選事件
goodsHodler.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//*******跳轉webActivity進行網頁訪問
// Intent intent = new Intent(context,WebActivity.class);
// intent.putExtra("url",goods.getDetailUrl());
// context.startActivity(intent);
//————————跳轉自定義的詳情頁面
Intent intent = new Intent(context,GoodsDetailActivity.class);
Bundle bundle = new Bundle();//使用bundle傳遞引用資料型別的物件
bundle.putSerializable("goods",goods);
intent.putExtras(bundle);//一定要把值放入了。
context.startActivity(intent);
// if (onItemClickListener!=null) {
// onItemClickListener.onItemClick(goods);
// }
}
});
goodsHodler.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
//*****************方案1*************
// ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(
// goodsHodler.itemView,"alpha",1,0
// );
// objectAnimator.setDuration(1000);
// objectAnimator.setInterpolator(new LinearInterpolator());
// objectAnimator.addListener(new Animator.AnimatorListener() {
// @Override
// public void onAnimationStart(Animator animation) {
//
// }
//
// @Override
// public void onAnimationEnd(Animator animation) {
// mList.remove(position);//動畫執行結束,移除
// notifyDataSetChanged();//重新整理列表
// goodsHodler.itemView.setAlpha(1);//由於複用機制,需要重新改成不透明
// }
//
// @Override
// public void onAnimationCancel(Animator animation) {
// mList.remove(position);//動畫執行結束,移除
// notifyDataSetChanged();//重新整理列表
// goodsHodler.itemView.setAlpha(1);//由於複用機制,需要重新改成不透明
// }
//
// @Override
// public void onAnimationRepeat(Animator animation) {
//
// }
// });
// objectAnimator.start();
//***********方案二**************
if (onItemLongClickListener!=null) {
onItemLongClickListener.onItemLongClick(position);
}
return true;
}
});
goodsHodler.text.setText(goods.getTitle());
//由於我們的資料圖片提供的不標準,所以我們需要切割得到圖片
String imageurl = "https" + goods.getImages().split("https")[1];
Log.i("dt", "imageUrl: " + imageurl);
imageurl = imageurl.substring(0, imageurl.lastIndexOf(".jpg") + ".jpg".length());
Glide.with(context).load(imageurl).into(goodsHodler.imageView);//載入圖片
}
public static void main(String[] args) {
String aa = "a111a222a333a";
String[] b = aa.split("a");
System.out.println(b[0]);
}
@Override
public int getItemCount() {
return mList.size();
}
/**
* 新增集合資料
*/
public void addAll(List<Goods> data) {
if (data != null) {
mList.addAll(data);
}
}
/**
* 清空資料
*/
public void clearList() {
mList.clear();
}
/**
* 設定點選方法
*/
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
public void remove(int position) {
mList.remove(position);
}
public class GoodsHodler extends RecyclerView.ViewHolder {
TextView text;
ImageView imageView;
public GoodsHodler(@NonNull View itemView) {
super(itemView);
text = itemView.findViewById(R.id.text);
imageView = itemView.findViewById(R.id.image);
}
}
/**
* @author dingtao
* @date 2018/12/15 10:28 AM
* 點選介面
*/
public interface OnItemClickListener {
void onItemClick(Goods goods);
}
/**
* @author dingtao
* @date 2018/12/15 10:28 AM
* 點選介面
*/
public interface OnItemLongClickListener {
void onItemLongClick(int position);
}
public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
this.onItemLongClickListener = onItemLongClickListener;
}
}
列表條目佈局
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:minHeight="50dp"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="aa"
android:padding="10dp"/>
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/image"
android:layout_margin="10dp"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:minHeight="50dp"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="aa"
android:padding="10dp"/>
</LinearLayout>
完成
設定點選條目進入詳情頁面
詳情頁佈局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.youth.banner.Banner
android:id="@+id/goods_banner"
android:layout_width="match_parent"
android:layout_height="200dp" />
</LinearLayout>
</ScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<Button
android:id="@+id/goods_add_cart_btn"
android:layout_width="100dp"
android:layout_height="50dp"
android:background="@color/mark_red"
android:textColor="@color/white"
android:layout_alignParentRight="true"
android:text="加入購物車"/>
</RelativeLayout>
</LinearLayout>
詳情頁功能實現
public class GoodsDetailActivity extends AppCompatActivity implements View.OnClickListener {
private Goods mGoods;//商品詳情
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_goods_details);
Bundle bundle = getIntent().getExtras();
mGoods = (Goods) bundle.getSerializable("goods");//讀取列表傳遞過來的商品資料
initBanner();
findViewById(R.id.goods_add_cart_btn).setOnClickListener(this);
}
/**
* 初始化banner
*/
private void initBanner() {
List<String> imageList = new ArrayList<>();//圖片url集合
String[] imageurls = mGoods.getImages().split("https");//對圖片進行切割
for (int i = 0; i < imageurls.length; i++) {
if (!TextUtils.isEmpty(imageurls[i])) {
String url = "https" + imageurls[i];
Log.i("dt", "imageUrl: " + url);
url = url.substring(0, url.lastIndexOf(".jpg") + ".jpg".length());
imageList.add(url);//圖片路徑拼接完成,重新賦值給陣列
}
}
Banner banner = findViewById(R.id.goods_banner);
//設定圖片載入器
banner.setImageLoader(new GlideImageLoader());
//設定banner樣式
banner.setBannerStyle(BannerConfig.NUM_INDICATOR);
//設定圖片集合
banner.setImages(imageList);
//設定標題集合(當banner樣式有顯示title時)
// banner.setBannerTitles(titleList);
//設定banner動畫效果
banner.setBannerAnimation(Transformer.DepthPage);
//設定自動輪播,預設為true
banner.isAutoPlay(false);
//設定輪播時間
// banner.setDelayTime(1500);
//設定指示器位置(當banner模式中有指示器時)
banner.setIndicatorGravity(BannerConfig.RIGHT);
//banner設定方法全部呼叫完畢時最後呼叫
banner.start();
}
@Override
public void onClick(View v) {
if (v.getId()==R.id.goods_add_cart_btn){
Toast.makeText(this,"加入購物車",Toast.LENGTH_LONG).show();
}
}
}
點選購物車進入購物車頁面
購物車頁面的佈局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<android.support.v7.widget.RecyclerView
android:id="@+id/left_recycler"
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="@color/grayblack">
</android.support.v7.widget.RecyclerView>
<android.support.v7.widget.RecyclerView
android:id="@+id/right_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="80dp">
<ImageView
android:id="@+id/shop_car_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:src="@drawable/gouwuc_r" />
<TextView
android:id="@+id/goods_sum_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="價格:"
android:layout_marginLeft="20dp"
android:layout_centerVertical="true"/>
<TextView
android:id="@+id/goods_number"
android:layout_width="30dp"
android:layout_height="30dp"
android:textSize="10sp"
android:gravity="center"
android:textColor="@color/white"
android:layout_marginLeft="-10dp"
android:background="@drawable/circle_red_bg"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/shop_car_image"
android:text="7"
tools:ignore="RtlCompat" />
</RelativeLayout>
</LinearLayout>
model層
public class CartModel {
public static Result goodsList() {
// String resultString = HttpUtils.get("http://www.zhaoapi.cn/product/getCarts?uid=71");
String resultString = "{\"msg\":\"請求成功\",\"code\":\"0\",\"data\":[{\"list\":[],\"sellerName\":\"\",\"sellerid\":\"0\"},{\"list\":[{\"bargainPrice\":111.99,\"createtime\":\"2017-10-14T21:48:08\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t9004\\/210\\/1160833155\\/647627\\/ad6be059\\/59b4f4e1N9a2b1532.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7504\\/338\\/63721388\\/491286\\/f5957f53\\/598e95f1N7f2adb87.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7441\\/10\\/64242474\\/419246\\/adb30a7d\\/598e95fbNd989ba0a.jpg!q70.jpg\",\"num\":11,\"pid\":11,\"price\":8989.0,\"pscid\":1,\"selected\":0,\"sellerid\":4,\"subhead\":\"每個中秋都不能簡單,無論身在何處,你總需要一塊餅讓生活更圓滿,京東月餅讓愛更圓滿京東自營,閃電配送,更多驚喜,快用手指戳一下\",\"title\":\"北京稻香村 稻香村中秋節月餅 老北京月餅禮盒655g\"}],\"sellerName\":\"商家4\",\"sellerid\":\"4\"},{\"list\":[{\"bargainPrice\":111.99,\"createtime\":\"2017-10-03T23:43:53\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t9004\\/210\\/1160833155\\/647627\\/ad6be059\\/59b4f4e1N9a2b1532.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7504\\/338\\/63721388\\/491286\\/f5957f53\\/598e95f1N7f2adb87.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7441\\/10\\/64242474\\/419246\\/adb30a7d\\/598e95fbNd989ba0a.jpg!q70.jpg\",\"num\":1,\"pid\":13,\"price\":465.0,\"pscid\":1,\"selected\":0,\"sellerid\":6,\"subhead\":\"每個中秋都不能簡單,無論身在何處,你總需要一塊餅讓生活更圓滿,京東月餅讓愛更圓滿京東自營,閃電配送,更多驚喜,快用手指戳一下\",\"title\":\"北京稻香村 稻香村中秋節月餅 老北京月餅禮盒655g\"}],\"sellerName\":\"商家6\",\"sellerid\":\"6\"},{\"list\":[{\"bargainPrice\":11800.0,\"createtime\":\"2017-10-14T21:38:26\",\"detailUrl\":\"https:\\/\\/mitem.jd.hk\\/ware\\/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t6130\\/97\\/1370670410\\/180682\\/1109582a\\/593276b1Nd81fe723.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5698\\/110\\/2617517836\\/202970\\/c9388feb\\/593276b7Nbd94ef1f.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5698\\/110\\/2617517836\\/202970\\/c9388feb\\/593276b7Nbd94ef1f.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5815\\/178\\/2614671118\\/51656\\/7f52d137\\/593276c7N107b725a.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5878\\/60\\/2557817477\\/30873\\/4502b606\\/593276caN5a7d6357.jpg!q70.jpg\",\"num\":3,\"pid\":63,\"price\":10000.0,\"pscid\":40,\"selected\":0,\"sellerid\":7,\"subhead\":\"購買電腦辦公部分商品滿1元返火車票5元優惠券(返完即止)\",\"title\":\"全球購 新款Apple MacBook Pro 蘋果膝上型電腦 銀色VP2新13英寸Bar i5\\/8G\\/256G\"}],\"sellerName\":\"商家7\",\"sellerid\":\"7\"},{\"list\":[{\"bargainPrice\":399.0,\"createtime\":\"2017-10-03T23:53:28\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/1439822107.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5887\\/201\\/859509257\\/69994\\/6bde9bf6\\/59224c24Ne854e14c.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5641\\/233\\/853609022\\/57374\\/5c73d281\\/59224c24N3324d5f4.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5641\\/233\\/853609022\\/57374\\/5c73d281\\/59224c24N3324d5f4.jpg!q70.jpg\",\"num\":1,\"pid\":87,\"price\":888.0,\"pscid\":85,\"selected\":0,\"sellerid\":8,\"subhead\":\"滿2件,總價打6.50折\",\"title\":\"Gap男裝 休閒舒適簡約水洗五袋直筒長褲緊身牛仔褲941825 深灰色 33\\/32(175\\/84A)\"},{\"bargainPrice\":3455.0,\"createtime\":\"2017-10-03T23:53:28\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/12224420750.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t9106\\/106\\/1785172479\\/537280\\/253bc0ab\\/59bf78a7N057e5ff7.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t9106\\/106\\/1785172479\\/537280\\/253bc0ab\\/59bf78a7N057e5ff7.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t8461\\/5\\/1492479653\\/68388\\/7255e013\\/59ba5e84N91091843.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t8461\\/5\\/1492479653\\/68388\\/7255e013\\/59ba5e84N91091843.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t8803\\/356\\/1478945529\\/489755\\/2a163ace\\/59ba5e84N7bb9a666.jpg!q70.jpg\",\"num\":2,\"pid\":52,\"price\":666.0,\"pscid\":39,\"selected\":0,\"sellerid\":8,\"subhead\":\"【現貨新品搶購】全面屏2.0震撼來襲,驍龍835處理器,四曲面陶瓷機\",\"title\":\"小米(MI) 小米MIX2 手機 黑色 全網通 (6GB+64GB)【標配版】\"}],\"sellerName\":\"商家8\",\"sellerid\":\"8\"},{\"list\":[{\"bargainPrice\":11800.0,\"createtime\":\"2017-10-14T21:48:08\",\"detailUrl\":\"https:\\/\\/mitem.jd.hk\\/ware\\/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t6130\\/97\\/1370670410\\/180682\\/1109582a\\/593276b1Nd81fe723.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5698\\/110\\/2617517836\\/202970\\/c9388feb\\/593276b7Nbd94ef1f.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5698\\/110\\/2617517836\\/202970\\/c9388feb\\/593276b7Nbd94ef1f.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5815\\/178\\/2614671118\\/51656\\/7f52d137\\/593276c7N107b725a.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5878\\/60\\/2557817477\\/30873\\/4502b606\\/593276caN5a7d6357.jpg!q70.jpg\",\"num\":3,\"pid\":65,\"price\":12000.0,\"pscid\":40,\"selected\":0,\"sellerid\":9,\"subhead\":\"購買電腦辦公部分商品滿1元返火車票5元優惠券(返完即止)\",\"title\":\"全球購 新款Apple MacBook Pro 蘋果膝上型電腦 銀色VP2新13英寸Bar i5\\/8G\\/256G\"},{\"bargainPrice\":2999.0,\"createtime\":\"2017-10-14T21:48:08\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/2385655.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t2068\\/298\\/2448145915\\/157953\\/7be197df\\/56d51a42Nd86f1c8e.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t2437\\/128\\/1687178395\\/117431\\/bcc190c1\\/56d3fcbaNb2963d21.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t2467\\/222\\/2263160610\\/95597\\/927b8a2f\\/56d3eafeNdecebeb6.jpg!q70.jpg\",\"num\":2,\"pid\":53,\"price\":777.0,\"pscid\":39,\"selected\":0,\"sellerid\":9,\"subhead\":\"Super AMOLED三星雙曲面2K 屏,支援無線充電!\",\"title\":\"三星 Galaxy S7 edge(G9350)4GB+32GB 鉑光金 移動聯通電信4G手機 雙卡雙待\"},{\"bargainPrice\":111.99,\"createtime\":\"2017-10-14T21:39:05\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t9004\\/210\\/1160833155\\/647627\\/ad6be059\\/59b4f4e1N9a2b1532.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7504\\/338\\/63721388\\/491286\\/f5957f53\\/598e95f1N7f2adb87.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7441\\/10\\/64242474\\/419246\\/adb30a7d\\/598e95fbNd989ba0a.jpg!q70.jpg\",\"num\":1,\"pid\":16,\"price\":199.0,\"pscid\":1,\"selected\":0,\"sellerid\":9,\"subhead\":\"每個中秋都不能簡單,無論身在何處,你總需要一塊餅讓生活更圓滿,京東月餅讓愛更圓滿京東自營,閃電配送,更多驚喜,快用手指戳一下\",\"title\":\"北京稻香村 稻香村中秋節月餅 老北京月餅禮盒655g\"}],\"sellerName\":\"商家9\",\"sellerid\":\"9\"},{\"list\":[{\"bargainPrice\":11800.0,\"createtime\":\"2017-10-14T21:38:26\",\"detailUrl\":\"https:\\/\\/mitem.jd.hk\\/ware\\/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t6130\\/97\\/1370670410\\/180682\\/1109582a\\/593276b1Nd81fe723.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5698\\/110\\/2617517836\\/202970\\/c9388feb\\/593276b7Nbd94ef1f.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5698\\/110\\/2617517836\\/202970\\/c9388feb\\/593276b7Nbd94ef1f.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5815\\/178\\/2614671118\\/51656\\/7f52d137\\/593276c7N107b725a.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5878\\/60\\/2557817477\\/30873\\/4502b606\\/593276caN5a7d6357.jpg!q70.jpg\",\"num\":3,\"pid\":66,\"price\":13000.0,\"pscid\":40,\"selected\":0,\"sellerid\":10,\"subhead\":\"購買電腦辦公部分商品滿1元返火車票5元優惠券(返完即止)\",\"title\":\"全球購 新款Apple MacBook Pro 蘋果膝上型電腦 銀色VP2新13英寸Bar i5\\/8G\\/256G\"},{\"bargainPrice\":159.0,\"createtime\":\"2017-10-14T21:49:15\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/5061723.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t8716\\/197\\/1271594444\\/173291\\/2f40bb4f\\/59b743bcN8509428e.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t8347\\/264\\/1286771527\\/92188\\/5cf5ec04\\/59b7420fN65378e9e.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7363\\/165\\/3000956253\\/190883\\/179a372\\/59b743bfNd0c79d93.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7399\\/112\\/2935531768\\/183594\\/b77c7d4a\\/59b7441aNc3d40133.jpg!q70.jpg\",\"num\":5,\"pid\":99,\"price\":2100.0,\"pscid\":112,\"selected\":0,\"sellerid\":10,\"subhead\":\"針織針織閃閃閃亮你的眼\",\"title\":\"維邇旎 2017秋冬新款長袖針織連衣裙韓版氣質中長款名媛包臀A字裙 zx179709 黑色 XL\"},{\"bargainPrice\":111.99,\"createtime\":\"2017-10-03T23:53:28\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t9004\\/210\\/1160833155\\/647627\\/ad6be059\\/59b4f4e1N9a2b1532.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7504\\/338\\/63721388\\/491286\\/f5957f53\\/598e95f1N7f2adb87.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7441\\/10\\/64242474\\/419246\\/adb30a7d\\/598e95fbNd989ba0a.jpg!q70.jpg\",\"num\":2,\"pid\":17,\"price\":299.0,\"pscid\":1,\"selected\":0,\"sellerid\":10,\"subhead\":\"每個中秋都不能簡單,無論身在何處,你總需要一塊餅讓生活更圓滿,京東月餅讓愛更圓滿京東自營,閃電配送,更多驚喜,快用手指戳一下\",\"title\":\"北京稻香村 稻香村中秋節月餅 老北京月餅禮盒655g\"}],\"sellerName\":\"商家10\",\"sellerid\":\"10\"},{\"list\":[{\"bargainPrice\":159.0,\"createtime\":\"2017-10-14T21:49:15\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/5061723.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t8716\\/197\\/1271594444\\/173291\\/2f40bb4f\\/59b743bcN8509428e.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t8347\\/264\\/1286771527\\/92188\\/5cf5ec04\\/59b7420fN65378e9e.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7363\\/165\\/3000956253\\/190883\\/179a372\\/59b743bfNd0c79d93.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7399\\/112\\/2935531768\\/183594\\/b77c7d4a\\/59b7441aNc3d40133.jpg!q70.jpg\",\"num\":1,\"pid\":100,\"price\":2200.0,\"pscid\":112,\"selected\":0,\"sellerid\":11,\"subhead\":\"針織針織閃閃閃亮你的眼\",\"title\":\"維邇旎 2017秋冬新款長袖針織連衣裙韓版氣質中長款名媛包臀A字裙 zx179709 黑色 XL\"},{\"bargainPrice\":22.9,\"createtime\":\"2017-10-14T21:38:26\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/2542855.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t1930\\/284\\/2865629620\\/390243\\/e3ade9c4\\/56f0a08fNbd3a1235.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t2137\\/336\\/2802996626\\/155915\\/e5e90d7a\\/56f0a09cN33e01bd0.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t1882\\/31\\/2772215910\\/389956\\/c8dbf370\\/56f0a0a2Na0c86ea6.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t2620\\/166\\/2703833710\\/312660\\/531aa913\\/57709035N33857877.jpg!q70.jpg\",\"num\":5,\"pid\":34,\"price\":9.0,\"pscid\":2,\"selected\":0,\"sellerid\":11,\"subhead\":\"三隻松鼠零食特惠,專區滿99減50,滿199減100,火速搶購》\",\"title\":\"三隻松鼠 堅果炒貨 零食奶油味 碧根果225g\\/袋\"},{\"bargainPrice\":111.99,\"createtime\":\"2017-10-14T21:48:08\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t9004\\/210\\/1160833155\\/647627\\/ad6be059\\/59b4f4e1N9a2b1532.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7504\\/338\\/63721388\\/491286\\/f5957f53\\/598e95f1N7f2adb87.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7441\\/10\\/64242474\\/419246\\/adb30a7d\\/598e95fbNd989ba0a.jpg!q70.jpg\",\"num\":2,\"pid\":18,\"price\":399.0,\"pscid\":1,\"selected\":0,\"sellerid\":11,\"subhead\":\"每個中秋都不能簡單,無論身在何處,你總需要一塊餅讓生活更圓滿,京東月餅讓愛更圓滿京東自營,閃電配送,更多驚喜,快用手指戳一下\",\"title\":\"北京稻香村 稻香村中秋節月餅 老北京月餅禮盒655g\"}],\"sellerName\":\"商家11\",\"sellerid\":\"11\"},{\"list\":[{\"bargainPrice\":111.99,\"createtime\":\"2017-10-14T21:39:05\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t9004\\/210\\/1160833155\\/647627\\/ad6be059\\/59b4f4e1N9a2b1532.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7504\\/338\\/63721388\\/491286\\/f5957f53\\/598e95f1N7f2adb87.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7441\\/10\\/64242474\\/419246\\/adb30a7d\\/598e95fbNd989ba0a.jpg!q70.jpg\",\"num\":3,\"pid\":19,\"price\":499.0,\"pscid\":1,\"selected\":0,\"sellerid\":12,\"subhead\":\"每個中秋都不能簡單,無論身在何處,你總需要一塊餅讓生活更圓滿,京東月餅讓愛更圓滿京東自營,閃電配送,更多驚喜,快用手指戳一下\",\"title\":\"北京稻香村 稻香村中秋節月餅 老北京月餅禮盒655g\"}],\"sellerName\":\"商家12\",\"sellerid\":\"12\"},{\"list\":[{\"bargainPrice\":111.99,\"createtime\":\"2017-10-14T21:39:05\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t9004\\/210\\/1160833155\\/647627\\/ad6be059\\/59b4f4e1N9a2b1532.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7504\\/338\\/63721388\\/491286\\/f5957f53\\/598e95f1N7f2adb87.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7441\\/10\\/64242474\\/419246\\/adb30a7d\\/598e95fbNd989ba0a.jpg!q70.jpg\",\"num\":3,\"pid\":1,\"price\":118.0,\"pscid\":1,\"selected\":0,\"sellerid\":17,\"subhead\":\"每個中秋都不能簡單,無論身在何處,你總需要一塊餅讓生活更圓滿,京東月餅讓愛更圓滿京東自營,閃電配送,更多驚喜,快用手指戳一下\",\"title\":\"北京稻香村 稻香村中秋節月餅 老北京月餅禮盒655g\"}],\"sellerName\":\"商家17\",\"sellerid\":\"17\"}]}";
try {
Gson gson = new Gson();
Type type = new TypeToken<Result<List<Shop>>>() {
}.getType();
Result result = gson.fromJson(resultString, type);
// Result<List<Goods>> result = new Result<>();
// result.setCode(0);
// List<Goods> list = new ArrayList<>();
// for (int i = 0; i < 30; i++) {
// Goods goods = new Goods();
// goods.setImages("");
// goods.setTitle("手機"+i);
// list.add(goods);
// }
// result.setData(list);
return result;
} catch (Exception e) {
}
Result result = new Result();
result.setCode(-1);
result.setMsg("資料解析異常");
return result;
}
}
presenter
public class CartPresenter extends BasePresenter {
public CartPresenter(DataCall dataCall) {
super(dataCall);
}
@Override
protected Result getData(Object... args) {
Result result = CartModel.goodsList();//呼叫網路請求獲取資料
return result;
}
}
兩個recycleview 的條目佈局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/left_text"
android:layout_width="100dp"
android:layout_height="50dp"
android:textSize="16sp"
android:gravity="center"
android:textColor="@color/white"
android:text="aa" />
</LinearLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:minHeight="50dp"
android:layout_alignParentLeft="true"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/text"
android:layout_toRightOf="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="aa"
android:padding="10dp"/>
<TextView
android:id="@+id/text_price"
android:layout_toRightOf="@+id/image"
android:layout_below="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="價格"
android:padding="10dp"/>
<com.example.yue3.util.view.AddSubLayout
android:id="@+id/add_sub_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp">
</com.example.yue3.util.view.AddSubLayout>
</RelativeLayout>
加減佈局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/btn_add"
android:layout_width="30dp"
android:layout_height="30dp"
android:focusable="false"
android:textSize="20sp"
android:gravity="center"
android:text="+" />
<TextView
android:id="@+id/text_number"
android:layout_width="60dp"
android:layout_height="30dp"
android:gravity="center"
android:textSize="14sp"
android:text="1000" />
<TextView
android:id="@+id/btn_sub"
android:layout_width="30dp"
android:layout_height="30dp"
android:textSize="20sp"
android:focusable="false"
android:gravity="center"
android:text="-" />
</LinearLayout>
介面卡
兩個recycleview 的介面卡
public class LeftAdapter extends RecyclerView.Adapter<LeftAdapter.MyHolder> {
private List<Shop> mList = new ArrayList<>();
public void addAll(List<Shop> list){
mList.addAll(list);
}
@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate(viewGroup.getContext(), R.layout.recycler_left_item,null);
MyHolder myHolder = new MyHolder(view);
return myHolder;
}
@Override
public void onBindViewHolder(@NonNull final MyHolder myHolder, int i) {
final Shop shop = mList.get(i);
myHolder.text.setText(shop.getSellerName());
myHolder.text.setBackgroundResource(shop.getBackground());
myHolder.text.setTextColor(shop.getTextColor());
myHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int j = 0; j <mList.size() ; j++) {
mList.get(j).setTextColor(0xffffffff);
mList.get(j).setBackground(R.color.grayblack);
}
shop.setBackground(R.color.white);
shop.setTextColor(0xff000000);
notifyDataSetChanged();
onItemClickListenter.onItemClick(shop);//切換右邊的列表
}
});
}
@Override
public int getItemCount() {
return mList.size();
}
public List<Shop> getList() {
return mList;
}
class MyHolder extends RecyclerView.ViewHolder{
TextView text;
public MyHolder(@NonNull View itemView) {
super(itemView);
text = itemView.findViewById(R.id.left_text);
}
}
private OnItemClickListenter onItemClickListenter;
public void setOnItemClickListenter(OnItemClickListenter onItemClickListenter) {
this.onItemClickListenter = onItemClickListenter;
}
public interface OnItemClickListenter{
void onItemClick(Shop shop);
}
}
public class RightAdapter extends RecyclerView.Adapter<RightAdapter.ChildHolder> {
private List<Goods> mList = new ArrayList<>();
public void addAll(List<Goods> list) {
mList.addAll(list);
}
@NonNull
@Override
public ChildHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
View view = View.inflate(viewGroup.getContext(), R.layout.recycler_right_item, null);
ChildHolder myHolder = new ChildHolder(view);
return myHolder;
}
@Override
public void onBindViewHolder(@NonNull ChildHolder childHolder, int position) {
final Goods goods = mList.get(position);
childHolder.text.setText(goods.getTitle());
childHolder.price.setText("單價:" + goods.getPrice());//單價
String imageurl = "https" + goods.getImages().split("https")[1];
Log.i("dt", "imageUrl: " + imageurl);
imageurl = imageurl.substring(0, imageurl.lastIndexOf(".jpg") + ".jpg".length());
Glide.with(DTApplication.getInstance()).load(imageurl).into(childHolder.image);//載入圖片
childHolder.addSub.setCount(goods.getNum());//設定商品數量
childHolder.addSub.setAddSubListener(new AddSubLayout.AddSubListener() {
@Override
public void addSub(int count) {
goods.setNum(count);
onNumListener.onNum();//計算價格
}
});
}
@Override
public int getItemCount() {
return mList.size();
}
public void clearList() {
mList.clear();
}
class ChildHolder extends RecyclerView.ViewHolder {
TextView text;
TextView price;
ImageView image;
AddSubLayout addSub;
public ChildHolder(@NonNull View itemView) {
super(itemView);
text = itemView.findViewById(R.id.text);
price = itemView.findViewById(R.id.text_price);
image = itemView.findViewById(R.id.image);
addSub = itemView.findViewById(R.id.add_sub_layout);
}
}
private OnNumListener onNumListener;
public void setOnNumListener(OnNumListener onNumListener) {
this.onNumListener = onNumListener;
}
public interface OnNumListener{
void onNum();
}
}
頁面實現
public class ShopCartActivity1 extends AppCompatActivity implements
DataCall<List<Shop>> {
private TextView mSumPrice;
private TextView mCount;
private RecyclerView mLeftRecycler,mRightRecycler;
private LeftAdapter mLeftAdapter;
private RightAdapter mRightAdapter;
private CartPresenter cartPresenter = new CartPresenter(this);
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
mSumPrice = findViewById(R.id.goods_sum_price);
mCount = findViewById(R.id.goods_number);
mLeftRecycler = findViewById(R.id.left_recycler);
mRightRecycler = findViewById(R.id.right_recycler);
mLeftRecycler.setLayoutManager(new LinearLayoutManager(this));
mRightRecycler.setLayoutManager(new LinearLayoutManager(this));
mLeftAdapter = new LeftAdapter();
mLeftAdapter.setOnItemClickListenter(new LeftAdapter.OnItemClickListenter() {
@Override
public void onItemClick(Shop shop) {
mRightAdapter.clearList();//清空資料
mRightAdapter.addAll(shop.getList());
mRightAdapter.notifyDataSetChanged();
}
});
mLeftRecycler.setAdapter(mLeftAdapter);
mRightAdapter = new RightAdapter();
mRightAdapter.setOnNumListener(new RightAdapter.OnNumListener() {
@Override
public void onNum() {
calculatePrice(mLeftAdapter.getList());
}
});
mRightRecycler.setAdapter(mRightAdapter);
cartPresenter.requestData();
}
@Override
public void success(List<Shop> data) {
calculatePrice(data);//計算價格和數量
mLeftAdapter.addAll(data);//左邊的新增型別
//得到預設選中的shop,設定上顏色和背景
Shop shop = data.get(1);
shop.setTextColor(0xff00