//======================================================注解的接口类两种方式
1:?之前的拼接
public interface Xp_pj_getrequest { @GET("{gid}?page=1&size=2&label=1&is_back=1&pdduid=3470667255") CallgetCall(@Path("gid") int uid); } 2:?之后的拼接
public interface Gg_getrequest { @GET("mall?pdduid=3470667255") Call//================================================M层getCall(@Query("goods_id") int gg_id); }
//=====================M接口
public interface XP_pj_Imodel { //将观察者传(bean)进去 public void shuju(Observer//===================================M类observer, int uid); }
public class XP_pj_model implements XP_pj_Imodel { private Sppjbean sppjbean; private int uid; @Override public void shuju(Observer//===========================================Vobserver, final int uid) { //创建被观察者 Observable.create(new Observable.OnSubscribe () { @Override public void call(final Subscriber super Sppjbean> subscriber) { //Retrofit的请求数据 Retrofit retrofit=new Retrofit.Builder() //设置网络请求的url .baseUrl(" http://apiv4.yangkeduo.com/reviews/") //设置Gson .addConverterFactory(GsonConverterFactory.create()) //设置Rxjava .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .build(); //创建网络请求接口的实例 Xp_pj_getrequest Request=retrofit.create(Xp_pj_getrequest.class); //对发的请求进行封装 Call call=Request.getCall(uid); //使用异步发送网络请求 call.enqueue(new Callback (){ //请求成功时回调 @Override public void onResponse(Call call, Response response) { sppjbean=response.body(); //调用OnNext方法把值传给观察者 subscriber.onNext(sppjbean); subscriber.onCompleted(); } //请求失败时的回调 @Override public void onFailure(Call call, Throwable t) { } }); } }).subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(observer); } }
//================不变
public interface XP_pj_Iview { //方法有参bean public void ShowView(Sppjbean sppjbean); }//=============================================P层
public class XP_pj_persenter { Context context; XP_pj_model mm; XP_pj_Iview vv; public XP_pj_persenter(Context context, XP_pj_Iview vv) { this.context = context; this.vv = vv; mm=new XP_pj_model(); } public void getData(int uid){ mm.shuju(new Observer() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(Sppjbean sppjbean) { vv.ShowView(sppjbean); } },uid); } }
//========================================适配器
public class XP_pjadapter extends RecyclerView.Adapter{ Context context; Sppjbean sppjbean; OnItemClickLinstener listener; @BindView(R.id.pj_img) SimpleDraweeView pjImg; @BindView(R.id.pj_tv) TextView pjTv; @BindView(R.id.pj_time) TextView pjTime; @BindView(R.id.pj_tv2) TextView pjTv2; public XP_pjadapter(Context context, Sppjbean sppjbean) { this.context = context; this.sppjbean = sppjbean; } //获取方法的传参值 public void setOnItemClickLinstener(OnItemClickLinstener listener) { //指定当前的值 this.listener = listener; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.sp_pj_item, null); MyViewHolder holder = new MyViewHolder(view); view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //实例化listener来调用接口的方法 强转类型 listener.OnItemClick(view, (int) view.getTag()); } }); return holder; } @Override public void onBindViewHolder(MyViewHolder holder, int position) { holder.tv.setText(sppjbean.getData().get(position).getName()); holder.tv2.setText(sppjbean.getData().get(position).getComment()); DraweeController dc = Fresco.newDraweeControllerBuilder() .setUri(sppjbean.getData().get(position).getAvatar()) .setAutoPlayAnimations(true) .build(); holder.iv.setController(dc); holder.time.setText(sppjbean.getData().get(position).getTime()+""); holder.itemView.setTag(position); } @Override public int getItemCount() { return sppjbean.getData().size(); } class MyViewHolder extends RecyclerView.ViewHolder { private final SimpleDraweeView iv; private final TextView tv, tv2,time; public MyViewHolder(View itemView) { super(itemView); iv = (SimpleDraweeView) itemView.findViewById(R.id.pj_img); tv = (TextView) itemView.findViewById(R.id.pj_tv); tv2 = (TextView) itemView.findViewById(R.id.pj_tv2); time = (TextView) itemView.findViewById(R.id.pj_time); } } //创建接口 public interface OnItemClickLinstener { //创建接口的方法 public void OnItemClick(View view, int position); } }
//====================================Activity类
//传值的方式
Intent intent=new Intent(getActivity(), XP_xqActivity.class); intent.putExtra("gid",xPbean.getGoods_list().get(position).getGoods_id());
//被接受值的类
XP_pj_persenter pjpp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Fresco.initialize(this); setContentView(R.layout.activity_xp_xq); ButterKnife.bind(this); //接受值 final Intent intent = getIntent(); goods_id = intent.getIntExtra("gid", 0); pjpp = new XP_pj_persenter(this, this); pjpp.getData(goods_id);