M层
M层接口
package com.example.shoppings.model;
import com.example.shoppings.callback.ICallBock;
public interface IModel {
public void getModel(ICallBock callBock, String url);
}
M层实现类
package com.example.shoppings.model;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import com.example.shoppings.beans.MyBean;
import com.example.shoppings.callback.ICallBock;
import com.example.shoppings.utils.OkHttps;
import com.google.gson.Gson;
import java.util.List;
public class Model implements IModel{
ICallBock iCallBock;
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
String json= (String) msg.obj;
Log.e("Model", "json"+json);
Gson gson=new Gson();
MyBean myBean = gson.fromJson(json, MyBean.class);
List<MyBean.DataBean> list = myBean.getData();
Log.e("Model", "list:" + list);
iCallBock.iCallBack(list);
}
};
@Override
public void getModel(ICallBock callBock ,String url) {
this.iCallBock=callBock;
OkHttps.getInstance().doget(handler,url);
}
}
P层
P层接口
package com.example.shoppings.presenter;
public interface IPresenter {
public void toModel();
}
P层实现类
package com.example.shoppings.presenter;
import android.util.Log;
import com.example.shoppings.beans.MyBean;
import com.example.shoppings.callback.ICallBock;
import com.example.shoppings.model.IModel;
import com.example.shoppings.model.Model;
import com.example.shoppings.view.IView;
import java.util.List;
public class Presenter implements IPresenter{
IModel iModel;
IView iView;
public Presenter(IView iView){
this.iView=iView;
iModel=new Model();
}
@Override
public void toModel() {
String url="http://www.wanandroid.com/tools/mockapi/6523/restaurant-list";
iModel.getModel(new ICallBock() {
@Override
public void iCallBack(List<MyBean.DataBean> list) {
Log.e("Presenter", "showview");
iView.showView(list);
}
},url);
}
}
V层
V层接口
package com.example.shoppings.view;
import com.example.shoppings.beans.MyBean;
import java.util.List;
public interface IView {
public void showView(List<MyBean.DataBean> list);
}
MainActivity类
package com.example.shoppings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.TextView;
import com.example.shoppings.beans.MyBean;
import com.example.shoppings.presenter.IPresenter;
import com.example.shoppings.presenter.Presenter;
import com.example.shoppings.utils.RightAdpler;
import com.example.shoppings.view.IView;
import java.util.List;
public class MainActivity extends AppCompatActivity implements IView {
IPresenter iPresenter;
private RecyclerView rectview;
private TextView jiesuantext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rectview = (RecyclerView) findViewById(R.id.rectview);
jiesuantext = (TextView) findViewById(R.id.jiesuantext);
iPresenter=new Presenter(this);
iPresenter.toModel();
}
@Override
public void showView(List<MyBean.DataBean> list) {
Log.e("MainActivity", "list:" + list);
LinearLayoutManager manager=new LinearLayoutManager(MainActivity.this);
rectview.setLayoutManager(manager);
RightAdpler rightAdpler=new RightAdpler(MainActivity.this,list,jiesuantext);
rectview.setAdapter(rightAdpler);
}
}
OkHttp
OkHttp封装类
package com.example.shoppings.utils;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class OkHttps {
static OkHttps https = null;
private OkHttps() {
}
public static OkHttps getInstance() {
if (https == null) {
https = new OkHttps();
} else {
return https;
}
return https;
}
public void doget(final Handler handler,String url) {
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Message message = new Message();
message.obj = response.body().string();
handler.sendMessage(message);
}
});
}
public void dopost(final Handler handler,String url) {
OkHttpClient okHttpClient = new OkHttpClient();
RequestBody body = new FormBody.Builder()
.add("page", "1")
.add("count", "10")
.add("keyword", url)
.build();
Request request = new Request.Builder().url("http://172.17.8.100/small/commodity/v1/findCommodityByKeyword?").post(body).build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Message message = new Message();
message.what=2;
message.obj = response.body().string();
handler.sendMessage(message);
}
});
}
}
父类适配器
父类的适配器
package com.example.shoppings.utils;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.shoppings.R;
import com.example.shoppings.beans.MyBean;
import java.util.List;
public class RightAdpler extends RecyclerView.Adapter<RightAdpler.Handler> {
Context context;
List<MyBean.DataBean> list;
private TextView jiesuantext;
public RightAdpler(Context context, List<MyBean.DataBean> list, TextView jiesuantext){
this.context=context;
this.list=list;
this.jiesuantext=jiesuantext;
}
@NonNull
@Override
public RightAdpler.Handler onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.rightlixtview, null);
Handler handler=new Handler(view);
return handler;
}
@Override
public void onBindViewHolder(@NonNull RightAdpler.Handler handler, final int i) {
handler.textView.setText(list.get(i).getName());
handler.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
list.get(i).setCreated_at(1+"");
notifyDataSetChanged();
}else{
list.get(i).setCreated_at(0+"");
notifyDataSetChanged();
}
}
});
LinearLayoutManager manager=new LinearLayoutManager(context);
handler.rectcview2.setLayoutManager(manager);
ZiLeiAdpler rightAdpler=new ZiLeiAdpler(list,context,i,jiesuantext);
handler.rectcview2.setAdapter(rightAdpler);
}
@Override
public int getItemCount() {
Log.e("Righttt", "list.size():" + list.size());
return list.size();
}
public class Handler extends RecyclerView.ViewHolder {
public TextView textView;
public RecyclerView rectcview2;
public CheckBox checkBox;
public Handler(@NonNull View itemView) {
super(itemView);
textView=itemView.findViewById(R.id.textview1);
rectcview2=itemView.findViewById(R.id.rectcview2);
checkBox=itemView.findViewById(R.id.fucheckbox);
}
}
}
子类适配器
子类的适配器
package com.example.shoppings.utils;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.example.shoppings.R;
import com.example.shoppings.beans.MyBean;
import java.util.List;
public class ZiLeiAdpler extends RecyclerView.Adapter<ZiLeiAdpler.Handlr> {
private Context context;
List<MyBean.DataBean> list;
int a;
private TextView jiesuantex;
int zongjia;
public ZiLeiAdpler(List<MyBean.DataBean> list, Context context, int i, TextView jiesuantext) {
this.list = list;
this.context = context;
this.a = i;
this.jiesuantex = jiesuantext;
}
@NonNull
@Override
public ZiLeiAdpler.Handlr onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.zileiview, null);
Handlr handler = new Handlr(view);
return handler;
}
@Override
public void onBindViewHolder(@NonNull final ZiLeiAdpler.Handlr handlr, int i) {
handlr.textView3.setText(list.get(a).getSpus().get(i).getName());
handlr.textView4.setText(list.get(a).getSpus().get(i).getPraise_num() + "");
Glide.with(context).load(list.get(a).getSpus().get(i).getPic_url()).into(handlr.imageView);
String at = list.get(a).getCreated_at();
final int price = list.get(a).getSpus().get(i).getPraise_num();
handlr.checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int js=0;
int xx=Integer.parseInt(jiesuantex.getText().toString());
if(isChecked){
js++;
int p=Integer.parseInt(handlr.xiaojiage.getText().toString());
xx=xx+p;
}else{
js--;
int p=Integer.parseInt(handlr.xiaojiage.getText().toString());
xx=xx-p;
}
jiesuantex.setText(xx+"");
}
});
handlr.buttonjian.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int shuliang = Integer.parseInt(handlr.zidinyitext.getText().toString());
int xiaojiage = Integer.parseInt(handlr.xiaojiage.getText().toString());
if (shuliang > 0) {
shuliang--;
handlr.zidinyitext.setText(shuliang + "");
handlr.xiaojiage.setText(xiaojiage - price + "");
if (handlr.checkBox1.isChecked()){
int zongjiatextview = Integer.parseInt(jiesuantex.getText().toString());
zongjiatextview=zongjiatextview-price;
jiesuantex.setText(zongjiatextview+"");
}
} else {
Toast.makeText(context, "不能再减了", Toast.LENGTH_SHORT).show();
}
}
});
handlr.buttonjia.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int shuxliang = Integer.parseInt(handlr.zidinyitext.getText().toString());
int xiaojiage = Integer.parseInt(handlr.xiaojiage.getText().toString());
shuxliang++;
handlr.zidinyitext.setText(shuxliang + "");
handlr.xiaojiage.setText(xiaojiage + price + "");
zongjia=zongjia+price;
if (handlr.checkBox1.isChecked()){
int zongjiatextview = Integer.parseInt(jiesuantex.getText().toString());
zongjiatextview=zongjiatextview+price;
jiesuantex.setText(zongjiatextview+"");
}
}
});
if (at.equals("1")) {
handlr.checkBox1.setChecked(true);
} else {
handlr.checkBox1.setChecked(false);
}
}
@Override
public int getItemCount() {
Log.e("ZiLeiAdpler", "list.size():" + list.size());
return list.get(a).getSpus().size();
}
public class Handlr extends RecyclerView.ViewHolder {
private TextView textView3, textView4;
private ImageView imageView;
private CheckBox checkBox1;
private Button buttonjian;
private TextView zidinyitext, xiaojiage;
private Button buttonjia;
public Handlr(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageview2);
textView3 = itemView.findViewById(R.id.zileitextview11);
textView4 = itemView.findViewById(R.id.zileitextview22);
checkBox1 = itemView.findViewById(R.id.zicheckbox);
buttonjian = itemView.findViewById(R.id.buttonjian);
zidinyitext = itemView.findViewById(R.id.zidinyitext);
buttonjia = itemView.findViewById(R.id.buttonjia);
xiaojiage = itemView.findViewById(R.id.xiaojiage);
}
}
}
自定义view类
自定义布局类
package com.example.shoppings.utils;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.shoppings.R;
public class ZiDingYiView extends LinearLayout {
Button buttonjian;
TextView zidinyitext;
Button buttonjia;
public ZiDingYiView(Context context) {
super(context);
}
public ZiDingYiView(Context context, AttributeSet attrs) {
super(context, attrs);
View view = LayoutInflater.from(context).inflate(R.layout.myviews, this);
buttonjian = (Button) view.findViewById(R.id.buttonjian);
zidinyitext = (TextView) view.findViewById(R.id.zidinyitext);
buttonjia = (Button) view.findViewById(R.id.buttonjia);
}
public ZiDingYiView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
CallBack返回接口
package com.example.shoppings.callback;
import com.example.shoppings.beans.MyBean;
import java.util.List;
public interface ICallBock {
public void iCallBack(List<MyBean.DataBean> list);
}
布局文件
MainACtivity布局
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/rectview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9">
</android.support.v7.widget.RecyclerView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/jiesuantext"
android:layout_width="0dp"
android:text="0"
android:textSize="20sp"
android:paddingTop="10dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:id="@+id/jiesuanbutton"
android:layout_width="0dp"
android:text="结算"
android:textSize="20sp"
android:paddingTop="10dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
父类布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/fucheckbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textview1"
android:layout_width="match_parent"
android:layout_height="40dp" /></LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/rectcview2"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</LinearLayout>
子类布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal">
<CheckBox
android:id="@+id/zicheckbox"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="1"/>
<ImageView
android:id="@+id/imageview2"
android:layout_width="100dp"
android:layout_height="100dp"
/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical">
<TextView
android:id="@+id/zileitextview11"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<TextView
android:id="@+id/zileitextview22"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<com.example.shoppings.utils.ZiDingYiView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</com.example.shoppings.utils.ZiDingYiView>
</LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
自定义view布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/xiaojiage"
android:text="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/buttonjian"
android:text="减"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/zidinyitext"
android:text="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/buttonjia"
android:text="加"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
依赖文档
依赖
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.okhttp3:okhttp:3.7.0'
implementation 'com.squareup.okio:okio:1.12.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.hjm:BottomTabBar:1.1.1'
implementation 'com.github.bumptech.glide:glide:4.8.0'