activity_main.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" android:padding="10dp" android:orientation="vertical" tools:context="zhuyuanyuan.bwei.com.lianxione.MainActivity"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/edit_shouji" android:hint="请输入手机号" android:textColorHint="#505050" android:textSize="20dp" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/edit_pass" android:hint="请输入密码" android:textColorHint="#505050" android:textSize="20dp" android:layout_marginTop="20dp" /> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn_login" android:onClick="LoginClick" android:text="登录" android:layout_margin="20dp" /> <Button android:layout_margin="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn_register" android:onClick="Register" android:text="注册" /> </LinearLayout> </LinearLayout>
activity_register.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" android:orientation="vertical" tools:context="zhuyuanyuan.bwei.com.lianxione.Register"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/reg_shouji" android:hint="请输入手机号" android:textColorHint="#505050" android:textSize="20dp" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/reg_pass" android:hint="请输入密码" android:textColorHint="#505050" android:textSize="20dp" android:layout_marginTop="20dp" /> <Button android:layout_gravity="center" android:layout_margin="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/reg_register" android:onClick="RegisterClick" android:text="立即注册" /> </LinearLayout>activity_search.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:orientation="vertical" 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=".searchActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="2" android:id="@+id/et"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="8" android:text="搜索" android:onClick="suosou" android:background="#ffffff" android:textColor="#000" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="热搜" android:textSize="26dp" android:textColor="@android:color/black" /> <zhuyuanyuan.bwei.com.lianxione.zidingyi android:layout_width="match_parent" android:layout_height="100dp"> <Button android:textSize="22dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="老人机" /> <Button android:textSize="22dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="应急启动电源" /> <Button android:textSize="22dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="餐桌" /> <Button android:textSize="22dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="粽子散装" /> <Button android:textSize="22dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="智能手表" /> <Button android:textSize="22dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="摩托车" /> </zhuyuanyuan.bwei.com.lianxione.zidingyi> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" android:text="历史记录" android:textSize="26dp" /> <ListView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="8" android:id="@+id/lv" ></ListView> <Button android:layout_gravity="center" android:layout_width="200dp" android:layout_height="30dp" android:layout_weight="1" android:text="清空历史搜索" android:onClick="sc" /> </LinearLayout> </LinearLayout>activity_show.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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="zhuyuanyuan.bwei.com.lianxione.showActivity"> <LinearLayout android:orientation="horizontal" android:id="@+id/liner" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="5" /> <TextView android:id="@+id/t1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="搜索商品" android:layout_centerHorizontal="true" android:textSize="24dp" android:layout_marginTop="20dp" android:layout_weight="3" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="3" /> <ImageView android:layout_weight="1" android:layout_margin="20dp" android:layout_width="30dp" android:layout_height="35dp" android:id="@+id/show_img" android:layout_toRightOf="@+id/t1" android:background="@drawable/bag_selector" android:src="@drawable/kind_grid" /> </LinearLayout> <TextView android:layout_below="@+id/liner" android:layout_width="match_parent" android:layout_height="1dp" android:background="#000" android:id="@+id/bg_t" /> <LinearLayout android:id="@+id/bg_lin" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_below="@+id/bg_t" > <EditText android:layout_marginTop="10dp" android:hint="请输入关键词" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="2" android:id="@+id/show_et"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="8" android:text="搜索" android:onClick="suosou" android:background="#ffffff" android:textColor="#000" /> </LinearLayout> <com.jcodecraeer.xrecyclerview.XRecyclerView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/xrecy" android:layout_below="@+id/bg_lin" > </com.jcodecraeer.xrecyclerview.XRecyclerView> </RelativeLayout>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="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv" /> </LinearLayout>item_show.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"> <ImageView android:layout_width="75dp" android:layout_height="75dp" android:id="@+id/show_img" /> <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/show_title" android:textSize="24dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/show_price" android:textSize="22dp" android:textColor="#f00" /> </LinearLayout> </LinearLayout>添加的依赖
compile 'com.android.support:appcompat-v7:26.+' compile 'com.android.support.constraint:constraint-layout:1.0.2' compile 'com.github.bumptech.glide:glide:3.5.2' compile 'com.squareup.okhttp3:okhttp:3.6.0' compile 'com.squareup.okio:okio:1.11.0' testCompile 'junit:junit:4.12' compile 'com.jcodecraeer:xrecyclerview:1.5.7' MainActivity类 public class MainActivity extends AppCompatActivity implements ILogin{ private EditText edit_phone; private EditText edit_pwd; private String regex; private LoginPresenter loginPresenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取控件 edit_phone = (EditText) findViewById(R.id.edit_shouji); edit_pwd = (EditText) findViewById(R.id.edit_pass); 手机号的正则 regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}$"; loginPresenter = new LoginPresenter(this); } public void LoginClick(View view){ String phone = edit_phone.getText().toString(); String pwd = edit_pwd.getText().toString(); //非空判断 if (!TextUtils.isEmpty(phone)&&!TextUtils.isEmpty(pwd)){ //验证手机号 Pattern p = Pattern.compile(regex); Matcher m = p.matcher(phone); if (m.matches()&&pwd.length()>=6){ //MD5加密...Md5Encoder.encode(pwd) loginPresenter.LoginUser(phone,pwd,"http://120.27.23.105/user/login"); }else if (!m.matches()){ Toast.makeText(this,"请输入正确的手机号", Toast.LENGTH_SHORT).show(); }else if (pwd.length()<6){ Toast.makeText(this,"密码长度至少六位",Toast.LENGTH_SHORT).show(); } }else if (TextUtils.isEmpty(phone)){ Toast.makeText(this,"手机号不能为空",Toast.LENGTH_SHORT).show(); }else if (TextUtils.isEmpty(pwd)){ Toast.makeText(this,"密码不能为空",Toast.LENGTH_SHORT).show(); } } public void Register(View view) { Intent intent = new Intent(MainActivity.this,Register.class); startActivity(intent); } @Override public void onSuccess(final LoginBean loginBean) { String code = loginBean.getCode(); if ("0".equals(code)){//登陆成功 MyApplication.isLoginSuccess = true; //登录的状态需要存在本地\ CommonUtils.putBoolean("isLogin",true); Toast.makeText(MainActivity.this,"aaaaaaa",Toast.LENGTH_SHORT).show(); Intent intent=new Intent(MainActivity.this,searchActivity.class); startActivity(intent); }else { Toast.makeText(MainActivity.this,loginBean.getMsg(),Toast.LENGTH_SHORT).show(); } } @Override public void onError(String s) { } }
Register类
public class Register extends AppCompatActivity implements IRegist{ private EditText edit_phone; private EditText edit_pwd; private String regex; private RegistPresenter registPresenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); edit_phone = (EditText) findViewById(R.id.reg_shouji); edit_pwd = (EditText) findViewById(R.id.reg_pass);//手机号的正则 regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}$"; //创建presenter registPresenter = new RegistPresenter(this); } public void RegisterClick(View view) { String phone = edit_phone.getText().toString(); String pwd = edit_pwd.getText().toString(); //非空判断 if (! TextUtils.isEmpty(phone) && !TextUtils.isEmpty(pwd)){ //验证手机号 Pattern p = Pattern.compile(regex); Matcher m = p.matcher(phone); if (m.matches() && pwd.length()>=6){ //MD5加密....Md5Encoder.encode(pwd) try { registPresenter.registUser(phone, pwd, "http://120.27.23.105/user/reg"); } catch (Exception e) { e.printStackTrace(); } }else if (!m.matches()){ Toast.makeText(Register.this,"请输入正确的手机号", Toast.LENGTH_SHORT).show(); }else if (pwd.length()<6){ Toast.makeText(Register.this,"密码长度至少六位",Toast.LENGTH_SHORT).show(); } }else if (TextUtils.isEmpty(phone)){ Toast.makeText(Register.this,"手机号不能为空",Toast.LENGTH_SHORT).show(); }else if (TextUtils.isEmpty(pwd)){ Toast.makeText(Register.this,"密码不能为空",Toast.LENGTH_SHORT).show(); } } @Override public void onSuccess(RegistBean registBean) {//此时已经是主线程 String code = registBean.getCode();//0注册成功...1注册失败 if ("0".equals(code)){ Toast.makeText(Register.this,"注册成功,请登录",Toast.LENGTH_SHORT).show(); Register.this.finish(); }else { Toast.makeText(Register.this,registBean.getMsg(),Toast.LENGTH_SHORT).show(); } } @Override public void onError(String s) { }}searchActivity类 public class searchActivity extends AppCompatActivity { private List<String> list; private EditText et; private ListView lv; private MySearAdapter mySearAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_search); list = new ArrayList<>(); et = (EditText) findViewById(R.id.et); lv = (ListView) findViewById(R.id.lv); } public void suosou(View view) { String s = et.getText().toString(); Intent intent=new Intent(searchActivity.this,showActivity.class); intent.putExtra("name",s); startActivity(intent); mySearAdapter = new MySearAdapter(list,searchActivity.this); list.add(s); lv.setAdapter(mySearAdapter); lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view,final int position, long id) { AlertDialog.Builder builder = new AlertDialog.Builder(searchActivity.this); builder.setMessage("确定删除"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { list.remove(list.get(position)); mySearAdapter.notifyDataSetChanged(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.show(); return true; } }); mySearAdapter.notifyDataSetChanged(); } public void sc(View view) { list.clear(); mySearAdapter.notifyDataSetChanged(); } } showActivity类public class showActivity extends AppCompatActivity { private MyShowAdapter showAdapter; private XRecyclerView xrecy; private int num=1; private List<ShowBean.DataBean> list=new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_show); xrecy = (XRecyclerView) findViewById(R.id.xrecy); String name = getIntent().getStringExtra("name"); xrecy.setLayoutManager(new LinearLayoutManager(showActivity.this)); OkHttp3Util.doGet("https://www.zhaoapi.cn/product/searchProducts?keywords=" + name + "&page=" + num, new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { Gson gson=new Gson(); String json = response.body().string(); ShowBean showBean = gson.fromJson(json, ShowBean.class); List<ShowBean.DataBean> data = showBean.getData(); list.addAll(data); runOnUiThread(new Runnable() { @Override public void run() { showAdapter = new MyShowAdapter(list,showActivity.this); xrecy.setItemAnimator(new DefaultItemAnimator()); xrecy.setAdapter(showAdapter); } }); } }); xrecy.setLoadingListener(new XRecyclerView.LoadingListener() { @Override public void onRefresh() { runOnUiThread(new Runnable() { @Override public void run() { num=1; //xrecy.setAdapter(showAdapter); xrecy.refreshComplete(); //下拉刷新完成 } }); } @Override public void onLoadMore() { runOnUiThread(new Runnable() { @Override public void run() { num++; //xrecy.setAdapter(showAdapter); xrecy.loadMoreComplete();//加载更多完成 } }); } }); } } zingdiyi类public class zidingyi extends ViewGroup { public zidingyi(Context context) { this(context,null); } public zidingyi(Context context, AttributeSet attrs) { this(context, attrs,0); } public zidingyi(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int wid=0; int hei=0; int childCount = getChildCount(); for (int i=0;i<childCount;i++){ View childAt = getChildAt(i); int measuredHeight = childAt.getMeasuredHeight(); int measuredWidth = childAt.getMeasuredWidth(); childAt.layout(wid,hei,wid+measuredWidth,hei+measuredHeight); wid=wid+measuredWidth+20; hei=hei+measuredHeight-measuredHeight; } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); measureChildren(widthMeasureSpec, heightMeasureSpec); } }adapter包
MySearADapter类public class MySearAdapter extends BaseAdapter { List<String> list; Context context; public MySearAdapter(List<String> list, Context context) { this.list = list; this.context = context; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { viewholder vh; if(convertView==null){ vh=new viewholder(); convertView=View.inflate(context, R.layout.item,null); vh.tv=convertView.findViewById(R.id.tv); convertView.setTag(vh); } else { vh= (viewholder) convertView.getTag(); } vh.tv.setText(list.get(position).toString()); return convertView; } class viewholder{ TextView tv; } }MyShowAdapter类public class MyShowAdapter extends RecyclerView.Adapter<MyShowAdapter.GiewHolder> { List<ShowBean.DataBean> list; Context context; public MyShowAdapter(List<ShowBean.DataBean> list, Context context) { this.list = list; this.context = context; } @Override public GiewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.item_show, null); GiewHolder holder=new GiewHolder(view); return holder; } @Override public void onBindViewHolder(GiewHolder holder, int position) { String images = list.get(position).getImages(); String[] split = images.split("\\|"); Glide.with(context).load(split[0]).into(holder.img); holder.title.setText(list.get(position).getTitle()); holder.price.setText(list.get(position).getPrice()+""); } @Override public int getItemCount() { return list.size(); } class GiewHolder extends RecyclerView.ViewHolder{ public ImageView img; public TextView title; public TextView price; public GiewHolder(View itemView) { super(itemView); img = itemView.findViewById(R.id.show_img); title = itemView.findViewById(R.id.show_title); price = itemView.findViewById(R.id.show_price); } } }Model层
LoginModel类public class LoginModel { private ILoginP iLoginP; public LoginModel(ILoginP iLoginP) { this.iLoginP = iLoginP; } public void LoginUser(String phone,String encodePwd,String loginUrl){ //注册 OkHttpClient okHttpClient = new OkHttpClient(); RequestBody requestBody=new FormBody.Builder() .add("mobile",phone) .add("password",encodePwd) .build(); Request request=new Request.Builder() .post(requestBody) .url(loginUrl) .build(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { iLoginP.onLoginError(e.getLocalizedMessage()); } @Override public void onResponse(Call call, Response response) throws IOException { if (response.isSuccessful()){ //response.body().string()响应只能一次,,子线程 final String json = response.body().string(); //回调给主线程 CommonUtils.runOnUIThread(new Runnable() { @Override public void run() { LoginBean loginBean = new Gson().fromJson(json, LoginBean.class); //返回presenter iLoginP.onLoginSuccess(loginBean); } }); } } }); } }RegistModel类public class RegistModel { private IRegistP iRegistP; public RegistModel(IRegistP iRegistP) { this.iRegistP = iRegistP; } public void registUser(String phone, String encodePwd, String registUrl) { //注册 OkHttpClient okHttpClient = new OkHttpClient(); RequestBody requestBody = new FormBody.Builder() .add("mobile",phone) .add("password",encodePwd) .build(); Request request = new Request.Builder() .post(requestBody) .url(registUrl) .build(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { iRegistP.onRegistError(e.getLocalizedMessage()); } @Override public void onResponse(Call call, Response response) throws IOException { if (response.isSuccessful()){ //response.body().string()响应只能一次,,,子线程 final String json = response.body().string(); //回调给主线程 CommonUtils.runOnUIThread(new Runnable() { @Override public void run() { RegistBean registBean = new Gson().fromJson(json, RegistBean.class); //返回presenter iRegistP.onRegistSuccess(registBean); } }); } } }); } }Presenter层
接口ILoginPpublic interface ILoginP { void onLoginSuccess(LoginBean loginBean); void onLoginError(String s); }接口IRegistPpublic interface IRegistP { void onRegistSuccess(RegistBean registBean); void onRegistError(String string); }LoginPresenter类 public class LoginPresenter implements ILoginP{ private ILogin iLogin; private final LoginModel loginModel; public LoginPresenter(ILogin iLogin) { //登录的model this.iLogin = iLogin; loginModel = new LoginModel(this); } public void LoginUser(String phone,String encodePwd,String loginUrl){ loginModel.LoginUser(phone,encodePwd,loginUrl); } @Override public void onLoginSuccess(LoginBean loginBean) { iLogin.onSuccess(loginBean); } @Override public void onLoginError(String s) { iLogin.onError(s); } }RegistPresenter类 public class RegistPresenter implements IRegistP { private IRegist iRegist; private RegistModel registModel; public RegistPresenter(IRegist iRegist) { this.iRegist = iRegist; //创建model registModel = new RegistModel(this); } public void registUser(String phone, String encodePwd, String registUrl) { //去model里面注册 registModel.registUser(phone,encodePwd,registUrl); } @Override public void onRegistSuccess(RegistBean registBean) { //回调给registActivity iRegist.onSuccess(registBean); } @Override public void onRegistError(String string) { iRegist.onError(string); } }view层
接口ILoginpublic interface ILogin { void onSuccess(LoginBean loginBean); void onError(String s); } 接口IRegist public interface IRegist {void onSuccess(RegistBean registBean); void onError(String s); }还有okhttp commonUtils MyApplication工具类