1.lib包
implementation ‘com.google.code.gson:gson:2.8.5’
implementation ‘com.android.support:recyclerview-v7:28.0.0’
implementation ‘com.github.bumptech.glide:glide:4.9.0’
implementation ‘com.squareup.okhttp3:okhttp:3.7.0’
2.View
package com.bwie.wu.zuihou.v;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import com.bwie.wu.zuihou.R;
import com.bwie.wu.zuihou.adapter.MyAdapter;
import com.bwie.wu.zuihou.bean.ProductBean;
import com.bwie.wu.zuihou.c.Contarct;
import com.bwie.wu.zuihou.p.Presenter;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements Contarct.MyViewActivity {
RecyclerView recycler;
List<ProductBean.ProductData.Product> mlist = new ArrayList<> ();
Contarct.PresenterInerFace presenterInerFace;
MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
recycler = findViewById (R.id.recycler);
LinearLayoutManager layoutManager = new LinearLayoutManager (this);
layoutManager.setOrientation (LinearLayoutManager.VERTICAL);
recycler.setLayoutManager (layoutManager);
myAdapter = new MyAdapter (mlist,this);
recycler.setAdapter (myAdapter);
presenterInerFace = new Presenter (this);
presenterInerFace.toProduct ();
}
@Override
public void ShowDisplay(Object object) {
ProductBean productBean = (ProductBean) object;
mlist.addAll(productBean.data.data);
myAdapter.notifyDataSetChanged();
}
@Override
protected void onDestroy() {
super.onDestroy();
presenterInerFace.onDeistry ();
presenterInerFace = null;
}
}
3.Presenter
public class Presenter<T> implements Contarct.PresenterInerFace {
Model myModel;
Contarct.MyViewActivity myViewActivity;
T tt;
public Presenter(T t) {
this.tt = t;
myModel = new Model ();
}
@Override
public void toProduct() {
myModel.setModel (new Model.MyCallBack () {
@Override
public void success(Object object) {
myViewActivity = (Contarct.MyViewActivity) tt;
myViewActivity.ShowDisplay (object);
}
});
myModel.getRequest ();
}
@Override
public void onDeistry() {
if(tt!=null){
tt = null;
}
}
}
4.Model
public class Model {
MyCallBack myCallBack;
@SuppressLint("HandlerLeak")
Handler mHandler = 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);
}
}
};
public void getRequest(){
OkHttpUtile utile = OkHttpUtile.getInstance ();
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 ();
Gson gson = new Gson ();
Message message = mHandler.obtainMessage ();
message.obj=json;
message.arg1 = 1;
mHandler.sendMessage (message);
}
});
}
public void setModel(MyCallBack myCallBack) {
this.myCallBack = myCallBack;
}
public interface MyCallBack{
public void success(Object object);
}
}
5.OkHttpUtile
public class OkHttpUtile {
static OkHttpUtile utile;
OkHttpClient okHttpClient;
private OkHttpUtile(){
okHttpClient= new OkHttpClient.Builder ().addInterceptor (new MyInterceptor ()).build ();
}
public static synchronized OkHttpUtile getInstance(){
if(utile == null) {
utile = new OkHttpUtile ();
}
return utile;
}
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);
}
public class MyInterceptor implements Interceptor{
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request ();
Response response = chain.proceed (request);
return response;
}
}
}
6.接口集合
public interface Contarct {
//View层
public interface MyViewActivity{
public void ShowDisplay(Object object);
}
//Presenter层
public interface PresenterInerFace{
public void toProduct();
public void onDeistry();
}
}
7.Adapter
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holdler>{
List<ProductBean.ProductData.Product> mlist;
Context context;
int type=1;
public MyAdapter(List<ProductBean.ProductData.Product> mlist, Context context) {
this.mlist = mlist;
this.context = context;
this.type = type;
}
@NonNull
@Override
public Holdler onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
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 Holdler(view);
}
@Override
public void onBindViewHolder(@NonNull Holdler holdler, int i) {
if(type == 1){
String title = mlist.get(i).title;
String imagUrl = "http://365jia.cn/uploads/" + mlist.get(i).pics.get(0);
if( holdler != null&&holdler.text1 != null){
holdler.text1.setText(title);
Glide.with(context).load(imagUrl).into(holdler.image1);
}
}else {
String title = mlist.get(i).title;
String imagUrl = "http://365jia.cn/uploads/" + mlist.get(i).pics_new.get(0);
if( holdler != null&&holdler.text2 != null){
holdler.text2.setText(title);
Glide.with(context).load(imagUrl).into(holdler.image2);
}
}
}
@Override
public int getItemCount() {
if(mlist!=null){
return mlist.size ();
}
return 0;
}
@Override
public int getItemViewType(int position) {
List<String> plist = mlist.get (position).pics_new;
if(plist == null || plist.size ()==0){
type=1;
}else{
type=2;
}
return super.getItemViewType (position);
}
public class Holdler extends RecyclerView.ViewHolder{
TextView text1,text2;
ImageView image1,image2;
public Holdler(@NonNull View itemView) {
super (itemView);
if(type == 1){
text1 = itemView.findViewById (R.id.title_1_id);
image1= itemView.findViewById (R.id.imag_1_id);
}else{
text2 = itemView.findViewById (R.id.title_2_id);
image2= itemView.findViewById (R.id.imag_2_id);
}
}
}
}