标签:int app server protect edr style over init instance
首先是依赖:
compile ‘com.squareup.retrofit2:retrofit:2.0.1‘ compile ‘com.squareup.retrofit2:converter-gson:2.0.1‘ compile ‘com.squareup.retrofit2:adapter-rxjava:2.0.1‘ compile ‘io.reactivex:rxandroid:1.1.0‘ compile ‘com.squareup.okhttp3:logging-interceptor:3.4.1‘ compile ‘com.android.support:mediarouter-v7:25.0.0‘ compile ‘com.android.support:recyclerview-v7:25.0.0‘ compile ‘com.nostra13.universalimageloader:universal-image-loader:1.9.5‘ compile ‘com.facebook.fresco:fresco:0.14.1‘ compile ‘com.facebook.fresco:animated-base-support:0.14.1‘ compile ‘com.facebook.fresco:animated-gif:0.14.1‘ compile ‘com.facebook.fresco:webpsupport:0.14.1‘ compile ‘com.facebook.fresco:animated-webp:0.14.1‘ compile ‘com.facebook.fresco:imagepipeline-okhttp3:0.14.1‘
然后是权限:
<uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
然后需要定义一个Apiservice接口(get里面写分类识别):
@GET("type/1/p/1") Observable<Home> gethomes();
然后是HomeAdapter类(这个是Recycleview的适配器,如果不明白,可以找找本人的随笔哦)
public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> { List<Home.DataBean> mDatas; Context context; public HomeAdapter(List<Home.DataBean> mDatas, Context context) { this.mDatas = mDatas; this.context = context; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { MyViewHolder holder = new MyViewHolder(LayoutInflater.from( context).inflate(R.layout.item_home, parent, false)); return holder; } @Override public int getItemCount() { return mDatas.size(); } class MyViewHolder extends RecyclerView.ViewHolder { TextView tv; SimpleDraweeView img; public MyViewHolder(View view) { super(view); tv = (TextView) view.findViewById(R.id.id_num); img= (SimpleDraweeView) view.findViewById(R.id.main_simple_drawee_view); } } public interface OnItemClickLitener { void onItemClick(View view, int position); void onItemLongClick(View view , int position); } private OnItemClickLitener mOnItemClickLitener; public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) { this.mOnItemClickLitener = mOnItemClickLitener; } @Override public void onBindViewHolder(final MyViewHolder holder, final int position) { holder.tv.setText(mDatas.get(position).getNews_title()); String url = mDatas.get(position).getPic_url(); if (url.equals("")||url==null){ holder.img.setImageResource(R.mipmap.ic_launcher); }else { Uri uri = Uri.parse(url); DraweeController controller = Fresco.newDraweeControllerBuilder() .setUri(uri) .setAutoPlayAnimations(true) .build(); holder.img.setController(controller); } // 如果设置了回调,则设置点击事件 if (mOnItemClickLitener != null) { holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int pos = holder.getLayoutPosition(); mOnItemClickLitener.onItemClick(holder.itemView, pos); } }); holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { int pos = holder.getLayoutPosition(); mOnItemClickLitener.onItemLongClick(holder.itemView, pos); return false; } }); } } }
API中放你的接口:
public class API { public static final String HOME_URL="http://api.expoon.com/AppNews/getNewsList/"; }
MainActivity
public class MainActivity extends AppCompatActivity { RecyclerView mRecyclerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mRecyclerView.setItemAnimator(new DefaultItemAnimator()); getmadata(); } public void getmadata() { Retrofit retrofit = new Retrofit.Builder().baseUrl(API.HOME_URL).addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build(); Apiservice apiservice = retrofit.create(Apiservice.class); Observable<Home> homes = apiservice.gethomes(); homes.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<Home>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(Home home) { List<Home.DataBean> data = home.getData(); mRecyclerView.setAdapter(new HomeAdapter(data,MainActivity.this)); } }); } }
MyApp配置fresco:
public class MyApp extends Application { public static MyApp mInstance; @Override public void onCreate() { super.onCreate(); Fresco.initialize(this); mInstance = this; } public static MyApp getInstance() { return mInstance; } }
然后就是布局了,首先是主布局(把recycleview写上):
<android.support.v7.widget.RecyclerView android:id="@+id/id_recyclerview" android:divider="#ffff0000" android:dividerHeight="10dp" android:layout_width="match_parent" android:layout_height="match_parent" />
然后好需要一个条目布局item_home:
<com.facebook.drawee.view.SimpleDraweeView android:layout_width="100dp" android:layout_height="100dp" fresco:roundAsCircle="true" fresco:roundingBorderColor="@android:color/holo_blue_light" fresco:roundingBorderWidth="5dp" android:id="@+id/main_simple_drawee_view" fresco:placeholderImage="@mipmap/ic_launcher"/> <TextView android:id="@+id/id_num" android:layout_below="@id/main_simple_drawee_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="1" />
这样就写好了
使用 RxJava+Retrofit进行数据请求,并放在RecycleView中使用fresco加载图片
标签:int app server protect edr style over init instance
原文地址:http://www.cnblogs.com/cainiaodongdong/p/7783608.html