public interface ServerApi { @GET(UrlApi.URL) Call<LogBean> logBeanCall(); } /public class UrlApi { public static final String HOST_URL="http://gank.io/api/data/Android/"; public static final String URL="10/1"; } //封装public class RetrofitHepler { public static OkHttpClient okHttpClient; public static ServerApi serverApi; static { initOkHttpClent(); } //初始化OkHttpClient private static void initOkHttpClent() { if(okHttpClient == null){ synchronized (RetrofitHepler.class){ if(okHttpClient == null){ okHttpClient=new OkHttpClient(); } } } } public static ServerApi getServerApi(){ if(serverApi == null){ synchronized (ServerApi.class){ serverApi=OnCreatApi(ServerApi.class,UrlApi.HOST_URL); } } return serverApi; } //定义方法初始化ServerApi public static<T> T OnCreatApi(Class<T> tClass,String url) { Retrofit retrofit=new Retrofit .Builder().baseUrl(url) .addConverterFactory(GsonConverterFactory.create()) .build(); return retrofit.create(tClass); } } //底部导航public class MainActivity extends AppCompatActivity { private BottomTabBar mb; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mb=(BottomTabBar)findViewById(R.id.bottom_tab_bar); mb.init(getSupportFragmentManager()) .setImgSize(50,50) .setFontSize(8) .setTabPadding(4,6,10) .setChangeColor(Color.RED,Color.DKGRAY) .addTabItem("月光茶人",R.drawable.shopcar, OneFragment.class) .addTabItem("优惠",R.drawable.shopcart_add_red, TwoFragment.class) .addTabItem("购物车",R.drawable.xx, ThreeFragment.class) .addTabItem("我的",R.drawable.shopcart_minus_red, FourFragment.class) .isShowDivider(true); } }//MainActivity布局
<com.hjm.bottomtabbar.BottomTabBar android:id="@+id/bottom_tab_bar" android:layout_width="match_parent" android:layout_height="match_parent" > </com.hjm.bottomtabbar.BottomTabBar>//FragmentOne展示数据//依赖public class OneFragment extends Fragment { private RecyclerView mRv; ServerApi serverApi; RecAdapter adapter; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View inflate = View.inflate(getContext(), R.layout.onefragmentlayout, null); mRv=inflate.findViewById(R.id.rv); mRv.setLayoutManager(new LinearLayoutManager(getActivity())); //创建ServerApi对象 serverApi = RetrofitHepler.getServerApi(); Call<LogBean> logBeanCall=serverApi.logBeanCall(); logBeanCall.enqueue(new Callback<LogBean>() { @Override public void onResponse(Call<LogBean> call, Response<LogBean> response) { //请求成功 final LogBean logBean = response.body(); getActivity().runOnUiThread(new Runnable() { @Override public void run() { //实例化适配器 adapter = new RecAdapter(logBean.getResults(), getActivity()); mRv.setAdapter(adapter); } }); } @Override public void onFailure(Call<LogBean> call, Throwable t) { } }); return inflate; } }//适配器public class RecAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { List<LogBean.ResultsBean> list; Context context; public RecAdapter(List<LogBean.ResultsBean> list, Context context) { this.list = list; this.context = context; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { //加载视图 View view=View.inflate(context, R.layout.iten_rec,null); return new MyViewHolder(view); } class MyViewHolder extends RecyclerView.ViewHolder { SimpleDraweeView img; TextView name; TextView msg; TextView time; public MyViewHolder(View itemView) { super(itemView); //初始化控件 img=itemView.findViewById(R.id.sdv); name=itemView.findViewById(R.id.name); msg=itemView.findViewById(R.id.msg); time=itemView.findViewById(R.id.times); } } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { LogBean.ResultsBean resultsBean=list.get(position); MyViewHolder myViewHolder= (MyViewHolder) holder; if(resultsBean.getImages()!=null) { Uri uri = Uri.parse("http://dynamic-image.yesky.com/740x-/uploadImages/2015/163/50/690V3VHW0P77.jpg"); DraweeController controller = Fresco.newDraweeControllerBuilder() .setUri(uri) .setAutoPlayAnimations(true) .build(); myViewHolder.img.setController(controller); } myViewHolder.name.setText(resultsBean.getType()); myViewHolder.msg.setText(resultsBean.getDesc()); myViewHolder.time.setText(resultsBean.getPublishedAt()+""); } @Override public int getItemCount() { return list.size(); } }//Fresc加载图片Apppublic class App extends Application { @Override public void onCreate() { super.onCreate(); Fresco.initialize(this); } }//item布局<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:fresco="http://schemas.android.com/apk/res-auto" 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"> <com.facebook.drawee.view.SimpleDraweeView android:layout_width="30dp" android:layout_height="30dp" android:id="@+id/sdv" fresco:placeholderImage="@mipmap/ic_launcher"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/name"/> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" android:id="@+id/msg"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/times"/> </LinearLayout>compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4' compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4' compile 'com.squareup.retrofit2:converter-scalars:2.0.0-beta4' compile 'org.greenrobot:greendao:3.2.0' compile 'org.greenrobot:eventbus:3.1.1' compile 'com.android.support:recyclerview-v7:26.0.0-alpha1' //butterknife依赖 compile 'com.jakewharton:butterknife:8.8.1' compile 'com.hjm:BottomTabBar:1.0.0' compile 'com.facebook.fresco:fresco:0.11.0'
封装Retrofit
最新推荐文章于 2024-05-22 23:07:14 发布