//model层
//展示的model
public class SearchModel {
public void getData(String name, int page, final SModel smodel){
String url="https://www.zhaoapi.cn/product/searchProducts";
HashMap<String,String> map=new HashMap<>();
//传入name值
map.put("keywords",name);
map.put("page",page+"");
OkHttpUtils.getInstance().doGet(url, map, new CallBack() {
@Override
public void onSuccess(Object o) {
smodel.onSuccess(o);
}
@Override
public void onFailure(String msg) {
smodel.onFailure(msg);
}
});
}
}
//传商品id的model层
public class PidModel {
private Context context;
private List<CartBean.DataBean> list = new ArrayList<>();
HashMap<String,String> map=new HashMap<>();
public void getPid(int pid, final SModel sModel){
map.put("pid",pid+"");
OkHttpUtils.getInstance().doGet("https://www.zhaoapi.cn/product/getProductDetail", map, new CallBack() {
@Override
public void onSuccess(Object o) {
CartBean cartBean = GsonUtils.getInstance().fromJson((String) o, CartBean.class);
CartBean.DataBean data = cartBean.getData();
if (data != null) {
list.add(data);
sModel.onPidSuccess(list);
}else {
Toast.makeText(context, "内容为空", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(String msg) {
sModel.onPidFailed(msg);
}
});
}
public void getUid(int pid, final SModel sModel) {
map.put("uid", "1");
map.put("pid", pid + "");
OkHttpUtils.getInstance().doGet("https://www.zhaoapi.cn/product/addCart", map, new CallBack() {
@Override
public void onSuccess(Object o) {
Gson gson = new Gson();
AddCenggong addCenggong = gson.fromJson((String)o, AddCenggong.class);
String msg = addCenggong.getMsg();
sModel.onAddSuccess(msg);
}
@Override
public void onFailure(String msg) {
sModel.onAddFailed(msg);
}
});
}
}
Present层
public class SearchPresent implements SModel {
private final SearchModel searchModel;
private final PidModel pidModel;
private SView sView;
public SearchPresent(SView sView){
this.sView=sView;
searchModel = new SearchModel();
pidModel = new PidModel();
}
public void getData(String name,int page){
searchModel.getData(name, page,this);
}
//加入购物车管过来的id
public void getUid(int pid){
pidModel.getUid(pid,this);
}
public void getPid(int id){
pidModel.getPid(id,this);
}
public void onSuccess(Object o) {
sView.onSuccess(o);
}
@Override
public void onFailure(String msg) {
sView.onFailure(msg);
}
@Override
public void onPidSuccess(List<CartBean.DataBean> list) {
sView.onPidSuccess(list);
}
@Override
public void onPidFailed(String message) {
sView.onPidFailed(message);
}
@Override
public void onAddSuccess(String msg) {
sView.onAddSuccess(msg);
}
@Override
public void onAddFailed(String msg) {
sView.onAddFailed(msg);
}
}
//接口
public interface SModel {
void onSuccess(Object o);
void onFailure(String msg);
void onPidSuccess(List<CartBean.DataBean> list);
void onPidFailed(String message);
void onAddSuccess(String msg);
void onAddFailed(String msg);
}
//view层
展示的view
public class SearchActivity extends AppCompatActivity implements SView{
private ImageView back;
private EditText edit;
private ImageView img;
private Button btn_sou;
private SearchPresent searchPresent;
private String s;
private int page;
private int q;
private boolean flag = true;
private List<SearchBean.DataBean> searchData;
private List<SearchBean.DataBean> searchList=new ArrayList<>();
private SearchAdapter searchAdapter;
private RecyclerView fen_rc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
//获取控件
initview();
//返回
backview();
searchPresent = new SearchPresent(this);
//搜索
sousuo();
}
private void sousuo() {
page=1;
btn_sou.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (edit.getText().toString().trim().equals(null) || edit.getText().toString().trim().equals("")) {
Toast.makeText(SearchActivity.this, "请输入内容", Toast.LENGTH_SHORT).show();
} else {
searchPresent.getData(edit.getText().toString().trim(),page);
}
}
});
}
//返回
private void backview() {
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
private void initview() {
back = (ImageView) findViewById(R.id.fen_btn_back);
edit = (EditText) findViewById(R.id.fen_edit);
img = (ImageView) findViewById(R.id.fen_img);
btn_sou = (Button) findViewById(R.id.fen_btn_sou);
fen_rc=(RecyclerView)findViewById(R.id.fen_rc);
}
@Override
public void onSuccess(Object o) {
SearchBean searchBean = GsonUtils.getInstance().fromJson((String) o, SearchBean.class);
searchData = searchBean.getData();
searchList.clear();
for (int i=0;i<searchData.size();i++){
searchList.add(searchData.get(i));
}
//图片的点击事件
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (flag) {
LinearLayoutManager manager = new LinearLayoutManager(SearchActivity.this, LinearLayoutManager.VERTICAL, false);
fen_rc.setLayoutManager(manager);
searchAdapter = new SearchAdapter(SearchActivity.this, searchList, 2);
fen_rc.setAdapter(searchAdapter);
searchAdapter.setOnItemClickListener(new SearchAdapter.OnItemClickListener() {
@Override
public void setOnItemClick(View view, int position) {
Intent intent = new Intent(SearchActivity.this, CartActivity.class);
intent.putExtra("id", searchList.get(position).getPid());
startActivity(intent);
}
@Override
public void setOnItemLongClick(View view, int position) {
}
});
img.setImageDrawable(getResources().getDrawable(R.drawable.lv_icon));
flag = true;
} else {
GridLayoutManager manager = new GridLayoutManager(SearchActivity.this, 2, LinearLayoutManager.VERTICAL, false);
fen_rc.setLayoutManager(manager);
searchAdapter = new SearchAdapter(SearchActivity.this,searchList,1);
fen_rc.setAdapter(searchAdapter);
searchAdapter.setOnItemClickListener(new SearchAdapter.OnItemClickListener() {
@Override
public void setOnItemClick(View view, int position) {
Intent intent = new Intent(SearchActivity.this, CartActivity.class);
intent.putExtra("id", searchList.get(position).getPid());
startActivity(intent);
}
@Override
public void setOnItemLongClick(View view, int position) {
}
});
img.setImageDrawable(getResources().getDrawable(R.drawable.grid_icon));
flag = false;
}
}
});
}
@Override
public void onFailure(String msg) {
}
@Override
public void onPidSuccess(List<CartBean.DataBean> list) {
}
@Override
public void onPidFailed(String message) {
}
@Override
public void onAddSuccess(String msg) {
}
@Override
public void onAddFailed(String msg) {
}
}
适配器
public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.MyViewHolder> {
private Context context;
private List<SearchBean.DataBean> searchList;
private int q;
private MyViewHolder myViewHolder;
public SearchAdapter(Context context, List<SearchBean.DataBean> searchList,int q) {
this.context=context;
this.searchList=searchList;
this.q=q;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (q == 1) {
View inflate = View.inflate(context, R.layout.search_view, null);
myViewHolder = new MyViewHolder(inflate);
} else {
View inflate = View.inflate(context, R.layout.search_view2, null);
myViewHolder = new MyViewHolder(inflate);
}
return myViewHolder;
}
//声明自己的一个事件回调接口
public interface OnItemClickListener {
//条目点击事件
void setOnItemClick(View view, int position);
//条目长按事件
void setOnItemLongClick(View view, int position);
}
private OnItemClickListener onItemClickListener;
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
holder.tv.setText(searchList.get(position).getTitle());
holder.price.setText(searchList.get(position).getPrice()+"");
String img = searchList.get(position).getImages();
String[] split = img.split("\\|");
Glide.with(context).load(split[0]).into(holder.img);
if (null != onItemClickListener) {
//回调点击事件
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//回调
onItemClickListener.setOnItemClick(v,position);
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
onItemClickListener.setOnItemLongClick(v, position);
return true;//消费掉
}
});
}
}
@Override
public int getItemCount() {
return searchList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
private final ImageView img;
private final TextView tv;
private final TextView price;
public MyViewHolder(View itemView) {
super(itemView);
img = (ImageView) itemView.findViewById(R.id.search_img);
tv = (TextView) itemView.findViewById(R.id.search_tv);
price = (TextView) itemView.findViewById(R.id.search_price);
}
}
}
//传id的view
public class CartActivity extends AppCompatActivity implements SView{
private RecyclerView rc;
private int id;
private SearchPresent searchPresent;
private MySearchShowAdapter mySearchShowAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
rc = (RecyclerView) findViewById(R.id.rc);
Intent intent = getIntent();
int id = intent.getIntExtra("id", this.id);
searchPresent = new SearchPresent(this);
searchPresent.getPid(id);
}
@Override
public void onSuccess(Object o) {
}
@Override
public void onFailure(String msg) {
}
@Override
public void onPidSuccess(List<CartBean.DataBean> list) {
rc.setLayoutManager(new GridLayoutManager(this, 1, GridLayoutManager.VERTICAL, false));
mySearchShowAdapter = new MySearchShowAdapter(this, list);
rc.setAdapter(mySearchShowAdapter);
}
@Override
public void onPidFailed(String message) {
}
@Override
public void onAddSuccess(String msg) {
}
@Override
public void onAddFailed(String msg) {
}
}
//适配器
public class MySearchShowAdapter extends RecyclerView.Adapter<MySearchShowAdapter.ViewHolder> implements SView{
private Context context;
private List<CartBean.DataBean> list;
private SearchPresent searchPresent;
public MySearchShowAdapter(Context context, List<CartBean.DataBean> list) {
this.context = context;
this.list = list;
}
@Override
public MySearchShowAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.fen_show, null);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(MySearchShowAdapter.ViewHolder holder, final int position) {
String img = list.get(position).getImages();
String[] split = img.split("\\|");
Glide.with(context).load(split[0]).into(holder.img);
holder.text.setText(list.get(position).getTitle());
holder.textprice.setText(list.get(position).getPrice() + "");
searchPresent = new SearchPresent(this);
holder.fen_btn_jia.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
searchPresent.getUid(list.get(position).getPid());
}
});
}
@Override
public int getItemCount() {
return list.size();
}
@Override
public void onSuccess(Object o) {
}
@Override
public void onFailure(String msg) {
}
@Override
public void onPidSuccess(List<CartBean.DataBean> list) {
}
@Override
public void onPidFailed(String message) {
}
@Override
public void onAddSuccess(String msg) {
Toast.makeText(context, msg + "", Toast.LENGTH_SHORT).show();
}
@Override
public void onAddFailed(String msg) {
Toast.makeText(context, msg + "", Toast.LENGTH_SHORT).show();
}
class ViewHolder extends RecyclerView.ViewHolder {
ImageView img;
TextView text;
TextView textprice;
Button fen_btn_jia;
public ViewHolder(View itemView) {
super(itemView);
img = (ImageView) itemView.findViewById(R.id.img);
text = (TextView) itemView.findViewById(R.id.text);
textprice = (TextView) itemView.findViewById(R.id.textprice);
fen_btn_jia = (Button) itemView.findViewById(R.id.fen_btn_jia);
}
}
}
布局文件
<ImageView
android:id="@+id/fen_btn_back"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignBottom="@+id/textView"
android:layout_alignStart="@+id/fen_edit"
android:background="@drawable/back"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="搜索商品"
android:textSize="24sp"
android:id="@+id/textView" />
<ImageView
android:id="@+id/fen_img"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:src="@drawable/lv_icon" />
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="50dp"
android:background="#666666" />
<EditText
android:layout_width="350dp"
android:layout_height="wrap_content"
android:id="@+id/fen_edit"
android:hint="输入内容"
android:layout_marginTop="65dp"
android:layout_marginLeft="20dp"/>
<Button
android:id="@+id/fen_btn_sou"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_marginTop="60dp"
android:text="搜索" />
<android.support.v7.widget.RecyclerView
android:id="@+id/fen_rc"
android:layout_below="@id/fen_btn_sou"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"></android.support.v7.widget.RecyclerView>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rc">
</android.support.v7.widget.RecyclerView>
适配器布局
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:scaleType="fitXY"
android:layout_height="200dp" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textprice"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="加入购物车"
android:id="@+id/fen_btn_jia"/>