1.MainActivity
public class MainActivity extends AppCompatActivity implements LoginView {
private EditText ed_pwd, ed_num;
private Button regist, login;
private LoginPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//控件
ed_num = findViewById(R.id.num_login);
ed_pwd = findViewById(R.id.pwd_login);
login = findViewById(R.id.login_login);
regist = findViewById(R.id.regist_login);
//实例化p
presenter = new LoginPresenter(this);
//跳转注册
regist.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, RegistActivity.class));
finish();
}
});
//登录按钮
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取输入框的值
String phone = ed_num.getText().toString();
String pwd = ed_pwd.getText().toString();
boolean mobileNO = TelUtils.isMobileNO(phone);
//判断格式
if (!mobileNO) {
Toast.makeText(MainActivity.this, "手机号格式不对", Toast.LENGTH_SHORT);
return;
}
if (pwd.length() < 3) {
Toast.makeText(MainActivity.this, "密码格式不对", Toast.LENGTH_SHORT);
return;
}
Map<String, String> params = new HashMap<>();
params.put("phone", phone);
params.put("pwd", pwd);
presenter.onRelated(params);
}
});
}
@Override
public void getViewData(String status) {
Toast.makeText(this, ""+status, Toast.LENGTH_SHORT).show();
if (status.equals("0000")) {
startActivity(new Intent(MainActivity.this, ShowActivity.class));
finish();
}else if(status.equals("1001")){
Toast.makeText(MainActivity.this,"登录失败",Toast.LENGTH_SHORT).show();
}
}
}
2.注册’’
package com.bawei.lian.activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.bawei.lian.R;
import com.bawei.lian.presenter.RegistPresenter;
import com.bawei.lian.utils.TelUtils;
import com.bawei.lian.view.RegistView;
import java.util.HashMap;
public class RegistActivity extends AppCompatActivity implements RegistView {
private EditText ed_num, ed_pwd;
private Button regist;
private TextView return_btu;
private RegistPresenter persenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_regist);
//初始化控件
return_btu = findViewById(R.id.return_regist);
ed_num = findViewById(R.id.num_regist);
ed_pwd = findViewById(R.id.pwd_regist);
regist = findViewById(R.id.regist_regist);
//实例化persenter
persenter = new RegistPresenter(this);
//点击返回按钮返回登录界面
return_btu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//跳转登录页面
startActivity(new Intent(RegistActivity.this, MainActivity.class));
finish();
}
});
regist.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取输入框的值
String phone = ed_num.getText().toString();
String pwd = ed_pwd.getText().toString();
boolean mobileNO = TelUtils.isMobileNO(phone);
//判断手机号
if (!mobileNO) {
Toast.makeText(RegistActivity.this, "手机号格式不正确!!!", Toast.LENGTH_SHORT).show();
return;
}
//判断密码
if (pwd.length() < 3) {
Toast.makeText(RegistActivity.this, "密码格式不正确!!!", Toast.LENGTH_SHORT).show();
return;
}
//传值给presenter
HashMap<String, String> params = new HashMap<>();
params.put("phone",phone);
params.put("pwd",pwd);
persenter.sendParameter(params);
}
});
}
@Override
public void getViewData(String status) {
Toast.makeText(this, ""+status, Toast.LENGTH_SHORT).show();
if (status.equals("0000")){
Toast.makeText(RegistActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
startActivity(new Intent(RegistActivity.this, MainActivity.class));
finish();
}else if(status.equals("1001")){
Toast.makeText(RegistActivity.this,"注册失败",Toast.LENGTH_SHORT).show();
}
}
}
3.展示页面
package com.bawei.lian.activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.bawei.lian.R;
import com.bawei.lian.adapter.MyAdapter;
import com.bawei.lian.bean.JsonBean;
import com.bawei.lian.presenter.ShowPresenter;
import com.bawei.lian.view.ShowView;
import com.google.gson.Gson;
import java.util.List;
public class ShowActivity extends AppCompatActivity implements ShowView {
private RecyclerView rlv;
private ShowPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
//控件
rlv = findViewById(R.id.rlv);
//实例化p
presenter = new ShowPresenter(this);
//为了方便p管理内存泄漏
presenter.attachView(this);
//创建布局管理器
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
//设置RecycleView展示类型
rlv.setLayoutManager(linearLayoutManager);
//传参
presenter.onRelated();
}
@Override
public void getViewData(String json) {
if (json != null) {
Gson gson = new Gson();
JsonBean jsonBean = gson.fromJson(json, JsonBean.class);
List<JsonBean.ResultBean> result = jsonBean.getResult();
rlv.setAdapter(new MyAdapter(ShowActivity.this, result));
}
}
@Override
protected void onDestroy() {
super.onDestroy();
presenter.deatchView();
}
}
4.P层
①LoginPresenter
public class LoginPresenter {
private final LoginModel loginModel;
private final LoginView loginView;
public LoginPresenter(LoginView view) {
loginModel = new LoginModel();
loginView = view;
}
public void onRelated(Map<String, String> params) {
loginModel.getHttpData(params);
loginModel.setLoginListener(new LoginModel.onLoginListener() {
@Override
public void onResult(String status) {
loginView.getViewData(status);
}
});
}
}
②RegistPresenter
public class RegistPresenter {
private final RegistModel registModel;
private final RegistView registView;
public RegistPresenter(RegistView view) {
registModel = new RegistModel();
registView = view;
}
public void sendParameter(HashMap<String, String> params) {
registModel.getHttpData(params);
registModel.setRegistListener(new RegistModel.onRegistListener() {
@Override
public void onResult(String status) {
registView.getViewData(status);
}
});
}
}
③ShowPresenter
public class ShowPresenter<T> {
private final ShowModel showModel;
private final ShowView showView;
private Reference<T> tReference;
public ShowPresenter(ShowView view) {
showModel = new ShowModel();
showView = view;
}
//在p层使用软引用管理外部类对象
public void attachView(T t) {
//创建软引用(父类引用子类)
tReference = new WeakReference<T>(t);
}
public void onRelated() {
showModel.getHttpData();
//获取m值
showModel.setShowListener(new ShowModel.onShowListener() {
@Override
public void onResult(String json) {
showView.getViewData(json);
}
});
}
//解除关联
public void deatchView(){
if (tReference!=null){
tReference.clear();
tReference = null;
}
}
}
5.v层
①LoginView
public interface LoginView {
void getViewData(String status);
}
②RegistView
public interface RegistView {
void getViewData(String status);
}
③ShowView
public interface ShowView {
void getViewData(String json);
}
6.M层
①LoginModel
public class LoginModel {
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0) {
String json = (String) msg.obj;
// Log.i("xxx", "handleMessage: " + json);
try {
JSONObject jsonObject = new JSONObject(json);
String status = jsonObject.getString("status");
loginListener.onResult(status);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
};
String path = "http://172.17.8.100/small/user/v1/login";
public void getHttpData(Map<String, String> params) {
OkHttpUtils.doPost(path, params, 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);
}
});
}
//接口回调
public interface onLoginListener {
void onResult(String status);
}
public onLoginListener loginListener;
public void setLoginListener(onLoginListener loginListener) {
this.loginListener = loginListener;
}
}
②RegistModel
public class RegistModel {
String url = "http://172.17.8.100/small/user/v1/register";
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0) {
String json = (String) msg.obj;
// Log.i("xxx", "handleMessage: " + json);
try {
JSONObject jsonObject = new JSONObject(json);
String status = jsonObject.getString("status");
registListener.onResult(status);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
};
public void getHttpData(HashMap<String, String> params) {
OkHttpUtils.doPost(url, params, 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);
}
});
}
public interface onRegistListener {
void onResult(String status);
}
public onRegistListener registListener;
public void setRegistListener(onRegistListener registListener) {
this.registListener = registListener;
}
}
③ShowModel
public class ShowModel {
String url = "http://172.17.8.100/small/commodity/v1/bannerShow";
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0) {
String json = (String) msg.obj;
//Log.i("xxx", "handleMessage: " + json);
showListener.onResult(json);
}
}
};
public void getHttpData() {
//网络请求
OkHttpUtils.doGet(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);
}
});
}
//接口回调
public interface onShowListener {
void onResult(String json);
}
public onShowListener showListener;
public void setShowListener(onShowListener showListener) {
this.showListener = showListener;
}
}
7.utils
①OkHttpUtils
public class OkHttpUtils {
//私有构造方法
private OkHttpUtils() {}
private static OkHttpUtils okHttpUtils = null;
//静态工厂
public static OkHttpUtils getInstance() {
if (okHttpUtils == null) {
okHttpUtils = new OkHttpUtils();
}
return okHttpUtils;
}
//拦截器
private static Interceptor myInterceptor() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
Log.i("xxx", "log:拦截 " + message);
}
});
return interceptor;
}
//执行get方法
public static void doGet(String url, Callback callback) {
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(myInterceptor())
.build();
Request request = new Request.Builder()
.url(url)
.build();
okHttpClient.newCall(request).enqueue(callback);
}
//执行post
public static void doPost(String url, Map<String, String> params, Callback callback) {
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(myInterceptor())
.build();
//请求体
FormBody.Builder builder = new FormBody.Builder();
//遍历map集合
for (String key : params.keySet()) {
builder.add(key, params.get(key));
}
Request request = new Request.Builder()
.url(url)
.post(builder.build())
.build();
okHttpClient.newCall(request).enqueue(callback);
}
}
②TelUtils
public class TelUtils {
public static boolean isMobileNO(String mobileNums) {
/**
* 判断字符串是否符合手机号码格式
* 移动号段: 134,135,136,137,138,139,147,150,151,152,157,158,159,170,178,182,183,184,187,188
* 联通号段: 130,131,132,145,155,156,170,171,175,176,185,186
* 电信号段: 133,149,153,170,173,177,180,181,189
* @param str
* @return 待检测的字符串
*/
String telRegex = "^((13[0-9])|(14[5,7,9])|(15[^4])|(18[0-9])|(17[0,1,3,5,6,7,8]))\\d{8}$";// "[1]"代表下一位为数字可以是几,"[0-9]"代表可以为0-9中的一个,"[5,7,9]"表示可以是5,7,9中的任意一位,[^4]表示除4以外的任何一个,\\d{9}"代表后面是可以是0~9的数字,有9位。
if (TextUtils.isEmpty(mobileNums))
return false;
else
return mobileNums.matches(telRegex);
}
}
8.Adapter 多条目
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private Context context;
private List<JsonBean.ResultBean> result;
private static final int TYPE_ONE = 0;
private static final int TYPE_TWO = 1;
@Override
public int getItemViewType(int position) {
return position % 2;
}
public MyAdapter(Context context, List<JsonBean.ResultBean> result) {
this.context = context;
this.result = result;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
if (TYPE_ONE == i) {
//引入条目布局
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_item, null, false);
//传给自定义Viewholder
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
} else {
//引入条目布局
View view = LayoutInflater.from(viewGroup.getContext()).inflate(android.R.layout.simple_expandable_list_item_1, null, false);
//传给自定义Viewholder
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
int itemViewType = getItemViewType(i);
if (TYPE_ONE == itemViewType) {
//给控件赋值
viewHolder.title.setText(result.get(i).getTitle());
Glide.with(context).load(result.get(i).getImageUrl()).into(viewHolder.img);
} else {
viewHolder.title2.setText(result.get(i).getTitle());
}
}
@Override
public int getItemCount() {
return result.size();
}
//自定义ViewHolder
public class ViewHolder extends RecyclerView.ViewHolder {
private final ImageView img;
private final TextView title;
private final TextView title2;
public ViewHolder(@NonNull View itemView) {
super(itemView);
//通过布局查找控件
title = itemView.findViewById(R.id.title1);
title2 = itemView.findViewById(android.R.id.text1);
img = itemView.findViewById(R.id.img);
}
}
}
9.布局
item
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp">
<ImageView
android:id="@+id/img"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentRight="true"
android:src="@drawable/pink" />
<TextView
android:id="@+id/title1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/img"
android:text="哈哈"
android:textSize="25sp" />
</RelativeLayout>