首先先导入我们要使用的依赖
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\"/>
//谨记如果配置application的话要在清单文件中配置
现在开始写布局文件
首先的布局肯定是我们的主页面的布局
<LinearLayout ns:android=\"http://schemas.android.com/apk/res/android\"
ns:app=\"http://schemas.android.com/apk/res-auto\"
ns:tools=\"http://schemas.android.com/tools\"
android:layout_width=\"match_parent\"
android:layout_height=\"match_parent\"
android:orientation=\"vertical\"
tools:context=\".MainActivity\">
<LinearLayout
android:orientation=\"horizontal\"
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\">
<ImageView
android:src=\"@drawable/b\"
android:layout_width=\"50dp\"
android:layout_height=\"50dp\" />
<ImageView
android:id=\"@+id/ss\"
android:src=\"@drawable/c\"
android:layout_width=\"50dp\"
android:layout_height=\"50dp\" />
<EditText
android:textFontWeight=\"1\"
android:id=\"@+id/edit\"
android:layout_weight=\"1\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\" />
<ImageView
android:id=\"@+id/qh\"
android:src=\"@drawable/d\"
android:layout_width=\"50dp\"
android:layout_height=\"50dp\" />
</LinearLayout>
<LinearLayout
android:orientation=\"horizontal\"
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\">
<TextView
android:layout_weight=\"1\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:text=\"排序▲\"
android:gravity=\"center\"/>
<TextView
android:layout_weight=\"1\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:text=\"排序▲\"
android:gravity=\"center\"/>
<TextView
android:layout_weight=\"1\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:text=\"排序▲\"
android:gravity=\"center\"/>
<TextView
android:layout_weight=\"1\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:text=\"排序▲\"
android:gravity=\"center\"/>
</LinearLayout>
<LinearLayout
android:orientation=\"vertical\"
android:layout_width=\"match_parent\"
android:layout_height=\"match_parent\">
<ImageView
android:id=\"@+id/shop_car_image\"
android:layout_width=\"50dp\"
android:layout_height=\"50dp\"
android:src=\"@drawable/a\" />
<com.jcodecraeer.xrecyclerview.XRecyclerView
android:id=\"@+id/xrecyclerView\"
android:layout_width=\"match_parent\"
android:layout_height=\"match_parent\"></com.jcodecraeer.xrecyclerview.XRecyclerView>
</LinearLayout>
</LinearLayout>
记得要注意自己的布局购物车的image是要跳入购物车的如果你写成相对布局的话会被XRecyclerView覆盖掉(这个要注意)
写接口的bean类
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;
}
}
泛型的bean类
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;
}
}
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\": \"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下\",
// \" \": \"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒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 ;
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 get () {
return ;
}
public void set (String ) {
this. = ;
}
}
写好 bean类就该写mvp了
我们的core层
首先先写成功和失败的借口
public interface DataCall<T> {
void success(T data);
void fail(Result result);
}
用hander来判断失败成功
public abstract class Presenter {
DataCall dataCall;
public Presenter(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 ...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( ...args);
public void unBindCall(){
this.dataCall = null;
}
}
SharedPreferences的类
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;
}
}
首先mvp框架的
model层(这个要注意的是接口的毛病,如果接口有问题的话是出不来数据的,我们可以直接传入从接口拿到的数据不通过okhttp直接解析数据显示到列表)
首先写列表的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.set (\"手机\"+i);
// list.add(goods);
// }
// result.setData(list);
return result;
} catch (Exception e) {
}
Result result = new Result();
result.setCode(-1);
result.setMsg(\"数据解析异常\");
return result;
}
}
presenter层
首先写列表的presenter层
public class GoodsListPresenter extends Presenter {
private int page=1;
private boolean isRefresh=true;
public GoodsListPresenter(DataCall dataCall) {
super(dataCall);
}
@Override
protected Result getData( ... 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;
}
}
忘记一个非常重要的东西
okhttp的封装类
我们model中用到的HttpUtils就是okhttp 的封装类
public class HttpUtils {
public static String get(String urlString){
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(new LoggingInterceptor())//日志拦截器
.connectTimeout(10, TimeUnit.SECONDS)//连接超时
.readTimeout(10,TimeUnit.SECONDS)//读取超时
.writeTimeout(10,TimeUnit.SECONDS)//写入超时
.build();
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.Builder()
.addInterceptor(new LoggingInterceptor())//日志拦截器
.connectTimeout(10, TimeUnit.SECONDS)//连接超时
.readTimeout(10,TimeUnit.SECONDS)//读取超时
.writeTimeout(10,TimeUnit.SECONDS)//写入超时
.build();
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.Builder()
.addInterceptor(new LoggingInterceptor())//日志拦截器
.connectTimeout(10, TimeUnit.SECONDS)//连接超时
.readTimeout(10,TimeUnit.SECONDS)//读取超时
.writeTimeout(10,TimeUnit.SECONDS)//写入超时
.build();
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 \"\";
}
}
接着我们的思路
完成presenter我们的下一步就是
主页面的效果实现
public class MainActivity extends AppCompatActivity implements XRecyclerView.LoadingListener,
DataCall<List<Goods>>, View. Listener,GoodsListAdapter.OnItemClickListener,
GoodsListAdapter.OnItemLongClickListener {
private XRecyclerView mRecyclerView;
private GoodsListAdapter mAdapter;
private LinearLayoutManager mLinearManager;
private GridLayoutManager mGridManager;
private static final int GRID_LAYOUT_MANAGER = 1;
private static final int LINEAR_LAYOUT_MANAGER = 2;
private ImageView mBtnLayout;
private EditText mKeywordsEdit;
//新建商品列表Presenter
private GoodsListPresenter mPresenter = new GoodsListPresenter(this);
private ImageView shop;
/**
* 在onCreate方法里面查找Recylerview,并且设置上适配器。
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_FINE_LOCATION)!=PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
android.Manifest.permission.READ_PHONE_STATE, android.Manifest.permission.ACCESS_FINE_LOCATION,
android.Manifest.permission.ACCESS_COARSE_LOCATION}, 100);
}
mKeywordsEdit = findViewById(R.id.edit);
mBtnLayout = findViewById(R.id.qh);
findViewById(R.id.ss).set Listener(this);
findViewById(R.id.shop_car_image).set Listener(this);
mBtnLayout.set Listener(this);
mRecyclerView = findViewById(R.id.xrecyclerView);//查找mRecyclerView
mRecyclerView.setLoadingListener(this);//添加下拉和刷新的监听器
// StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
mGridManager = new GridLayoutManager(this, 2,
GridLayoutManager.VERTICAL, false);//网格布局
mLinearManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);//线性布局
mRecyclerView.setLayoutManager(mLinearManager);
mAdapter = new GoodsListAdapter(this);//新建适配器
mAdapter.setOnItemClickListener(this);
mAdapter.setOnItemLongClickListener(this);
mRecyclerView.setAdapter(mAdapter);
}
@Override
public void (View v) {
switch (v.getId()){
case R.id.ss:
mRecyclerView.refresh();
break;
case R.id.qh:
if (mRecyclerView.getLayoutManager().equals(mLinearManager)) {
// if (mAdapter.getItemViewType(0) == GoodsListAdapter.LINEAR_TYPE) {
isGrid = true;
mAdapter.setViewType(GoodsListAdapter.GRID_TYPE);
mRecyclerView.setLayoutManager(mGridManager);
} else {
isGrid = false;
mAdapter.setViewType(GoodsListAdapter.LINEAR_TYPE);
mRecyclerView.setLayoutManager(mLinearManager);
}
mAdapter.notifyDataSetChanged();
break;
case R.id.shop_car_image:
Intent intent=new Intent(this,Cart2Activity.class);
startActivity(intent);
break;
}
// if (v.getId() == R.id.ss) {//搜索
// mRecyclerView.refresh();
// //调用列表刷新,刷新方法中获取输入框的值进行自动请求;
// } else if (v.getId() == R.id.qh) {//切换布局
//
//// if (!isGrid) {
// if (mRecyclerView.getLayoutManager().equals(mLinearManager)) {
//// if (mAdapter.getItemViewType(0) == GoodsListAdapter.LINEAR_TYPE) {
// isGrid = true;
// mAdapter.setViewType(GoodsListAdapter.GRID_TYPE);
// mRecyclerView.setLayoutManager(mGridManager);
// } else {
// isGrid = false;
// mAdapter.setViewType(GoodsListAdapter.LINEAR_TYPE);
// mRecyclerView.setLayoutManager(mLinearManager);
// }
// mAdapter.notifyDataSetChanged();
// }else if (v.getId() == R.id.shop){//进入购物车
//// Intent intent = new Intent(this,ShopCartActivity1.class);
// Intent intent=new Intent(this,Cart2Activity.class);
// startActivity(intent);
// }
}
@Override
public void onItemClick(Goods goods) {
Intent intent = new Intent(this,WebActivity.class);
intent.putExtra(\"url\",goods.getDetailUrl());
startActivity(intent);
}
@Override
public void onItemLongClick(int position) {
mAdapter.remove(position);
mAdapter.notifyItemRemoved(position+1);//xRecyclerView如果增加了header,childView的序号就需要增加headerview的数量
if (position < mAdapter.getItemCount()+1) {
mAdapter.notifyItemRangeChanged(position+1,mAdapter.getItemCount()-position);
}
}
@Override
public void success(List<Goods> data) {
mRecyclerView.refreshComplete();//结束刷新
mRecyclerView.loadMoreComplete();//结束加载更多
if (mPresenter.isResresh()) {//只有刷新需要清空数据
mAdapter.clearList();
}
mAdapter.addAll(data);
mAdapter.notifyDataSetChanged();
}
@Override
public void fail(Result result) {
mRecyclerView.refreshComplete();//刷新完成,隐藏刷新view
mRecyclerView.loadMoreComplete();//加载完成,隐藏加载view
Toast.makeText(this, result.getCode() + \" \" + result.getMsg(),
Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
super.onDestroy();
mPresenter.unBindCall();//释放引用,防止内存溢出
}
private boolean isGrid = false;
@Override
public void onRefresh() {
String keywords = mKeywordsEdit.getText().toString();
mPresenter.requestData(true, keywords);
}
@Override
public void More() {
String keywords = mKeywordsEdit.getText().toString();
mPresenter.requestData(false, keywords);
}
}
首先先写列表的adapter适配器
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.set Listener(new View. Listener() {
@Override
public void (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*************
// Animator Animator = Animator.ofFloat(
// goodsHodler.itemView,\"alpha\",1,0
// );
// Animator.setDuration(1000);
// Animator.setInterpolator(new LinearInterpolator());
// Animator.addListener(new Animator.AnimatorListener() {
// @Override
// public void onAnimati (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) {
//
// }
// });
// Animator.start();
//***********方案二**************
if (onItemLongClickListener!=null) {
onItemLongClickListener.onItemLongClick(position);
}
return true;
}
});
goodsHodler.text.setText(goods.get ());
//由于我们的数据图片提供的不标准,所以我们需要切割得到图片
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;
}
}
我们要实现切换视图的效果
所以我们要写两个item布局
一个网格一个纵向
先是网格
<LinearLayout
ns: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
ns: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>
接口回调点击事件
我们所要跳转的两个页面
其中一个就是点击条目跳转
跳转到用户详情页
实现轮播图功能和加入购物车点击吐司
用户详情页布局
一个是网页webview一个是banner轮播图
<LinearLayout ns: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>
<LinearLayout ns:android=\"http://schemas.android.com/apk/res/android\"
android:orientation=\"vertical\" android:layout_width=\"match_parent\"
android:layout_height=\"match_parent\">
<WebView
android:id=\"@+id/webview\"
android:layout_width=\"match_parent\"
android:layout_height=\"match_parent\" />
</LinearLayout>
详情页功能代码实现
public class GoodsDetailActivity extends AppCompatActivity implements View. Listener {
private Goods mGoods;//商品详情
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_goods_detail);
Bundle bundle = getIntent().getExtras();
mGoods = (Goods) bundle.getSerializable(\"goods\");//读取列表传递过来的商品数据
initBanner();
findViewById(R.id.goods_add_cart_btn).set Listener(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];
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样式有显示 时)
// banner.setBanner s( List);
//设置banner动画效果
banner.setBannerAnimation(Transformer.DepthPage);
//设置自动轮播,默认为true
banner.isAutoPlay(false);
//设置轮播时间
// banner.setDelayTime(1500);
//设置指示器位置(当banner模式中有指示器时)
banner.setIndicatorGravity(BannerConfig.RIGHT);
//banner设置方法全部调用完毕时最后调用
banner.start();
}
@Override
public void (View v) {
if (v.getId()==R.id.goods_add_cart_btn){
Toast.makeText(this,\"加入购物车\",Toast.LENGTH_LONG).show();
}
}
}
public class WebActivity extends AppCompatActivity {
WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
mWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = mWebView.getSettings();
// 设置与Js交互的权限
webSettings.set Enabled(true);
// 设置允许JS弹窗
webSettings.set CanOpenWindowsAutomatically(true);
String url = getIntent().getStringExtra(\"url\");
//如果不设置WebViewClient,请求会跳转系统浏览器
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//该方法在Build.VERSION_CODES.LOLLIPOP以前有效,从Build.VERSION_CODES.LOLLIPOP起,建议使用shouldOverrideUrlLoading(WebView, WebResourceRequest)} instead
//返回false,意味着请求过程里,不管有多少次的跳转请求(即新的请求地址),均交给webView自己处理,这也是此方法的默认处理
//返回true,说明你自己想根据url,做新的跳转,比如在判断url符合条件的情况下
return false;
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
//返回false,意味着请求过程里,不管有多少次的跳转请求(即新的请求地址),均交给webView自己处理,这也是此方法的默认处理
//返回true,说明你自己想根据url,做新的跳转,比如在判断url符合条件的情况下
return false;
}
});
// 先载入JS代码
// 格式规定为:file:///android_asset/文件名.html
mWebView.loadUrl(url);
// webview只是载体,内容的渲染需要使用webviewChromClient类去实现
// 通过设置WebChromeClient对象处理 的对话框
//设置响应js 的Alert()函数
mWebView.setWebChromeClient(new WebChromeClient());
}
}
第二个跳转就是跳转页面到
购物车页面布局
<LinearLayout ns:android=\"http://schemas.android.com/apk/res/android\"
android:layout_width=\"match_parent\"
android:layout_height=\"match_parent\"
android:orientation=\"vertical\">
<ExpandableListView
android:id=\"@+id/list_cart\"
android:layout_width=\"match_parent\"
android:layout_height=\"match_parent\"
android:layout_weight=\"1\">
</ExpandableListView>
<RelativeLayout
android:layout_width=\"match_parent\"
android:layout_height=\"50dp\">
<CheckBox
android:id=\"@+id/check_all\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:layout_centerVertical=\"true\"
android:layout_marginLeft=\"10dp\"
android:text=\"全选\" />
<TextView
android:id=\"@+id/goods_sum_price\"
android:layout_toRightOf=\"@+id/check_all\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:text=\"价格:\"
android:layout_marginLeft=\"20dp\"
android:layout_centerVertical=\"true\"/>
</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\\\":\\\"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下\\\",\\\" \\\":\\\"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒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\\\":\\\"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下\\\",\\\" \\\":\\\"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒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元优惠券(返完即止)\\\",\\\" \\\":\\\"全球购 新款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折\\\",\\\" \\\":\\\"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处理器,四曲面陶瓷机\\\",\\\" \\\":\\\"小米(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元优惠券(返完即止)\\\",\\\" \\\":\\\"全球购 新款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 屏,支持无线充电!\\\",\\\" \\\":\\\"三星 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\\\":\\\"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下\\\",\\\" \\\":\\\"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒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元优惠券(返完即止)\\\",\\\" \\\":\\\"全球购 新款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\\\":\\\"针织针织闪闪闪亮你的眼\\\",\\\" \\\":\\\"维迩旎 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\\\":\\\"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下\\\",\\\" \\\":\\\"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒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\\\":\\\"针织针织闪闪闪亮你的眼\\\",\\\" \\\":\\\"维迩旎 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,火速抢购》\\\",\\\" \\\":\\\"三只松鼠 坚果炒货 零食奶油味 碧根果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\\\":\\\"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下\\\",\\\" \\\":\\\"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒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\\\":\\\"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下\\\",\\\" \\\":\\\"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒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\\\":\\\"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下\\\",\\\" \\\":\\\"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒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.set (\"手机\"+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 Presenter {
public CartPresenter(DataCall dataCall) {
super(dataCall);
}
@Override
protected Result getData( ... args) {
Result result = CartModel.goodsList();//调用网络请求获取数据
return result;
}
}
方法实现
public class Cart2Activity extends AppCompatActivity implements
DataCall<List<Shop>>,CartAdapter2.TotalPriceListener{
private TextView mSumPrice;
private CheckBox mCheckAll;
private ExpandableListView mGoodsList;
private CartAdapter2 mCartAdapter;
private CartPresenter cartPresenter = new CartPresenter(this);
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart2);
mSumPrice = findViewById(R.id.goods_sum_price);
mCheckAll = findViewById(R.id.check_all);
mCheckAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mCartAdapter.checkAll(isChecked);
}
});
mGoodsList = findViewById(R.id.list_cart);
mCartAdapter = new CartAdapter2();
mGoodsList.setAdapter(mCartAdapter);
mCartAdapter.setTotalPriceListener(this);//设置总价回调器
mGoodsList.setGroupIndicator(null);
//让其group不能被点击
mGoodsList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
return true;
}
});
cartPresenter.requestData();
}
@Override
public void success(List<Shop> data) {
mCartAdapter.addAll(data);
//遍历所有group,将所有项设置成默认展开
int groupCount = data.size();
for (int i = 0; i < groupCount; i++) {
mGoodsList.expandGroup(i);
}
mCartAdapter.notifyDataSetChanged();
}
@Override
public void fail(Result result) {
Toast.makeText(this, result.getCode() + \" \" + result.getMsg(), Toast.LENGTH_LONG).show();
}
@Override
public void totalPrice(double totalPrice) {
mSumPrice.setText(String.valueOf(totalPrice));
}
}
购物车数据的适配器
public class CartAdapter2 extends ExpandableListAdapter {
private List<Shop> mList = new ArrayList<>();
private TotalPriceListener totalPriceListener;
public CartAdapter2(){
}
public void setTotalPriceListener(TotalPriceListener totalPriceListener) {
this.totalPriceListener = totalPriceListener;
}
@Override
public int getGroupCount() {
return mList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return mList.get(groupPosition).getList().size();
}
@Override
public getGroup(int groupPosition) {
return mList.get(groupPosition);
}
@Override
public getChild(int groupPosition, int childPosition) {
return mList.get(groupPosition).getList().get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
GroupHodler holder;
if (convertView == null) {
convertVi 继续阅读与本文标签相同的文章
-
微信小程序兼容性问题。
2026-05-18栏目: 教程
-
flex的兼容性处理问题
2026-05-18栏目: 教程
-
Mac高效开发之iTerm2、Prezto和Solarized主题
2026-05-18栏目: 教程
-
将阿里云产品整合成为高校课程实训的训练营产品的实践(四)
2026-05-18栏目: 教程
-
中间人攻击,HTTPS也可以被碾压
2026-05-18栏目: 教程
