工具类
公共类okhttputil {
okhttp客户端okhttp客户端;
静态okhttputil okhttputil;
publicokhttputil ((ok http client=newokhttpclient.builder ).add interceptor (newmyinterceptor ) ) ) ) build ); } publicstaticsynchronizedokhttputilgetintface () if ) okhttputil==null ) { okhttpUtil=new OkhttpUtil ); } return okhttpUtil; }公共语音文档(callback callback )请求=new request.builder ).get ).URL ) http://365JIA.cn/news page=1 呼叫呼叫=ok http client.new call (request ); call.enqueue (呼叫后退; } publicclassmyinterceptorimplementsinterceptor { @ overridepublicresponseintercept (chain ) throws殷恳的棘球声学(request requeques ) 返回响应; } }
模型层
公共类mymodel {
我的客服我的客服;
Handler handler=new Handler
@Override
publicvoidhandlemessage (消息msg )。
super.handle消息(msg;
stringJSON=(string ) msg.obj;
Gson gson=new Gson (;
goodsbeangoodsbean=gson.from JSON (JSON,GoodsBean.class );
mycallback.seuUess(GoodsBean );
}; 公共语音获取数据((okhttputilokhttputil=okhttputil.getint face ) ); okhttputil.doget(newcallback () overridepublicvoidonfailure ) call,体贴的金针菇) (/要求I失败) @ overridepublicvoidonrerencence 消息消息=new消息(; message.obj=json; Handler.sendmessage; (); } publicvoidsetmycollback (my callback my callback ) { this.mycallback=mycallback; } publicinterfacemycallback { publicvoidseuuess (objectobj ); }
presenter层
publicclassmypersenterimplementsconaterinterface .热画板{
MyModel myModel;
ConaterInterface .幸福短靴viewInterface;
publicmypersenter (conaterinterface .幸福短靴viewInterface ) this.view interface=view interface; myModel=new MyModel (; } @ overridepublicvoidtomodel ((mymodel.setmycollback ) newmymodel.mycallback ) { @ override公共语音安全} )。
Object obj) { viewInterface.toData(obj); } }); myModel.getData();}}
public class MainActivity extends 野性的小鸽子 implements ConaterInterface.幸福的短靴{
ConaterInterface.炙热的画板 presenterInterface;
public RecyclerView recycler_view;
List<GoodsBean.DataBeanX.DataBean> list = new ArrayList<>();
GoodsAdapter adapter;
}
接口
public interface ConaterInterface {
适配器
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@Overridepublic 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);}@Overridepublic 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); } }}@Overridepublic int getItemCount() { Log.e("tag" ,"getItemCount"); if(list != null){ return list.size(); } return 0;}//重写条目类型的回调方法@Overridepublic 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); } }}