RecycleView+Glide
封装类
public class Utils {
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);
}
}
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("xx",message);
}
});
return interceptor;
}
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);
}
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);
}
}
登录
布局
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/colorAccent"
android:gravity="center"
android:text="登录"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="50dp"
android:hint="请输入手机号" />
<EditText
android:id="@+id/et_pwd"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:hint="请输入密码" />
<Button
android:id="@+id/bt_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:text="登录" />
<Button
android:id="@+id/bt_zc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="注册" />
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
public class MainActivity extends AppCompatActivity implements LoginView {
private Button button1;
private Button button;
private EditText et_pwd;
private EditText et_phone;
private LoginPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_phone = findViewById(R.id.et_phone);
et_pwd = findViewById(R.id.et_pwd);
button = findViewById(R.id.bt_login);
button1 = findViewById(R.id.bt_zc);
presenter = new LoginPresenter(this);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String phone = et_phone.getText().toString();
String pwd = et_pwd.getText().toString();
boolean mobileNO = Utils.isMobileNO(phone);
if (!mobileNO){
Toast.makeText(MainActivity.this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
return;
}
if (pwd.length()<3){
Toast.makeText(MainActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
return;
}
Map<String,String> params=new HashMap<>();
params.put("phone",phone);
params.put("pwd",pwd);
presenter.sendParamter(params);
}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,ZcActivity.class));
finish();
}
});
}
@Override
public void getViewData(String status) {
if (status.equals("0000")){
startActivity(new Intent(MainActivity.this,ShowActivity.class));
finish();
}
}
}
public class LoginPresenter {
private final LoginModel loginModel;
private final LoginView loginView;
public LoginPresenter(LoginView view) {
loginModel = new LoginModel();
loginView=view;
}
public void sendParamter(Map<String,String> params) {
loginModel.getHttpData(params);
loginModel.setLoginListener(new LoginModel.onLoginListener() {
@Override
public void onResult(String status) {
loginView.getViewData(status);
}
});
}
}
public class LoginModel {
private String path="http://172.17.8.100/small/user/v1/login";
@SuppressLint("HandlerLeak")
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 0:
String json= (String) msg.obj;
Log.i("xxx",json);
try {
JSONObject jsonObject=new JSONObject(json);
String status = jsonObject.getString("status");
loginListener.onResult(status);
} catch (JSONException e) {
e.printStackTrace();
}
break;
}
}
};
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;
}
}
public interface LoginView {
void getViewData(String status);
}
注册
<?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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#621afc">
<TextView
android:id="@+id/return_regist"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="<"
android:textColor="#faf8f8"
android:textSize="30sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="注册"
android:textColor="#faf8f8"
android:textSize="30sp" />
</RelativeLayout>
<EditText
android:id="@+id/num_regist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:hint="手机号"
android:padding="10dp" />
<EditText
android:id="@+id/pwd_regist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:hint="密码"
android:padding="10dp" />
<Button
android:id="@+id/regist_regist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:text="立即注册" />
</LinearLayout>
public class ZcActivity extends AppCompatActivity implements ZcView {
private EditText num_regist;
private EditText pwd_regist;
private Button button;
private TextView textView;
private ZcPresenter zcPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zc);
num_regist = findViewById(R.id.num_regist);
pwd_regist = findViewById(R.id.pwd_regist);
button = findViewById(R.id.regist_regist);
textView = findViewById(R.id.return_regist);
zcPresenter = new ZcPresenter(this);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(ZcActivity.this,MainActivity.class));
finish();
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String phone = num_regist.getText().toString();
String pwd = pwd_regist.getText().toString();
boolean mobileNO = Utils.isMobileNO(phone);
if (!mobileNO){
Toast.makeText(ZcActivity.this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
}
if (pwd.length()<3){
Toast.makeText(ZcActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
}
HashMap<String,String> params=new HashMap<>();
params.put("phone",phone);
params.put("pwd",pwd);
zcPresenter.sendParameter(params);
}
});
}
@Override
public void getViewData(String status) {
if (status.equals("0000")){
startActivity(new Intent(ZcActivity.this,MainActivity.class));
finish();
}
}
}
public class ZcPresenter {
private final ZcModel zcModel;
private final ZcView zcView;
public ZcPresenter(ZcView view) {
zcModel = new ZcModel();
zcView = view;
}
public void sendParameter(HashMap<String,String> params) {
zcModel.getHttpData(params);
zcModel.setRegistListener(new ZcModel.onRegistListener() {
@Override
public void onResult(String status) {
zcView.getViewData(status);
}
});
}
}
public class ZcModel {
String url = "http://172.17.8.100/small/user/v1/register";
@SuppressLint("HandlerLeak")
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);
String status = jsonObject.getString("status");
registListener.onResult(status);
} catch (JSONException e) {
e.printStackTrace();
}
break;
}
}
};
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;
}
}
public interface ZcView {
void getViewData(String status);
}
展示
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#228bc4"
android:gravity="center"
android:text="商品展示"
android:textColor="#faf8f8"
android:textSize="30sp" />
<android.support.v7.widget.RecyclerView
android:id="@+id/rlv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
public class ShowActivity extends AppCompatActivity implements ShowView {
private RecyclerView recyclerView;
private ShowPresenter showPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
recyclerView = findViewById(R.id.rlv);
showPresenter = new ShowPresenter(this);
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
showPresenter.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();
recyclerView.setAdapter(new MyAdapter(ShowActivity.this,result));
}
}
@Override
protected void onDestroy() {
super.onDestroy();
showPresenter.deatchView();
}
}
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;
}
public void attachView(T t){
tReference=new WeakReference<T>(t);
}
public void onRelated() {
showModel.getHttpData();
showModel.setShowListener(new ShowModel.onShowListener() {
@Override
public void onResult(String json) {
showView.getViewData(json);
}
});
}
public void deatchView(){
if (tReference!=null){
tReference.clear();
tReference=null;
}
}
}
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);
switch (msg.what){
case 0:
String json= (String) msg.obj;
showListener.onResult(json);
break;
}
}
};
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;
}
}
public interface ShowView {
void getViewData(String json);
}
适配器
<?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="wrap_content"
tools:context=".MainActivity">
<ImageView
android:id="@+id/img"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentRight="true"/>
<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>
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;
public MyAdapter(Context context, List<JsonBean.ResultBean> result) {
this.context = context;
this.result = result;
}
@Override
public int getItemViewType(int position) {
return position%2;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
if (TYPE_ONE==i){
View view=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_list,null,false);
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=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();
}
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);
}
}
}