MVP+Rxjava+Okhttp+Retrofit+XRecyclerview请求数据

      效果展示如下:

           

//导入的依赖如下,记得在manifest中加权限:
   // xrecyclerview的依赖
    compile 'com.jcodecraeer:xrecyclerview:1.3.2'

// fresco
    compile 'com.facebook.fresco:fresco:0.12.0'

// Retrofit库
    compile 'com.squareup.retrofit2:retrofit:2.0.1'
// Okhttp库
    compile 'com.squareup.okhttp3:okhttp:3.1.2'

// rxjava+rxandroid+retrofit2+okhttp
    compile 'io.reactivex:rxjava:1.0.14'
    compile 'io.reactivex:rxandroid:1.0.1'
    compile 'com.squareup.retrofit2:converter-gson:2.0.1'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.0.1'
    compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'


//布局如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
    android:layout_width="match_parent" android:layout_height="match_parent"
>

<com.jcodecraeer.xrecyclerview.XRecyclerView
    android:id="@+id/xr"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"></com.jcodecraeer.xrecyclerview.XRecyclerView>
</RelativeLayout>
 
 
 //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:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/recy_item_img"
        android:layout_width="100dp"
        android:layout_height="100dp"
        fresco:placeholderImage="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/recy_item_tv"
        android:layout_width="wrap_content"
        android:textSize="25sp"
        android:layout_marginTop="20dp"
        android:textColor="@color/colorAccent"
        android:layout_height="wrap_content"
        android:text="标题" />
</LinearLayout>



//定义一个Api:
public class API {
    //http://api.expoon.com/AppNews/getNewsList/type/1/p/1
    public static final String URL = "http://api.expoon.com";
}
 
 
 
 //定义一个接口ApiService: 
public interface ApiService {
    @GET("/AppNews/getNewsList/type/1/p/1")
    Observable<NewsBean> getData();
}

//定义一个app:

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

 
 
//这里Bean就不写了,接口可以另换
 
 
//定义一个Imodel接口:
public interface IMOdel {
    // 处理数据的方法
    void GetData();

}

//定义一个UserModel类实现接口
public class UserModel implements IMOdel{

  // 创建集合 保存请求下来的数据
    ArrayList<NewsBean.DataBean> lists;

    private OnFinish onFinish;

    public UserModel(OnFinish onFinish) {
        this.onFinish = onFinish;
    }

    // 创建接口
    public interface OnFinish{
        void onFinishLinisher(ArrayList<NewsBean.DataBean> lists);
    }

    @Override
    public void GetData() {
        lists = new ArrayList<>();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(API.URL)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        // 通过动态代理获得网络接口数据
        ApiService apiService = retrofit.create(ApiService.class);
        Observable<NewsBean> data = apiService.getData();
        // 设置请求数据时候的线程
        data.subscribeOn(Schedulers.io())
                // 设置请求结束后的线程
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NewsBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(NewsBean newsBean) {
                        Log.d("main2", "onNext: " + newsBean.getData() + "----");
                        lists = (ArrayList<NewsBean.DataBean>) newsBean.getData();
                        onFinish.onFinishLinisher(lists);
                    }
                });
    }
}

//定义一个p类进行v和m的交互
public class UserPresenter implements UserModel.OnFinish{
    private final Iview iview;
    private final UserModel userModel;

    public UserPresenter(Iview iview) {
        this.iview = iview;
        userModel = new UserModel(this);
    }

    // 处理model与view的逻辑
    public void getData(){
        userModel.GetData();
    }

    @Override
    public void onFinishLinisher(ArrayList<NewsBean.DataBean> lists) {
        iview.ShowData(lists);
    }
}


//定义一个Iview的接口:
public interface Iview {
    // 展示数据的方法
    void ShowData(ArrayList<NewsBean.DataBean> lists);
}
 
 
//定义一个adapter,将数据和图片优化,用recyclerview展示
public class MyXrecyclerviewAdapter extends RecyclerView.Adapter<MyXrecyclerviewAdapter.MyViewHolder> {
    // 数据源
    ArrayList<NewsBean.DataBean> lists;
    // 上下文
    Context context;

    public MyXrecyclerviewAdapter(ArrayList<NewsBean.DataBean> lists, Context context) {
        this.lists = lists;
        this.context = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.recy_item, parent,
                false));
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position)
    {
        holder.tv.setText(lists.get(position).getNews_title());
        Uri uri = Uri.parse(lists.get(position).getPic_url());
        holder.img.setImageURI(uri);
    }

    @Override
    public int getItemCount()
    {
        return lists.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder
    {

        TextView tv;
        SimpleDraweeView img;

        public MyViewHolder(View view)
        {
            super(view);
            tv =  view.findViewById(R.id.recy_item_tv);
            img = view.findViewById(R.id.recy_item_img);
        }
    }
}
//最后,在mainActivity实现代码:

public class MainActivity extends AppCompatActivity implements Iview{
    XRecyclerView xr;
    UserPresenter presenter;
    MyXrecyclerviewAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化
        init();

    }

    private void init() {
        xr = (XRecyclerView) findViewById(R.id.xr);

        // 初始化数据
        initData();

        // 上拉刷新 下拉加载
        xr.setLoadingMoreEnabled(true);
        xr.setPullRefreshEnabled(true);

        // 监听事件就不做了 具体需求自定义即可
        xr.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                xr.refreshComplete();
                Toast.makeText(MainActivity.this,"下拉刷新",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onLoadMore() {
                xr.loadMoreComplete();
                Toast.makeText(MainActivity.this,"上拉加载",Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initData() {
        presenter = new UserPresenter((Iview) this);
        presenter.getData();
    }

    @Override
    public void ShowData(ArrayList<NewsBean.DataBean> lists) {
        xr.setLayoutManager(new LinearLayoutManager(this));
        xr.setAdapter(adapter = new MyXrecyclerviewAdapter(lists,this));
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值