Use Rxjava+retrofit for data requests and place them in Recycleview to load pictures using fresco

Source: Internet
Author: User
Tags recyclerview android

The first is dependency:

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'

Then the permissions:

<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>

Then you need to define a Apiservice interface (get inside write classification recognition):

@GET ("type/1/p/1")    Observable<Home> gethomes ();
Then is the Homeadapter class (this is the Recycleview adapter, if not understand, you can find my essay OH)
 Public classHomeadapter extends Recyclerview.adapter{List<Home.DataBean>Mdatas;    Context context;  PublicHomeadapter (listMdatas, Context context) {         This. Mdatas =Mdatas;  This. Context =context; } @Override PublicMyviewholder Oncreateviewholder (viewgroup parent,intViewType) {Myviewholder Holder=NewMyviewholder (Layoutinflater. from(context). Inflate (R.layout.item_home, parent,false)); returnHolder; } @Override Public intGetItemCount () {returnmdatas.size (); }    classMyviewholder extends Recyclerview.viewholder {TextView TV;        Simpledraweeview img;  PublicMyviewholder (view view) {super (view); TV=(TextView) View.findviewbyid (r.id.id_num); IMG=(Simpledraweeview) View.findviewbyid (R.id.main_simple_drawee_view); }    }     Public InterfaceOnitemclicklitener {voidOnitemclick (View view,intposition); voidOnitemlongclick (View view,intposition); }    PrivateOnitemclicklitener Monitemclicklitener;  Public voidSetonitemclicklitener (Onitemclicklitener monitemclicklitener) { This. Monitemclicklitener =Monitemclicklitener; } @Override Public voidOnbindviewholder (final Myviewholder holder, finalintposition) {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); }        //Set the Click event if a callback is set        if(Monitemclicklitener! =NULL) {Holder.itemView.setOnClickListener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {intpos =holder.getlayoutposition ();                Monitemclicklitener.onitemclick (Holder.itemview, POS);            }            }); Holder.itemView.setOnLongClickListener (NewView.onlongclicklistener () {@Override Publicboolean Onlongclick (View v) {intpos =holder.getlayoutposition ();                    Monitemclicklitener.onitemlongclick (Holder.itemview, POS); return false;        }            }); }    }}
Put your interface in the API:
 Public class API {    publicstatic final String home_url="http://api.expoon.com/ appnews/getnewslist/";}
Mainactivity
 Public classMainactivity extends Appcompatactivity {Recyclerview mrecyclerview; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Mrecyclerview=(Recyclerview) Findviewbyid (R.id.id_recyclerview); Mrecyclerview.setlayoutmanager (NewLinearlayoutmanager ( This)); Mrecyclerview.setitemanimator (Newdefaultitemanimator ());    Getmadata (); }     Public voidGetmadata () {Retrofit Retrofit=NewRetrofit.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 (NewObserver() {@Override Public voidoncompleted () {} @Override Public voidOnError (Throwable e) {} @Override Public voidOnNext (home home) {List<Home.DataBean> data =Home.getdata (); Mrecyclerview.setadapter (NewHomeadapter (data,mainactivity. This));    }                }); }}
 myapp configuration fresco: 
 public  class   MyApp extends application { public  static   MyApp minstance; @Override  public  void   OnCreate () {super.oncreate ();        Fresco.initialize ( this   = this  ;   " public   Static   MyApp getinstance () { return      minstance; }}

The

is then the layout, first the main layout (write the 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  " /> 
And then you need an entry layout 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"/>

So it's written.

Use Rxjava+retrofit for data requests and place them in Recycleview to load pictures using fresco

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.