1.ManActivity.xml
<?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" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="清单列表" android:textSize="20sp" android:gravity="center" /> <android.support.v7.widget.RecyclerView android:id="@+id/rec" 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" > <CheckBox android:id="@+id/checkall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="全选" /> <TextView android:layout_marginLeft="10dp" android:id="@+id/zong" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:text="总价:" android:textSize="15sp" /> <Button android:id="@+id/jiesuan" android:layout_width="0dp" android:layout_weight="3" android:layout_height="wrap_content" android:text="去结算" /> </LinearLayout> </LinearLayout>
2.item.xml
<?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="wrap_content" android:orientation="horizontal" android:gravity="center_vertical" > <CheckBox android:id="@+id/check" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" /> <ImageView android:id="@+id/img" android:layout_width="80dp" android:layout_height="80dp" android:src="@mipmap/ic_launcher" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/name" android:layout_width="match_parent" android:layout_height="35dp" android:text="商品名" android:layout_marginLeft="10dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:id="@+id/price" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="5" android:text="单价:" android:layout_marginLeft="10dp" /> <com.example.wangsijiagouwu.view.NumView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="5"/> </LinearLayout> </LinearLayout> </LinearLayout>
3.view.xml
<?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="horizontal" > <Button android:id="@+id/jian" android:layout_width="25dp" android:layout_height="wrap_content" android:text="-" android:background="#fff" /> <EditText android:id="@+id/ed_num" android:layout_width="25dp" android:layout_height="wrap_content" android:background="@null" /> <Button android:id="@+id/add" android:layout_width="25dp" android:layout_height="wrap_content" android:background="#fff" android:text="+" /> </LinearLayout>
4.https
public class OkHttps { private static OkHttps instance; private Handler handler=new Handler(Looper.getMainLooper()); private OkHttpClient okHttpClient; public static OkHttps getInstance(){ if(instance==null){ synchronized (OkHttps.class){ if(instance==null){ instance=new OkHttps(); } } } return instance; } //设置拦截器 private OkHttps(){ HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(); loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); okHttpClient = new OkHttpClient.Builder() .addNetworkInterceptor(loggingInterceptor) .connectTimeout(20,TimeUnit.SECONDS) .writeTimeout(20,TimeUnit.SECONDS) .readTimeout(20,TimeUnit.SECONDS) .build(); } //设置doget方法 public void doGet(String url,Callback callback) { Request request = new Request.Builder() .url(url) .get() .build(); Call call = okHttpClient.newCall(request); call.enqueue(callback); } }
5.api
public class Api { public static final String URL="http://172.17.8.100/small/commodity/v1/findCommodityByKeyword?keyword=板鞋&page=1&count=10"; }
6.model
public class Model { //接口回调用于和p层进行连接 public interface OnShowLisenter{ void OnShow(JSONArray result); } private OnShowLisenter onShowLisenter; public void setOnShowLisenter(OnShowLisenter onShowLisenter) { this.onShowLisenter = onShowLisenter; } private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what){ case 0: String json = (String) msg.obj; try { JSONObject jsonObject = new JSONObject(json); JSONArray result = jsonObject.getJSONArray("result"); if(onShowLisenter!=null){ onShowLisenter.OnShow(result); } } catch (JSONException e) { e.printStackTrace(); } break; } } }; public void sendModel(){ OkHttps.getInstance().doGet(Api.URL, new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { String json = response.body().string(); Message message = new Message(); message.what=0; message.obj=json; handler.sendMessage(message); } }); } }
7.presenter
public class Presenter { private Model model; private IView iView; public Presenter(IView iView) { this.iView = iView; model = new Model(); } public void sendPresenter(){ model.sendModel(); model.setOnShowLisenter(new Model.OnShowLisenter() { @Override public void OnShow(JSONArray result) { iView.getView(result); } }); } }
8.adapter
public class ShopAdapter extends RecyclerView.Adapter<ShopAdapter.ViewHolder> { Context context;JSONArray result; private Map<String, Boolean> map = new HashMap<>(); //长按接口回调 与activity进行连接 public interface LongClicklisteren { void LongClick(int position); } LongClicklisteren longClicklisteren; public void setLongClicklisteren(LongClicklisteren longClicklisteren) { this.longClicklisteren = longClicklisteren; } //checkbox点击回调接口 与activity进行连接 public interface onChecklistener { void check(boolean isCheck, String price); } onChecklistener onChecklistener; public void setOnChecklistener(ShopAdapter.onChecklistener onChecklistener) { this.onChecklistener = onChecklistener; } public ShopAdapter(Context context, JSONArray result, onChecklistener onChecklistener) { this.context = context; this.result=result; setCheckData(false); this.onChecklistener = onChecklistener; } @NonNull @Override public ShopAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View view = LayoutInflater.from(context).inflate(R.layout.item, viewGroup, false); final ViewHolder holder = new ViewHolder(view); //长按点击事件 view.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { //获取在布局上的位置 int layoutPosition = holder.getLayoutPosition(); if (longClicklisteren != null) { longClicklisteren.LongClick(layoutPosition); } return false; } }); return holder; } @Override public void onBindViewHolder(@NonNull ShopAdapter.ViewHolder viewHolder, int i) { try { //获取jsonobject对象 JSONObject jsonObject = result.getJSONObject(i); //获取单个值 String masterPic = jsonObject.getString("masterPic"); String commodityName = jsonObject.getString("commodityName"); final String price = jsonObject.getString("price"); final String commodityId = jsonObject.getString("commodityId"); //给他们一一设置值 viewHolder.name.setText(commodityName); viewHolder.price.setText(price); Glide.with(context).load(masterPic).into(viewHolder.img); viewHolder.check.setChecked(map.get(commodityId)); //设置点击事件单选框 viewHolder.check.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //进行点击 boolean checked = ((CheckBox) v).isChecked(); //传入id map.put(commodityId, checked); boolean isChecked = true; for (String key : map.keySet()) { Boolean aBoolean = map.get(key); if (!aBoolean) { isChecked = false; if (onChecklistener != null) { onChecklistener.check(isChecked, price); } return; } } if (isChecked) { if (onChecklistener != null) { onChecklistener.check(isChecked, price); } } } }); } catch (JSONException e) { e.printStackTrace(); } } private void setCheckData(boolean b) { map.clear(); for (int i = 0; i < result.length(); i++) { try { JSONObject jsonObject = result.getJSONObject(i); String commodityId = jsonObject.getString("commodityId"); map.put(commodityId, b); } catch (JSONException e) { e.printStackTrace(); } } } //刷新适配器 public void shuaCheckData(boolean checkFlag) { setCheckData(checkFlag); notifyDataSetChanged(); } @Override public int getItemCount() { return result.length(); } @RequiresApi(api = Build.VERSION_CODES.KITKAT) public void del(int i){ result.remove(i); notifyDataSetChanged(); } public class ViewHolder extends RecyclerView.ViewHolder { private ImageView img; private CheckBox check; private TextView name, price; public ViewHolder(@NonNull View itemView) { super(itemView); check = itemView.findViewById(R.id.check); img = itemView.findViewById(R.id.img); name = itemView.findViewById(R.id.name); price = itemView.findViewById(R.id.price); } } }
9.manactivity
public class MainActivity extends AppCompatActivity implements IView { private CheckBox checkall; private RecyclerView rec; private TextView zong; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取控件 checkall=findViewById(R.id.checkall); rec=findViewById(R.id.rec); zong=findViewById(R.id.zong); Presenter presenter = new Presenter(this); presenter.sendPresenter(); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); linearLayoutManager.setOrientation(OrientationHelper.VERTICAL); rec.setLayoutManager(linearLayoutManager); } @Override public void getView(JSONArray result) { final ShopAdapter adapter = new ShopAdapter(this, result, new ShopAdapter.onChecklistener() { @Override public void check(boolean isCheck, String price) { checkall.setChecked(isCheck); zong.setText(price+""); } }); checkall.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(((CheckBox)v).isChecked()){ adapter.shuaCheckData(true); }else { adapter.shuaCheckData(false); } } }); adapter.setLongClicklisteren(new ShopAdapter.LongClicklisteren() { @Override public void LongClick(int position) { AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this); builder.setTitle("删除"); builder.setMessage("确定删除吗?"); builder.setNegativeButton("取消",null); final int position1 = position; builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @RequiresApi(api = Build.VERSION_CODES.KITKAT) @Override public void onClick(DialogInterface dialog, int which) { adapter.del(position1); } }); builder.show(); } }); rec.setAdapter(adapter); } }