记住写权限:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bwie.dome3">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".v.ProductActivy"/>
</application>
</manifest>
导包加依赖:
implementation 'com.squareup.okhttp3:okhttp:3.7.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.github.bumptech.glide:glide:4.9.0'
implementation 'com.google.code.gson:gson:2.8.1'
布局:
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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入账号"/>
<EditText
android:id="@+id/edit_phone_id"
android:layout_width="200dp"
android:layout_height="50dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入密码"/>
<EditText
android:id="@+id/edit_pwd_id"
android:layout_width="200dp"
android:layout_height="50dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/but_login_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登陆"
/>
<Button
android:id="@+id/but_regist_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
item_1_layout.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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/title_1_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="title"/>
<ImageView
android:id="@+id/imag_1_id"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher_round"/>
</LinearLayout>
</LinearLayout>
item_2_layout.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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:id="@+id/title_2_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="title"/>
<ImageView
android:id="@+id/imag_2_id"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@mipmap/ic_launcher_round"/>
</LinearLayout>
</LinearLayout>
product_layout.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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/RecyclerView_id"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</LinearLayout>
主方法:
MainActivity:
package com.bwie.dome3;
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.Toast;
import com.bwie.dome3.contarct.ContarctIntface;
import com.bwie.dome3.p.MyPrensenter;
import com.bwie.dome3.v.ProductActivy;
public class MainActivity extends AppCompatActivity implements ContarctIntface.UserViewIntface{
EditText edphone , edpwd;
Button loginBut , registBut;
//声明P层的接口对象
ContarctIntface.PresenterIntface presenterIntface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
presenterIntface = new MyPrensenter(this);
init();
}
private void init() {
edphone = findViewById(R.id.edit_phone_id);
edpwd = findViewById(R.id.edit_pwd_id);
loginBut = findViewById(R.id.but_login_id);
registBut = findViewById(R.id.but_regist_id);
//点击登录
loginBut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String phone = edphone.getText().toString();
String pwd = edpwd.getText().toString();
//调用P层的方法
presenterIntface.toMLogin(phone ,pwd );
}
});
//点击注册
registBut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String phone = edphone.getText().toString();
String pwd = edpwd.getText().toString();
//调用P层的方法
presenterIntface.toMRegist(phone ,pwd );
}
});
}
@Override
public void showLogin(Object object) {
Intent intent = new Intent(this, ProductActivy.class);
startActivity(intent);
finish();
}
@Override
public void showRegist(Object object) {
Toast.makeText(this , (String)object ,Toast.LENGTH_LONG).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
presenterIntface.onDeistry();
presenterIntface = null;
}
}
V层ProductActivity:
package com.bwie.dome3.v;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.bwie.dome3.R;
import com.bwie.dome3.adapter.ProductAdapter;
import com.bwie.dome3.bean.ProductBean;
import com.bwie.dome3.contarct.ContarctIntface;
import com.bwie.dome3.p.MyPrensenter;
import java.util.ArrayList;
import java.util.List;
/**
* @Auther: lenovo
* @Date: 2019/3/16
* @Description:
*/
public class ProductActivy extends Activity implements ContarctIntface.ProductViewIntface {
//声明P层接口对象
ContarctIntface.PresenterIntface presenterIntface;
ProductAdapter productAdapter;
RecyclerView recyclerView;
List<ProductBean.ProductData.Product> mlist = new ArrayList<>();
@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.product_layout);
recyclerView = findViewById(R.id.RecyclerView_id);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
productAdapter = new ProductAdapter(mlist , this);
recyclerView.setAdapter(productAdapter);
presenterIntface = new MyPrensenter(this);
presenterIntface.toProduct();
}
@Override
public void ShowDisplay(Object obj) {
ProductBean productBean = (ProductBean) obj;
mlist.addAll(productBean.data.data);
productAdapter.notifyDataSetChanged();
}
@Override
protected void onDestroy() {
super.onDestroy();
presenterIntface.onDeistry();
presenterIntface = null;
}
}
工具类OkhttpUtile:
package com.bwie.dome3.utile;
import android.util.Log;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
/**
* @Auther: lenovo
* @Date: 2019/3/16
* @Description:
*/
public class OkhttpUtile {
static OkhttpUtile utile;
OkHttpClient okHttpClient;
//构造方法私有化
private OkhttpUtile(){
okHttpClient = new OkHttpClient.Builder().addInterceptor(new MyInterceptor()).build();
}
public static synchronized OkhttpUtile getInstace(){
if (utile == null){
utile = new OkhttpUtile();
}
return utile;
}
//get
public void doGet(Callback callback){
Request request = new Request.Builder()
.url("http://365jia.cn/news/api3/365jia/news/headline?page=1")
.get()
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(callback);
}
//post
public void doPost(String phone,String pwd,String url,Callback callback){
RequestBody body = new FormBody.Builder()
.add("phone",phone)
.add("pwd",pwd)
.build();
Request request = new Request.Builder()
.post(body)
.url(url)
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(callback);
}
public class MyInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Log.e("tag" ,"request url = " + request.url());
Response response = chain.proceed(request);
return response;
}
}
}
P层代码MyPrensenter:
package com.bwie.dome3.p;
import com.bwie.dome3.contarct.ContarctIntface;
import com.bwie.dome3.m.MyModel;
/**
* @Auther: lenovo
* @Date: 2019/3/16
* @Description:
*/
public class MyPrensenter<T> implements ContarctIntface.PresenterIntface {
//声明model类对象
MyModel myModel;
//声明view接口对象
T tt;
public MyPrensenter(T t){
this.tt = t;
myModel = new MyModel();
}
@Override
public void toProduct() {
myModel.setMyCallback(new MyModel.MyCallback(){
@Override
public void success(Object obj) {
ContarctIntface.ProductViewIntface productViewIntface = (ContarctIntface.ProductViewIntface) tt;
productViewIntface.ShowDisplay(obj);
}
});
myModel.getRequst();
}
@Override
public void toMLogin(String phone, String pwd) {
myModel.setMyCallback(new MyModel.MyCallback() {
@Override
public void success(Object obj) {
((ContarctIntface.UserViewIntface) tt).showLogin(obj);
}
});
myModel.postRequst(phone,pwd,"http://172.17.8.100/small/user/v1/register");
}
@Override
public void toMRegist(String phone, String pwd) {
myModel.setMyCallback(new MyModel.MyCallback() {
@Override
public void success(Object obj) {
((ContarctIntface.UserViewIntface)tt).showRegist(obj);
}
});
myModel.postRequst(phone,pwd,"http://172.17.8.100/small/user/v1/register");
}
@Override
public void onDeistry() {
if (tt != null){
tt = null;
}
}
}
M层MyModel代码:
package com.bwie.dome3.m;
import android.os.Handler;
import android.os.Message;
import com.bwie.dome3.bean.ProductBean;
import com.bwie.dome3.utile.OkhttpUtile;
import com.google.gson.Gson;
import org.json.JSONObject;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
/**
* @Auther: lenovo
* @Date: 2019/3/16
* @Description:
*/
public class MyModel {
MyCallback myCallback;
Handler mhandel = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
String josn = (String) msg.obj;
int type = msg.arg1;
if (type == 1){
Gson gson = new Gson();
ProductBean productBean = gson.fromJson(josn , ProductBean.class);
myCallback.success(productBean);
}
if (type == 2){
try{
JSONObject object = new JSONObject(josn);
String me = object.getString("message");
myCallback.success(me);
}catch (Exception e){
e.printStackTrace();
}
}
}
};
public void getRequst(){
OkhttpUtile utile = OkhttpUtile.getInstace();
utile.doGet(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.obj = json;
message.arg1 = 1;
mhandel.sendMessage(message);
}
});
}
public void postRequst(String phone,String pwd,String url){
OkhttpUtile utile = OkhttpUtile.getInstace();
utile.doPost(phone, pwd, 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.obj = json;
message.arg1 = 2;
mhandel.sendMessage(message);
}
});
}
public void setMyCallback(MyCallback myCallback){
this.myCallback = myCallback;
}
public interface MyCallback{
public void success(Object obj);
}
}
ContarctIntface类:
package com.bwie.dome3.contarct;
/**
* @Auther: lenovo
* @Date: 2019/3/16
* @Description:
*/
public interface ContarctIntface {
//view 层接口
public interface UserViewIntface{
public void showLogin(Object object);
public void showRegist(Object object);
}
// view层商品展示接口
public interface ProductViewIntface{
public void ShowDisplay(Object obj);
}
//p层接口
public interface PresenterIntface{
public void toProduct();
public void toMLogin(String phone , String pwd);
public void toMRegist(String phone , String pwd);
public void onDeistry();
}
}
Bean类ProductBean:
package com.bwie.dome3.bean;
import java.util.List;
/**
* @Auther: lenovo
* @Date: 2019/3/16
* @Description:
*/
public class ProductBean {
public String httpStatusCode;
public String code;
public ProductData data;
public static class ProductData{
public String page;
public String perpage;
public String max_page;
public String total;
public List<Product> data;
public static class Product{
public String id;
public String title;
public String link;
public String t;
public String type;
public String alias;
public String pic_amount;
public String comment_amount;
public String source;
public String type_sign;
public String inner_news;
public String views;
public String comment_amount_label;
public String views_label;
public List<String> pics;
public List<String> pics_new;
}
}
}
适配器ProductAdapter:
package com.bwie.dome3.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bwie.dome3.R;
import com.bwie.dome3.bean.ProductBean;
import java.util.List;
import java.util.concurrent.RecursiveTask;
/**
* @Auther: lenovo
* @Date: 2019/3/16
* @Description:
*/
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.Holder> {
List<ProductBean.ProductData.Product> list;
int type = 1;
Context mContext;
public ProductAdapter(List<ProductBean.ProductData.Product> list , Context context){
this.list = list;
this.mContext = context;
}
@NonNull
@Override
public Holder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
Log.e("tag" ,"onCreateViewHolder");
View view = null;
if(type == 1){
view = LayoutInflater.from(viewGroup.getContext()) .inflate(R.layout.item_1_layout , null);
}else {
view = LayoutInflater.from(viewGroup.getContext()) .inflate(R.layout.item_2_layout , null);
}
return new Holder(view);
}
@Override
public void onBindViewHolder(@NonNull Holder holder, int i) {
Log.e("tag" ,"onBindViewHolder");
if(type == 1){
String title = list.get(i).title;
String imagUrl = "http://365jia.cn/uploads/" + list.get(i).pics.get(0);
if( holder != null&&holder.title_1 != null){
holder.title_1.setText(title);
Glide.with(mContext).load(imagUrl).into(holder.imag_1);
}
}else {
String title = list.get(i).title;
String imagUrl = "http://365jia.cn/uploads/" + list.get(i).pics_new.get(0);
if( holder != null&&holder.title_2 != null){
holder.title_2.setText(title);
Glide.with(mContext).load(imagUrl).into(holder.imag_2);
}
}
}
@Override
public int getItemCount() {
Log.e("tag" ,"getItemCount");
if(list != null){
return list.size();
}
return 0;
}
//重写条目类型的回调方法
@Override
public int getItemViewType(int position) {
List<String> plist = list.get(position).pics_new ;
if(plist == null || plist.size() == 0){
type = 1;
}else {
type = 2;
}
Log.e("tag" ,"getItemViewType = " + type);
return super.getItemViewType(position);
}
public class Holder extends RecyclerView.ViewHolder{
public TextView title_1 , title_2;
public ImageView imag_1 , imag_2;
public Holder(@NonNull View itemView) {
super(itemView);
Log.e("tag" ,"Holder");
if(type == 1){
title_1 = itemView.findViewById(R.id.title_1_id);
imag_1 = itemView.findViewById(R.id.imag_1_id);
}else {
title_2 = itemView.findViewById(R.id.title_2_id);
imag_2 = itemView.findViewById(R.id.imag_2_id);
}
}
}
}