主页面
-
public class MainActivity extends AppCompatActivity implements IContact.IView { @BindView(R.id.rv_view) RecyclerView rvView; private IContact.IPresenter presenterlmp; private Context context; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); context = MainActivity.this; presenterlmp = new IPresenterlmp(); presenterlmp.attachView(this); presenterlmp.responseInfo(); } @Override public void showdata(final String responseString) { runOnUiThread(new Runnable() { @Override public void run() { Gson gson = new Gson(); UserBeen userBeen = gson.fromJson(responseString, UserBeen.class); Log.i("xxx",responseString); List<UserBeen.DataBean> beenData = userBeen.getData(); GridLayoutManager manager = new GridLayoutManager(context, 2,GridLayoutManager.VERTICAL,false); //LinearLayoutManager manager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false); rvView.setLayoutManager(manager); SubAdapter adapter = new SubAdapter(context, beenData); rvView.setAdapter(adapter); } }); } @Override protected void onDestroy() { super.onDestroy(); presenterlmp.datachView(this); } }
IC层
package com.example.zk2mn.ui;
public interface IContact {
public interface IView{
void showdata(String responseString);
}
public interface IPresenter{
void attachView(IView iView);
void datachView(IView iView);
void responseInfo();
}
public interface IModel{
interface onCallBack{
void onCallBack(String responseString);
}
void reponsesData(onCallBack onCallBack);
}
}
P层
package com.example.zk2mn.ui;
import java.lang.ref.WeakReference;
public class IPresenterlmp implements IContact.IPresenter {
IContact.IView iView;
private IContact.IModel mdelmp;
private WeakReference<IContact.IView> viewWeakReference;
private WeakReference<IContact.IModel> modelWeakReference;
@Override
public void attachView(IContact.IView iView) {
this.iView=iView;
mdelmp = new IMdelmp();
viewWeakReference = new WeakReference<>(iView);
modelWeakReference = new WeakReference<>(mdelmp);
}
@Override
public void datachView(IContact.IView iView) {
viewWeakReference.clear();
modelWeakReference.clear();
}
@Override
public void responseInfo() {
mdelmp.reponsesData(new IContact.IModel.onCallBack() {
@Override
public void onCallBack(String responseString) {
iView.showdata(responseString);
}
});
}
}
M层
public class IMdelmp implements IContact.IModel {
@Override
public void reponsesData(final onCallBack onCallBack) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Contant.BASE_url)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
Api api = retrofit.create(Api.class);
Observable<ResponseBody> observable = api.getObservableResonse();
observable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<ResponseBody>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(ResponseBody responseBody) {
try {
String responseString = responseBody.string();
onCallBack.onCallBack(responseString);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
App层
Fresco.initialize(this);
适配器
public class SubAdapter extends RecyclerView.Adapter<SubAdapter.SubViewHolder> {
Context context;
List<UserBeen.DataBean> beenData;
public SubAdapter(Context context, List<UserBeen.DataBean> beenData) {
this.context = context;
this.beenData = beenData;
}
@NonNull
@Override
public SubViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View rootView = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
SubViewHolder holder = new SubViewHolder(rootView);
return holder;
}
@Override
public void onBindViewHolder(@NonNull SubViewHolder holder, int position) {
String images = beenData.get(position).getList().get(0).getImages();
if (images.contains("|")){
images=images.substring(0,images.indexOf("|"));
}else{
images=beenData.get(position).getList().get(0).getImages();
}
//Picasso.get().load(images).into(((SubViewHolder)holder).img);
Uri uri = Uri.parse(images);
holder.img.setImageURI(uri);
holder.text_view.setText(beenData.get(position).getList().get(0).getTitle());
}
@Override
public int getItemCount() {
return beenData.size();
}
class SubViewHolder extends RecyclerView.ViewHolder{
private SimpleDraweeView img;
private TextView text_view;
public SubViewHolder(View itemView) {
super(itemView);
img = itemView.findViewById(R.id.img);
text_view = itemView.findViewById(R.id.text_view);
}
}
}
依赖
implementation 'com.squareup.okhttp3:okhttp:3.11.0' implementation 'com.jakewharton:butterknife:8.8.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' implementation 'com.android.support:design:27.1.1' implementation 'com.google.code.gson:gson:2.2.4' implementation 'io.reactivex.rxjava2:rxandroid:2.0.2' implementation 'io.reactivex.rxjava2:rxjava:2.0.2' implementation 'com.squareup.retrofit2:retrofit:2.0.2' implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0' implementation 'com.squareup.picasso:picasso:2.71828' implementation 'com.facebook.fresco:fresco:1.10.0' implementation 'org.greenrobot:greendao:3.2.2' // add library