Mvp+rxjava+recyclerview implement all the files in the SD card root directory to load and display the photos

Source: Internet
Author: User

Beginner Rxjava, currently can only traverse the loading of all folders in the specified directory of photos, folders in the folder if there are also nested folders currently do not find the implementation method.

First look at the MVP directory structure:

I'm sorry, there is no model.

Next is the interface code of the view layer and the interface Code of the presenter layer.

 Public Interface Iimgsview {    void onimgsloadcompleted (arraylist<string> IMGs);}  Public Interface iimgspresenter {    void  getimages (file[] files);

Then is the activity of the picture display, implements Iimgsview interface, Inside holds presenter object

 PackageCom.create.qdocumentimtest.rxjavatest.view;ImportAndroid.os.Bundle;Importandroid.os.Environment;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.support.v7.widget.RecyclerView;ImportAndroid.support.v7.widget.StaggeredGridLayoutManager;ImportCOM.CREATE.QDOCUMENTIMTEST.R;ImportCom.create.qdocumentimtest.rxjavatest.adapter.MyRecyRxAdapter;ImportCom.create.qdocumentimtest.rxjavatest.presenter.IImgsPresenter;ImportCom.create.qdocumentimtest.rxjavatest.presenter.ImgPresenterCompl;ImportJava.io.File;Importjava.util.ArrayList;ImportButterknife. Butterknife;ImportButterknife. Injectview;/*** Function: Use MVP Rxjava to complete the loading of images from all subdirectories in the SD card root directory, and display them in waterfall stream in Recyclerview. */ Public classThreeactivityextendsAppcompatactivityImplementsIimgsview {@InjectView (r.id.recyclerview) Recyclerview Recyclerview; PrivateString BasePath; PrivateIimgspresenter Imgspresenter; PrivateArraylist<string> IMGs =NewArraylist<>(); @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_three); Butterknife.inject ( This); Imgspresenter=NewImgpresentercompl ( This); BasePath=environment.getexternalstoragedirectory (). GetPath (); File File=NewFile (BasePath); file[] Files=File.listfiles ();    Imgspresenter.getimages (files); } @Override Public voidOnimgsloadcompleted (arraylist<string>IMGs) {//presenter callback Recyclerview.setlayoutmanager after loading the resource (NewStaggeredgridlayoutmanager (3, staggeredgridlayoutmanager.vertical)); Myrecyrxadapter Adapter=NewMyrecyrxadapter ( This, IMGs);    Recyclerview.setadapter (adapter); }}

Next is the presenter implementation class, which is responsible for handling the main logic of the code.

 PackageCom.create.qdocumentimtest.rxjavatest.presenter;ImportAndroid.util.Log;ImportCom.create.qdocumentimtest.rxjavatest.view.IImgsView;ImportJava.io.File;Importjava.util.ArrayList;ImportRx. Observable;ImportRx. Subscriber;Importrx.android.schedulers.AndroidSchedulers;Importrx.functions.Func1;Importrx.schedulers.Schedulers;/*** Created by Administrator on 2016/6/8 0008.*/ Public classImgpresentercomplImplementsIimgspresenter {PrivateIimgsview Imgsview; PrivateArraylist<string> imgs=NewArraylist<>();  PublicImgpresentercompl (Iimgsview imgsview) { This. imgsview=Imgsview; } @Override Public voidgetimages (file[] files) {observable.from (Files). Filter (NewFunc1<file, boolean> () {//filter folders, select only folders, do not select other files (even if there are pictures can not be selected)@Override PublicBoolean Call (file file) {returnfile.isdirectory (); }}). FlatMap (NewFunc1<file, observable<file>>() {@Override PublicObservable<file> Call (file file) {//Traverse Folder                        returnObservable.from (File.listfiles ()); }}). filter (NewFunc1<file, boolean> () {//filter images, images in level Two folders cannot be found@Override PublicBoolean Call (file file) {returnFile.getname (). EndsWith (". png") | | File.getname (). EndsWith (". jpg") | | File.getname (). EndsWith (". jpeg"); }}). map (NewFunc1<file, string>() {@Override PublicString Call (file file) {returnFile.getpath ();                  }}). Subscribeon (Schedulers.io ()). Observeon (Androidschedulers.mainthread ()) . Subscribe (NewSubscriber<string>() {@Override Public voidOnCompleted () {//OnNext () call after execution finishesLOG.E ("img", "Done");                     Imgsview.onimgsloadcompleted (IMGs); } @Override Public voidOnError (Throwable e) {} @Override Public voidOnNext (String s) {LOG.E ("IMG", s);                     Imgs.add (s);    }                 }); }}

The following is the adapter of Recyclerview

 PackageCom.create.qdocumentimtest.rxjavatest.adapter;ImportAndroid.content.Context;ImportAndroid.support.v7.widget.RecyclerView;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.ImageView;Importandroid.widget.LinearLayout;Importcom.bumptech.glide.Glide;ImportCOM.CREATE.QDOCUMENTIMTEST.R;Importcom.create.qdocumentimtest.rxjavatest.utils.Dp2pxUtils;Importcom.create.qdocumentimtest.rxjavatest.utils.ImgUtils;ImportCom.create.qdocumentimtest.rxjavatest.utils.ScreenUtil;Importjava.util.ArrayList;Importjava.util.List;ImportButterknife. Butterknife;ImportButterknife. Injectview;/*** Created by Administrator on 2016/6/8 0008.*/ Public classMyrecyrxadapterextendsRecyclerview.adapter<myrecyrxadapter.myviewholder>{    Privatecontext Context; List<String>IMGs;  PublicMyrecyrxadapter (context context, arraylist<string>IMGs) {         This. Context =context;  This. imgs=IMGs; } @Override PublicMyviewholder Oncreateviewholder (viewgroup parent,intViewType) {View Itemview=view.inflate (context, R.layout.item_recycle_rxjava_layout,NULL); Myviewholder VH=NewMyviewholder (Itemview); returnVH; } @Override Public voidOnbindviewholder (Myviewholder holder,intposition) {        intShowimageheight =getshowimageheight (position); Linearlayout.layoutparams params=(Linearlayout.layoutparams) holder.imageView.getLayoutParams (); Params.height=Showimageheight;        Holder.imageView.setLayoutParams (params);        Glide.with (context). Load (imgs.get (position)). into (Holder.imageview); //Height Setting    }    /*** Get Entry Height *@paramposition *@return     */    Private intGetshowimageheight (intposition) {        intWidth=screenutil.getscreenwidth (context); intheight=screenutil.getscreenhight (context); intpx= dp2pxutils.dp2px (Context, 6); intViewwidth = WIDTH/3-px; intOriginalWidth =Imgutils.getimgoriginalwidth (imgs.get (position)); intOriginalHeight =Imgutils.getimgoriginalheight (imgs.get (position)); returnOriginalHeight * Viewwidth/OriginalWidth; } @Override Public intGetItemCount () {returnimgs.size (); }    classMyviewholderextendsrecyclerview.viewholder{ImageView ImageView;  PublicMyviewholder (View itemview) {Super(Itemview); ImageView=(ImageView) Itemview.findviewbyid (r.id.image); }    }}

Mvp+rxjava+recyclerview implement all the files in the SD card root directory to load and display the photos

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.