MVP+ Retrofit+RxJava+XRecyclerView+TabLayout+Fresco结合使用


  MVP+ Retrofit+RxJava+XRecyclerView+TabLayout+Fresco结合使用

 最后运行结果

MVP+ Retrofit+RxJava+XRecyclerView+TabLayout+Fresco结合使用_第1张图片


  添加一些依赖

compile'com.squareup.retrofit2:retrofit:2.0.0-beta4'
    compile'com.squareup.retrofit2:converter-gson:2.0.0-beta4'
    compile 'com.squareup.okhttp3:okhttp:3.1.2'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta4'
    compile 'com.facebook.fresco:fresco:0.11.0'
    compile 'com.jcodecraeer:xrecyclerview:1.3.2'
    compile 'com.android.support:design:26.+'
    compile 'io.reactivex:rxjava:1.1.0'
    compile 'io.reactivex:rxandroid:1.1.0'
    compile 'com.google.code.gson:gson:2.6.2'

 网络权限

 

          android:name=".MyApp"

  Model层

public interface IshowModel {
    public void ShowData(int start, int count, Observer observer);

}

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class ShowModel implements IshowModel {

    @Override
    public void ShowData(int start, int count, Observer observer) {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("https://api.douban.com/v2/movie/")
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        IGetRequest result=retrofit.create(IGetRequest.class);
        result.getMovie(start,count)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
}

View层

public interface IshowView {
    public void GetData(List listb);
    public int getStart();
    public int getCount();
}

Presenter层

public class Presenter {
    Context context;
    List listb;
    IshowView view;
    IshowModel model;
    SoftReference srf;
    public Presenter(Context context,IshowView view){
        this.context=context;
        this.view=view;
        model=new ShowModel();

}

    public void showData(){
        int count = view.getCount();
        int start = view.getStart();
        model.ShowData(start, count, new Observer() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Bean b) {
                listb=b.getSubjects();
                view.GetData(listb);
            }
        });
    }
}

  @GET注解
public interface IGetRequest {
    @GET("top250")
    Observable getMovie(@Query("start") int start, @Query("count") int count);
}

   Application
public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Fresco.initialize(this);
    }
}

    MainActivity
import android.support.annotation.IdRes;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends FragmentActivity {
   private TabLayout mTab;
        private ViewPager mVp;
        private RadioButton mRb1;
        private RadioButton mRb2;
        private RadioButton mRb3;
        private RadioButton mRb4;
        private RadioButton mRb5;
        private RadioGroup mRg;
        private List list;
        String[] title={"泸深","指数","板块","商品","港股"};
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_main);
            initView();
            addFragment();

            MyViewPager  adapter=new  MyViewPager (getSupportFragmentManager());
            mVp.setAdapter(adapter);
            //设置可以滑动
            mTab.setTabMode(TabLayout.MODE_SCROLLABLE);
            mTab.setupWithViewPager(mVp);
            mRg.check(R.id.rb1);
            mRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                    switch (i){
                        case R.id.rb1:
                            mVp.setCurrentItem(0);
                            break;
                        case R.id.rb2:
                            mVp.setCurrentItem(1);
                            break;
                        case R.id.rb3:
                            mVp.setCurrentItem(2);
                            break;
                        case R.id.rb4:
                            mVp.setCurrentItem(3);
                            break;

                        case R.id.rb5:
                            mVp.setCurrentItem(4);
                            break;
                        default:
                            break;
                    }
                }
            });

        }
        private void initView() {
            mTab = (TabLayout) findViewById(R.id.tab);
            mVp = (ViewPager) findViewById(R.id.vp);
            mRb1 = (RadioButton) findViewById(R.id.rb1);
            mRb2 = (RadioButton) findViewById(R.id.rb2);
            mRb3 = (RadioButton) findViewById(R.id.rb3);
            mRb4 = (RadioButton) findViewById(R.id.rb4);
            mRb5 = (RadioButton) findViewById(R.id.rb5);
            mRg = (RadioGroup) findViewById(R.id.rg);
        }
        private void addFragment() {
            list = new ArrayList<>();
            list.add(new FragmentOne());
            list.add(new Fragment_hangqing());
            list.add(new Fragment_shangpin());
            list.add(new Fragment_zixun());
            list.add(new Fragment_jiaoyi());
        }
      ViewPager适配器
        class  MyViewPager extends FragmentPagerAdapter {


            public MyViewPager(FragmentManager fm) {
                super(fm);
            }

            @Override
            public Fragment getItem(int position) {
                return list.get(position);
            }

            @Override
            public int getCount() {
                return list.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return title[position];
            }
        }
    }

    

     FragmentOne

  import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
 import com.facebook.drawee.backends.pipeline.Fresco;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import java.util.ArrayList;
import java.util.List;

 public class FragmentOne extends Fragment implements IshowView{
    private XRecyclerView xrv;
    View view;
    List listb;
    int start=0;

    int count=10;
    private Presenter presenter;

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = View.inflate(getActivity(), R.layout.fragone, null);
        initView();
//        showData();
        presenter = new Presenter(getActivity(),this);
        presenter.showData();
        showupdate();
        return view;
    }

    private void showupdate() {
        xrv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                start++;
                presenter.showData();
                xrv.refreshComplete();
            }

            @Override
            public void onLoadMore() {
                start++;
                presenter.showData();
                
                xrv.loadMoreComplete();
            }
        });
    }

    private void initView() {
        xrv = (XRecyclerView) view.findViewById(R.id.xrv);
        LinearLayoutManager llm=new LinearLayoutManager(getActivity());
        xrv.setLayoutManager(llm);
        listb=new ArrayList<>();
    }


    @Override
    public void GetData(List listb) {
        MyAdapter myAdapter = new MyAdapter(getActivity(),listb);
        xrv.setAdapter(myAdapter);
    }

    @Override
    public int getStart() {
        return start;
    }

    @Override
    public int getCount() {
        return count;
    }


}

    Adapter适配器

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
 import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import java.util.List;
public class MyAdapter extends XRecyclerView.Adapter {
    Context context;
    List listb;
    public MyAdapter(Context context,List listb){
        this.context=context;
        this.listb=listb;
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=View.inflate(context, R.layout.showitem,null);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.tv1.setText(listb.get(position).getTitle());
        holder.tv2.setText(listb.get(position).getYear());
        DraweeController controller= Fresco.newDraweeControllerBuilder()
                .setUri(listb.get(position).getImages().getLarge())
                .setAutoPlayAnimations(true)
                .build();
        holder.sdv.setController(controller);
    }

    @Override
    public int getItemCount() {
        return listb==null?0:listb.size();
    }

    class MyViewHolder extends XRecyclerView.ViewHolder{
        SimpleDraweeView sdv;
        TextView tv1;
        TextView tv2;
        public MyViewHolder(View itemView) {
            super(itemView);
            sdv=itemView.findViewById(R.id.sdv);
            tv1=itemView.findViewById(R.id.tvtop);
            tv2=itemView.findViewById(R.id.tvbottom);
        }
    }
}

 drawable里的selector样式




   

   Layout布局

activity—main


    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
   >

            android:id="@+id/tab"
        android:layout_width="match_parent"
        android:layout_height="40dp">
            android:id="@+id/vp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/tab"
        >
            android:id="@+id/rg"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        >
                    android:id="@+id/rb1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="首页"
            android:button="@null"
            android:gravity="center"
            android:background="@drawable/selecor"
            />
                    android:id="@+id/rb2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="自选"
            android:button="@null"
            android:gravity="center"
            android:background="@drawable/selecor"
            />
                    android:id="@+id/rb3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="行情"
            android:button="@null"
            android:gravity="center"
            android:background="@drawable/selecor"
            />

                    android:id="@+id/rb4"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="资讯"
            android:button="@null"
            android:gravity="center"
            android:background="@drawable/selecor"
            />
                    android:id="@+id/rb5"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="交易"
            android:button="@null"
            android:gravity="center"
            android:background="@drawable/selecor"
            />
   

 fragmentOne


    android:layout_width="match_parent"
    android:gravity="center"
    android:layout_height="match_parent">
          android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:id="@+id/xrv">

  


item


    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">
            android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/sdv"/>
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:orientation="vertical"
    >
            android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tvtop"
        android:textSize="20sp"/>
            android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tvbottom"
        android:textSize="15sp"/>


Bean类

public class Bean {

    /**
     * count : 10
     * start : 0
     * total : 250
   
     * title : 豆瓣电影Top250
     */

    private int count;
    private int start;
    private int total;
    private String title;
    private List subjects;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public List getSubjects() {
        return subjects;
    }

    public void setSubjects(List subjects) {
        this.subjects = subjects;
    }

    public static class SubjectsBean {
        /**
         * rating : {"max":10,"average":9.6,"stars":"50","min":0}
         * genres : ["犯罪","剧情"]
         * title : 肖申克的救赎
         * casts : [{"alt":"https://movie.douban.com/celebrity/1054521/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p17525.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p17525.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p17525.jpg"},"name":"蒂姆·罗宾斯","id":"1054521"},{"alt":"https://movie.douban.com/celebrity/1054534/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p34642.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p34642.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p34642.jpg"},"name":"摩根·弗里曼","id":"1054534"},{"alt":"https://movie.douban.com/celebrity/1041179/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p5837.jpg","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p5837.jpg","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p5837.jpg"},"name":"鲍勃·冈顿","id":"1041179"}]
         * collect_count : 1147356
         * original_title : The Shawshank Redemption
         * subtype : movie
         * directors : [{"alt":"https://movie.douban.com/celebrity/1047973/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p230.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p230.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p230.jpg"},"name":"弗兰克·德拉邦特","id":"1047973"}]
         * year : 1994
         * images : {"small":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p480747492.jpg","large":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p480747492.jpg","medium":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p480747492.jpg"}
         * alt : https://movie.douban.com/subject/1292052/
         * id : 1292052
         */

        private RatingBean rating;
        private String title;
        private int collect_count;
        private String original_title;
        private String subtype;
        private String year;
        private ImagesBean images;
        private String alt;
        private String id;
        private List genres;
        private List casts;
        private List directors;

        public RatingBean getRating() {
            return rating;
        }

        public void setRating(RatingBean rating) {
            this.rating = rating;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public int getCollect_count() {
            return collect_count;
        }

        public void setCollect_count(int collect_count) {
            this.collect_count = collect_count;
        }

        public String getOriginal_title() {
            return original_title;
        }

        public void setOriginal_title(String original_title) {
            this.original_title = original_title;
        }

        public String getSubtype() {
            return subtype;
        }

        public void setSubtype(String subtype) {
            this.subtype = subtype;
        }

        public String getYear() {
            return year;
        }

        public void setYear(String year) {
            this.year = year;
        }

        public ImagesBean getImages() {
            return images;
        }

        public void setImages(ImagesBean images) {
            this.images = images;
        }

        public String getAlt() {
            return alt;
        }

        public void setAlt(String alt) {
            this.alt = alt;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public List getGenres() {
            return genres;
        }

        public void setGenres(List genres) {
            this.genres = genres;
        }

        public List getCasts() {
            return casts;
        }

        public void setCasts(List casts) {
            this.casts = casts;
        }

        public List getDirectors() {
            return directors;
        }

        public void setDirectors(List directors) {
            this.directors = directors;
        }

        public static class RatingBean {
            /**
             * max : 10
             * average : 9.6
             * stars : 50
             * min : 0
             */

            private int max;
            private double average;
            private String stars;
            private int min;

            public int getMax() {
                return max;
            }

            public void setMax(int max) {
                this.max = max;
            }

            public double getAverage() {
                return average;
            }

            public void setAverage(double average) {
                this.average = average;
            }

            public String getStars() {
                return stars;
            }

            public void setStars(String stars) {
                this.stars = stars;
            }

            public int getMin() {
                return min;
            }

            public void setMin(int min) {
                this.min = min;
            }
        }

        public static class ImagesBean {
            /**
             * small : https://img3.doubanio.com/view/photo/s_ratio_poster/public/p480747492.jpg
             * large : https://img3.doubanio.com/view/photo/s_ratio_poster/public/p480747492.jpg
             * medium : https://img3.doubanio.com/view/photo/s_ratio_poster/public/p480747492.jpg
             */

            private String small;
            private String large;
            private String medium;

            public String getSmall() {
                return small;
            }

            public void setSmall(String small) {
                this.small = small;
            }

            public String getLarge() {
                return large;
            }

            public void setLarge(String large) {
                this.large = large;
            }

            public String getMedium() {
                return medium;
            }

            public void setMedium(String medium) {
                this.medium = medium;
            }
        }

        public static class CastsBean {
            /**
             * alt : https://movie.douban.com/celebrity/1054521/
             * avatars : {"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p17525.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p17525.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p17525.jpg"}
             * name : 蒂姆·罗宾斯
             * id : 1054521
             */

            private String alt;
            private AvatarsBean avatars;
            private String name;
            private String id;

            public String getAlt() {
                return alt;
            }

            public void setAlt(String alt) {
                this.alt = alt;
            }

            public AvatarsBean getAvatars() {
                return avatars;
            }

            public void setAvatars(AvatarsBean avatars) {
                this.avatars = avatars;
            }

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }

            public String getId() {
                return id;
            }

            public void setId(String id) {
                this.id = id;
            }

            public static class AvatarsBean {
                /**
                 * small : https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p17525.jpg
                 * large : https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p17525.jpg
                 * medium : https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p17525.jpg
                 */

                private String small;
                private String large;
                private String medium;

                public String getSmall() {
                    return small;
                }

                public void setSmall(String small) {
                    this.small = small;
                }

                public String getLarge() {
                    return large;
                }

                public void setLarge(String large) {
                    this.large = large;
                }

                public String getMedium() {
                    return medium;
                }

                public void setMedium(String medium) {
                    this.medium = medium;
                }
            }
        }

        public static class DirectorsBean {
            /**
             * alt : https://movie.douban.com/celebrity/1047973/
             * avatars : {"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p230.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p230.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p230.jpg"}
             * name : 弗兰克·德拉邦特
             * id : 1047973
             */

            private String alt;
            private AvatarsBeanX avatars;
            private String name;
            private String id;

            public String getAlt() {
                return alt;
            }

            public void setAlt(String alt) {
                this.alt = alt;
            }

            public AvatarsBeanX getAvatars() {
                return avatars;
            }

            public void setAvatars(AvatarsBeanX avatars) {
                this.avatars = avatars;
            }

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }

            public String getId() {
                return id;
            }

            public void setId(String id) {
                this.id = id;
            }

            public static class AvatarsBeanX {
                /**
                 * small : https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p230.jpg
                 * large : https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p230.jpg
                 * medium : https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p230.jpg
                 */

                private String small;
                private String large;
                private String medium;

                public String getSmall() {
                    return small;
                }

                public void setSmall(String small) {
                    this.small = small;
                }

                public String getLarge() {
                    return large;
                }

                public void setLarge(String large) {
                    this.large = large;
                }

                public String getMedium() {
                    return medium;
                }

                public void setMedium(String medium) {
                    this.medium = medium;
                }
            }
        }
    }
}






你可能感兴趣的:(MVP+ Retrofit+RxJava+XRecyclerView+TabLayout+Fresco结合使用)