Api
package com.example.xiejiaqi0306er.api;
public class Api {
public static final String Api = "http://www.wanandroid.com/tools/mockapi/6523/restaurant-list";
}
callback
package com.example.xiejiaqi0306er.callback;
public interface GoodsCallBack {
void onSuccess(String result);
void onFilure(String msg);
}
Contract
package com.example.xiejiaqi0306er.contract;
import com.example.xiejiaqi0306er.callback.GoodsCallBack;
import java.util.HashMap;
public interface GoodsContract {
interface IModle{
void Modle(HashMap<String, String> map, GoodsCallBack goodsCallBack);
}
interface IView{
void onSuccess(String result);
void onFilure(String msg);
}
abstract class IPresenter{
public abstract void Presenter(HashMap<String,String> map);
}
}
OkHttpUtils
package com.example.xiejiaqi0306er.utils;
import android.os.Handler;
import com.example.xiejiaqi0306er.bean.Bean;
import com.example.xiejiaqi0306er.callback.GoodsCallBack;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
public class OkHttpUtils {
private static OkHttpUtils okHttpUtils;
private final OkHttpClient okHttpClient;
private Handler handler = new Handler();
private OkHttpUtils(){
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
okHttpClient = new OkHttpClient.Builder()
.addInterceptor(httpLoggingInterceptor)
.writeTimeout(5,TimeUnit.SECONDS)
.readTimeout(5,TimeUnit.SECONDS)
.connectTimeout(5,TimeUnit.SECONDS)
.build();
}
public static OkHttpUtils getInstance(){
if (okHttpUtils == null){
synchronized (Bean.class){
if (okHttpUtils == null){
okHttpUtils = new OkHttpUtils();
}
}
}
return okHttpUtils;
}
public void dopost(String url, HashMap<String,String> map, final GoodsCallBack goodsCallBack){
FormBody.Builder builder = new FormBody.Builder();
for (Map.Entry<String,String> p:map.entrySet()){
builder.add(p.getKey(),p.getValue());
}
RequestBody requestBody = builder.build();
Request request = new Request.Builder()
.url(url)
.post(requestBody)
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
if(goodsCallBack != null){
handler.post(new Runnable() {
@Override
public void run() {
goodsCallBack.onFilure("网络异常");
}
});
}
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String s = response.body().toString();
handler.post(new Runnable() {
@Override
public void run() {
goodsCallBack.onSuccess(s);
}
});
}
});
}
public void goGet(String url, final GoodsCallBack goodsCallBack){
Request build = new Request.Builder()
.url(url)
.get()
.build();
Call call = okHttpClient.newCall(build);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
goodsCallBack.onFilure("网络异常");
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String string = response.body().string();
handler.post(new Runnable() {
@Override
public void run() {
goodsCallBack.onSuccess(string);
}
});
}
});
}
public void finish(){
if(okHttpClient != null){
okHttpClient.dispatcher().cancelAll();
}
}
}
Modle
package com.example.xiejiaqi0306er.modle;
import android.os.Handler;
import com.example.xiejiaqi0306er.api.Api;
import com.example.xiejiaqi0306er.callback.GoodsCallBack;
import com.example.xiejiaqi0306er.contract.GoodsContract;
import com.example.xiejiaqi0306er.utils.OkHttpUtils;
import java.util.HashMap;
public class Modle implements GoodsContract.IModle{
private Handler handler = new Handler();
@Override
public void Modle(HashMap<String, String> map, final GoodsCallBack goodsCallBack) {
OkHttpUtils.getInstance().goGet(Api.Api, new GoodsCallBack() {
@Override
public void onSuccess(final String result) {
if (goodsCallBack!=null){
handler.post(new Runnable() {
@Override
public void run() {
goodsCallBack.onSuccess(result);
}
});
}
}
@Override
public void onFilure(final String msg) {
handler.post(new Runnable() {
@Override
public void run() {
goodsCallBack.onFilure(msg);
}
});
}
});
}
}
Presenter
package com.example.xiejiaqi0306er.presenter;
import com.example.xiejiaqi0306er.callback.GoodsCallBack;
import com.example.xiejiaqi0306er.contract.GoodsContract;
import com.example.xiejiaqi0306er.modle.Modle;
import java.util.HashMap;
public class Presenter extends GoodsContract.IPresenter{
private final Modle modle;
;
private GoodsContract.IView iView;
public Presenter(GoodsContract.IView iView) {
this.iView = iView;
modle = new Modle();
}
@Override
public void Presenter(HashMap<String, String> map) {
modle.Modle(map, new GoodsCallBack() {
@Override
public void onSuccess(String result) {
if (iView != null){
iView.onSuccess(result);
}
}
@Override
public void onFilure(String msg) {
iView.onFilure(msg);
}
});
}
}
MainActivity
package com.example.xiejiaqi0306er;
import android.os.TestLooperManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;
import com.example.xiejiaqi0306er.adapter.Adapter;
import com.example.xiejiaqi0306er.bean.Bean;
import com.example.xiejiaqi0306er.contract.GoodsContract;
import com.example.xiejiaqi0306er.presenter.Presenter;
import com.google.gson.Gson;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity implements GoodsContract.IView{
private RecyclerView rv1;
private Presenter presenter;
private Bean bean;
private Adapter adapter;
private TextView stv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
presenter = new Presenter(this);
rv1.setLayoutManager(new LinearLayoutManager(this));
presenter.Presenter(new HashMap<String, String>());
}
private void initView() {
rv1 = findViewById(R.id.rv1);
stv1 = findViewById(R.id.stv1);
}
@Override
public void onSuccess(String result) {
bean = new Gson().fromJson(result, Bean.class);
adapter = new Adapter(this,stv1);
rv1.setAdapter(adapter);
adapter.setList(bean.getData());
}
@Override
public void onFilure(String msg) {
}
}
父适配器
package com.example.xiejiaqi0306er.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
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.xiejiaqi0306er.R;
import com.example.xiejiaqi0306er.bean.Bean;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder>{
private List<Bean.DataEntity> list;
private Context context;
int a;
private Adapters adapters;
private TextView stv1;
public Adapter(Context context, TextView stv1) {
this.context = context;
list=new ArrayList<>();
this.stv1 = stv1;
}
public void setList(List<Bean.DataEntity> list){
if (list != null){
this.list = list;
}
notifyDataSetChanged();
}
@NonNull
@Override
public Adapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.name1, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull Adapter.ViewHolder viewHolder, final int i) {
a=i;
Bean.DataEntity dataEntity = list.get(i);
viewHolder.tt1.setText(dataEntity.getName());
viewHolder.rv2.setLayoutManager(new LinearLayoutManager(context));
adapters = new Adapters(context, list, i,stv1);
viewHolder.rv2.setAdapter(adapters);
adapters.setList(list);
viewHolder.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();
}
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView tt1;
private RecyclerView rv2;
private CheckBox checkBox;
public ViewHolder(@NonNull View itemView) {
super(itemView);
tt1 = itemView.findViewById(R.id.tt1);
rv2 = itemView.findViewById(R.id.rv2);
checkBox = itemView.findViewById(R.id.scb1);
}
}
}
子适配器
package com.example.xiejiaqi0306er.adapter;
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.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.example.xiejiaqi0306er.R;
import com.example.xiejiaqi0306er.bean.Bean;
import com.example.xiejiaqi0306er.views.Views;
import java.util.ArrayList;
import java.util.List;
public class Adapters extends RecyclerView.Adapter<Adapters.PViewHolder>{
private List<Bean.DataEntity> list;
private Context context;
private int a;
private TextView stv1;
int zongjia;
public Adapters(Context context, List<Bean.DataEntity> list, int i, TextView stv1) {
this.context = context;
list=new ArrayList<>();
a=i;
this.stv1 = stv1;
}
public void setList(List<Bean.DataEntity> list){
if (list != null){
this.list = list;
}
notifyDataSetChanged();
}
@NonNull
@Override
public Adapters.PViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.name2, viewGroup, false);
PViewHolder pViewHolder = new PViewHolder(view);
return pViewHolder;
}
@Override
public void onBindViewHolder(@NonNull final Adapters.PViewHolder pViewHolder, int i) {
List<Bean.DataEntity.SpusEntity> spus = list.get(a).getSpus();
pViewHolder.tv1.setText(spus.get(i).getName());
pViewHolder.tv2.setText(spus.get(i).getPraise_num()+"");
Glide.with(context).load(spus.get(i).getPic_url()).into(pViewHolder.iv1);
String at = list.get(a).getCreated_at();
final int price = spus.get(i).getPraise_num();
pViewHolder.checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int js = 0;
int xx=Integer.parseInt(stv1.getText().toString());
if (isChecked){
js++;
int p = Integer.parseInt(pViewHolder.dtv1.getText().toString());
xx = xx+p;
}else{
js--;
int p = Integer.parseInt(pViewHolder.dtv1.getText().toString());
xx = xx-p;
}
stv1.setText(xx+"");
}
});
pViewHolder.button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int i1 = Integer.parseInt(pViewHolder.et1.getText().toString());
int i2 = Integer.parseInt(pViewHolder.dtv1.getText().toString());
if (i1 > 0){
i1--;
pViewHolder.et1.setText(i1 + "");
pViewHolder.dtv1.setText(i2 - price + "");
if (pViewHolder.checkBox1.isChecked()){
int i3 = Integer.parseInt(stv1.getText().toString());
i3 = i3-price;
stv1.setText(i3 + "");
}
}else{
Toast.makeText(context,"不能在减了",Toast.LENGTH_LONG).show();
}
}
});
pViewHolder.button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int i1 = Integer.parseInt(pViewHolder.et1.getText().toString());
int i2 = Integer.parseInt(pViewHolder.dtv1.getText().toString());
i1++;
pViewHolder.et1.setText(i1 + "");
pViewHolder.dtv1.setText(i2 + price + "");
zongjia = zongjia + price;
if(pViewHolder.checkBox1.isChecked()){
int i3 = Integer.parseInt(stv1.getText().toString());
i3 = i3+price;
stv1.setText(i3 + "");
}
}
});
if(at.equals("1")){
pViewHolder.checkBox1.setChecked(true);
}else{
pViewHolder.checkBox1.setChecked(false);
}
}
@Override
public int getItemCount() {
return list.get(a).getSpus().size();
}
public class PViewHolder extends RecyclerView.ViewHolder {
public ImageView iv1;
public TextView tv1,tv2,dtv1;
private Button button1,button2;
EditText et1;
private CheckBox checkBox1;
public PViewHolder(@NonNull View itemView) {
super(itemView);
iv1 = itemView.findViewById(R.id.iv1);
tv1 = itemView.findViewById(R.id.tv1);
tv2 = itemView.findViewById(R.id.tv2);
checkBox1 = itemView.findViewById(R.id.cb1);
button1 = itemView.findViewById(R.id.ctv1);
et1 = itemView.findViewById(R.id.et1);
button2 = itemView.findViewById(R.id.ctv2);
dtv1 = itemView.findViewById(R.id.dtv1);
}
}
}
自定义View
package com.example.xiejiaqi0306er.views;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.xiejiaqi0306er.R;
public class Views extends LinearLayout{
private TextView ctv1,ctv2;
private EditText et1;
public Views(Context context) {
super(context);
}
public Views(Context context, AttributeSet attrs) {
super(context, attrs);
View view = LayoutInflater.from(context).inflate(R.layout.name3, this);
ctv1 = view.findViewById(R.id.ctv1);
ctv2 = view.findViewById(R.id.ctv2);
et1 = view.findViewById(R.id.et1);
// ctv1.setOnClickListener(new OnClickListener() {
// @Override
// public void onClick(View v) {
// String et1s = et1.getText().toString();
//
// }
// });
// ctv2.setOnClickListener(new OnClickListener() {
// @Override
// public void onClick(View v) {
//
// }
// });
}
public Views(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
/* private onClickItemListener onClickItemListener;
public void setOnClickItemListener(onClickItemListener onClickItemListener) {
this.onClickItemListener = onClickItemListener;
}
public interface onClickItemListener{
}*/
}
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"
android:orientation="vertical"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9.4"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="0.6">
<Button
android:id="@+id/sbt1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="全选"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="25sp"
android:text="总价:"/>
<TextView
android:id="@+id/stv1"
android:text="0"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
/>
<Button
android:id="@+id/sbt2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="去结算"/>
</LinearLayout>
</LinearLayout>
父布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f4f"
android:orientation="horizontal">
<CheckBox
android:id="@+id/scb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tt1"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="1234"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#4f4" />
</LinearLayout>
子布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<CheckBox
android:layout_marginLeft="30dp"
android:id="@+id/cb1"
android:layout_marginTop="25dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/iv1"
android:layout_width="100dp"
android:layout_height="100dp"/>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="100dp"
android:orientation="vertical">
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="123456"/>
<TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="123" />
<com.example.xiejiaqi0306er.views.Views
android:layout_marginLeft="30dp"
android:id="@+id/views"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<Button
android:id="@+id/bt1"
android:layout_marginTop="25dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="加入购物车"/>
</LinearLayout>
自定义View
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/ctv1"
android:background="#4f4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"/>
<EditText
android:id="@+id/et1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"/>
<Button
android:id="@+id/ctv2"
android:background="#4f4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"/>
<TextView
android:id="@+id/dtv1"
android:text="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>