依赖:
compile 'com.squareup.okhttp3:okhttp:3.10.0' compile 'com.android.support:recyclerview-v7:26.1.0' //黄油刀 compile 'com.jakewharton:butterknife:8.8.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' //图片加载框架 compile 'com.facebook.fresco:fresco:0.12.0' //访问网络 implementation 'com.squareup.retrofit2:retrofit:2.4.0' compile 'com.squareup.retrofit2:converter-gson:2.4.0' //compile 'com.squareup.retrofit2:adapter-rxjava:2.0.2' compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0' //RxJava implementation 'io.reactivex.rxjava2:rxandroid:2.0.2' implementation 'io.reactivex.rxjava2:rxjava:2.1.12' compile 'com.github.userswlwork:pull-to-refresh:1.0.0' compile 'com.jcodecraeer:xrecyclerview:1.3.2' compile 'org.greenrobot:greendao:3.2.2' implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0' compile 'com.github.bumptech.glide:glide:3.7.0' //Eventbus compile 'org.greenrobot:eventbus:3.0.0' compile 'com.youth.banner:banner:1.4.10' //最新版本 compile 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-5' compile 'com.scwang.smartrefresh:SmartRefreshHeader:1.1.0-alpha-5'//没有使用特殊Header,可以不加这行 compile 'com.android.support:appcompat-v7:26.1.0'//版本 23以上(必须)权限
android:name="android.permission.INTERNET"/>
android:name="android.permission.READ_EXTERNAL_STORAGE"/>
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
utils
package com.example.slx.songliangxing428_orders.utils; import com.example.slx.songliangxing428_orders.http.ApiService; import com.example.slx.songliangxing428_orders.http.ServiceUrl; import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; import okhttp3.OkHttpClient; import okhttp3.logging.HttpLoggingInterceptor; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; /** * Created by lenovo on 2018/4/28. */ public class HttpNetUtils { private static volatile HttpNetUtils instance; private final Retrofit retrofit; public static HttpNetUtils getInstance(){ if(instance == null){ synchronized (HttpNetUtils.class){ if(null==instance){ instance=new HttpNetUtils(); } } } return instance; } private HttpNetUtils(){ HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(HttpLoggingInterceptor.Level.BASIC); OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(interceptor) .build(); retrofit = new Retrofit.Builder() .baseUrl(ServiceUrl.BASE_URL) .client(client) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); } //retrofit自定义的接口 public ApiService getService(){ return retrofit.create(ApiService.class); } }
http
package com.example.slx.songliangxing428_orders.http; import com.example.slx.songliangxing428_orders.bean.DataBean; import com.example.slx.songliangxing428_orders.bean.MessageBean; import io.reactivex.Flowable; import retrofit2.http.GET; import retrofit2.http.Query; /** * fetrofit的借口定义 * Created by lenovo on 2018/4/20. */ public interface ApiService { /** * 请求 * * @return */ @GET("getOrders") Flowable> getJavaData(@Query("uid") String uid,@Query("page")String page); }
package com.example.slx.songliangxing428_orders.http; /** * Created by lenovo on 2018/4/20. */ public class ServiceUrl { public static final String BASE_URL = "https://www.zhaoapi.cn/product/"; }
bean
public class MessageBean<T> { private String msg; private String code; private List<T> data; private String page;
public class DataBean { private String createtime; private String price; private String title; private int status;
inter
package com.example.slx.songliangxing428_orders.inter; import com.example.slx.songliangxing428_orders.bean.DataBean; import com.example.slx.songliangxing428_orders.bean.MessageBean; /** * Created by lenovo on 2018/4/28. */ public interface IPresenter<T> { void success(T t); void error(Throwable t); }
package com.example.slx.songliangxing428_orders.inter; import com.example.slx.songliangxing428_orders.bean.DataBean; import com.example.slx.songliangxing428_orders.bean.MessageBean; /** * Created by lenovo on 2018/4/28. */ public interface IView<T> { void success(T t); void error(Throwable t); }v
package com.example.slx.songliangxing428_orders; import android.os.Bundle; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import com.example.slx.songliangxing428_orders.adapter.MyAdapter; import com.example.slx.songliangxing428_orders.bean.DataBean; import com.example.slx.songliangxing428_orders.bean.MessageBean; import com.example.slx.songliangxing428_orders.inter.IPresenter; import com.example.slx.songliangxing428_orders.inter.IView; import com.example.slx.songliangxing428_orders.presenter.MyPresenter; import com.scwang.smartrefresh.layout.SmartRefreshLayout; import com.scwang.smartrefresh.layout.api.RefreshLayout; import com.scwang.smartrefresh.layout.constant.SpinnerStyle; import com.scwang.smartrefresh.layout.footer.BallPulseFooter; import com.scwang.smartrefresh.layout.header.BezierRadarHeader; import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener; import com.scwang.smartrefresh.layout.listener.OnRefreshListener; import java.util.ArrayList; import java.util.List; import butterknife.BindView; import butterknife.ButterKnife; public class MainActivity extends AppCompatActivity implements IView> { @BindView(R.id.rv) RecyclerView rv; @BindView(R.id.refreshLayout) SmartRefreshLayout refreshLayout; private MyAdapter adapter; private List list; private MyPresenter myPresenter; private String page="1"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); refreshLayout.setOnRefreshListener(new OnRefreshListener() { @Override public void onRefresh(RefreshLayout refreshlayout) { refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败 list.clear(); page = "1"; myPresenter.toNetWork("71",page); } }); refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() { @Override public void onLoadMore(RefreshLayout refreshlayout) { refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败 int i = Integer.parseInt(page); i++; String page = String.valueOf(i); myPresenter.toNetWork("71",page); } }); //设置 Header 为 贝塞尔雷达 样式 refreshLayout.setRefreshHeader(new BezierRadarHeader(this).setEnableHorizontalDrag(true)); //设置 Footer 为 球脉冲 样式 refreshLayout.setRefreshFooter(new BallPulseFooter(this).setSpinnerStyle(SpinnerStyle.Scale)); rv.setLayoutManager(new LinearLayoutManager(this)); //创建p层对象 myPresenter = new MyPresenter(this); myPresenter.toNetWork("71",page); list = new ArrayList<>(); adapter = new MyAdapter(MainActivity.this, list); rv.setAdapter(adapter); } @Override public void success(MessageBean dataBeanMessageBean) { list.addAll(dataBeanMessageBean.getData()); adapter.notifyDataSetChanged(); } @Override public void error(Throwable t) { } }
p
package com.example.slx.songliangxing428_orders.presenter; import com.example.slx.songliangxing428_orders.bean.DataBean; import com.example.slx.songliangxing428_orders.bean.MessageBean; import com.example.slx.songliangxing428_orders.inter.IPresenter; import com.example.slx.songliangxing428_orders.inter.IView; import com.example.slx.songliangxing428_orders.model.MyModel; /** * Created by lenovo on 2018/4/28. */ public class MyPresenter implements IPresenter>{ private final MyModel myModel; private IView iView; public MyPresenter(IView iView){ this.iView=iView; myModel = new MyModel(); } public void toNetWork(String uid,String page) { myModel.getData(uid,page,this); } @Override public void success(MessageBean dataBeanMessageBean) { iView.success(dataBeanMessageBean); } @Override public void error(Throwable t) { } }
m
package com.example.slx.songliangxing428_orders.model; import com.example.slx.songliangxing428_orders.bean.DataBean; import com.example.slx.songliangxing428_orders.bean.MessageBean; import com.example.slx.songliangxing428_orders.http.ApiService; import com.example.slx.songliangxing428_orders.inter.IPresenter; import com.example.slx.songliangxing428_orders.utils.HttpNetUtils; import io.reactivex.Flowable; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.schedulers.Schedulers; import io.reactivex.subscribers.DisposableSubscriber; /** * Created by lenovo on 2018/4/28. */ public class MyModel{ public void getData(String uid,String page, final IPresenter iPresenter) { HttpNetUtils instance = HttpNetUtils.getInstance(); ApiService service = instance.getService(); Flowable> javaData = service.getJavaData(uid,page); javaData.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new DisposableSubscriber >() { @Override public void onNext(MessageBean dataBeanMessageBean) { iPresenter.success(dataBeanMessageBean); } @Override public void onError(Throwable t) { } @Override public void onComplete() { } }); } }
适配器
package com.example.slx.songliangxing428_orders.adapter; import android.content.Context; import android.graphics.Color; import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; import com.example.slx.songliangxing428_orders.MainActivity; import com.example.slx.songliangxing428_orders.R; import com.example.slx.songliangxing428_orders.bean.DataBean; import java.util.List; /** * Created by lenovo on 2018/4/28. */ public class MyAdapter extends RecyclerView.Adapter{ private Context context; private Listlist; public MyAdapter(Context context, List list) { this.context = context; this.list = list; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(parent.getContext(), R.layout.itemlayout, null); //创建ViewHolder 把view通过构造方法传过去 MyViewHolder myViewHolder = new MyViewHolder(view); return myViewHolder; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { final MyViewHolder myViewHolder = (MyViewHolder) holder; myViewHolder.textTitle.setText(list.get(position).getTitle()); myViewHolder.textPrice.setText("价格:"+list.get(position).getPrice()); myViewHolder.textTime.setText(list.get(position).getCreatetime()); if(list.get(position).getStatus() == 0){ myViewHolder.textStatus.setText("待支付"); myViewHolder.textStatus.setTextColor(Color.RED); myViewHolder.but.setText("取消订单"); }else if(list.get(position).getStatus() == 1){ myViewHolder.textStatus.setText("已支付"); myViewHolder.but.setText("查看订单"); }else { myViewHolder.textStatus.setText("已取消"); myViewHolder.but.setText("查看订单"); } } @Override public int getItemCount() { return list.size(); } //内部类创建V,在此方法中,寻找控件 class MyViewHolder extends RecyclerView.ViewHolder{ private TextView textTitle; private TextView textPrice; private TextView textTime; private TextView textStatus; private Button but; public MyViewHolder(View view) { super(view); textTitle = view.findViewById(R.id.textTitle); textPrice=view.findViewById(R.id.textPrice); textTime=view.findViewById(R.id.textTime); textStatus=view.findViewById(R.id.textStatus); but=view.findViewById(R.id.but); } } }