刷新加Rectorfit2和Rxjava
来源:互联网 发布:豆豆营销软件官方 编辑:程序博客网 时间:2024/05/17 21:40
//要添加的依赖
compile 'cn.bingoogolapple:bga-refreshlayout:1.1.7'compile 'com.squareup.okhttp3:okhttp:3.9.0'compile 'com.squareup.okio:okio:1.13.0'implementation 'com.google.code.gson:gson:2.8.2'implementation 'com.android.support:recyclerview-v7:26.+'compile 'cn.bingoogolapple:bga-adapter:1.1.5@aar'compile 'com.squareup.retrofit2:converter-gson:2.3.0'//compile 'com.squareup.retrofit2:retrofit:2.3.0'compile 'io.reactivex.rxjava2:rxjava:2.1.3'compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
//布局文件注意两个ID
<cn.bingoogolapple.refreshlayout.BGARefreshLayout 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:id="@+id/bga_rl" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.administrator.lian_yue.MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/rv" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="0dp"></android.support.v7.widget.RecyclerView></cn.bingoogolapple.refreshlayout.BGARefreshLayout>//rectorfit请求接口public interface NetInterface { @GET() Call<FoodInfo> getDataFromNet2(@Url String url);}//刷新适配器public class RvAdapter extends BGARecyclerViewAdapter<FoodInfo.DataBean>{ public RvAdapter(RecyclerView recyclerView) { //添加布局 super(recyclerView,R.layout.item_rv); } @Override protected void fillData(BGAViewHolderHelper helper, int position, FoodInfo.DataBean model) { //设置内容 helper.setText(R.id.tv,model.getTitle()) .setText(R.id.tv_food,model.getFood_str()) .setImageResource(R.id.iv,R.mipmap.ic_launcher_round); }}//实现代码Rxjavaimport android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;import android.widget.Toast;import com.example.demonstrate.DemonstrateUtil;import com.google.gson.Gson;import java.util.List;import cn.bingoogolapple.refreshlayout.BGARefreshLayout;import cn.bingoogolapple.refreshlayout.BGAStickinessRefreshViewHolder;import io.reactivex.Observable;import io.reactivex.ObservableEmitter;import io.reactivex.ObservableOnSubscribe;import io.reactivex.android.schedulers.AndroidSchedulers;import io.reactivex.functions.Consumer;import io.reactivex.functions.Function;import io.reactivex.schedulers.Schedulers;import okhttp3.OkHttpClient;import retrofit2.Response;import retrofit2.Retrofit;import retrofit2.converter.gson.GsonConverterFactory;public class MainActivity extends AppCompatActivity { protected RecyclerView rv; protected BGARefreshLayout bgaRl; private String url = "http://www.qubaobei.com/ios/cf/dish_list.php/?stage_id=1&limit=10&page=1"; private Gson gson; private RvAdapter adapter; private OkHttpClient client; private String baseUrl = "http://www.qubaobei.com/"; private String urlAll ="http://www.qubaobei.com/ios/cf/dish_list.php/?stage_id=1&limit=10&page=1"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gson = new Gson(); client = new OkHttpClient .Builder() .build(); initView(); request(); } private void request() { Observable.create(new ObservableOnSubscribe<FoodInfo>() { @Override public void subscribe(ObservableEmitter<FoodInfo> e) throws Exception { //发射数据的逻辑. Response<FoodInfo> response = new Retrofit.Builder() .baseUrl(baseUrl) .addConverterFactory(GsonConverterFactory.create()) .client(client) .build() .create(NetInterface.class) .getDataFromNet2(urlAll) .execute(); FoodInfo foodInfo = response.body(); //不用map符// List<FoodInfo.DataBean> dataBeans = foodInfo.getData(); e.onNext(foodInfo); } }).subscribeOn(Schedulers.io()).map(new Function<FoodInfo, List<FoodInfo.DataBean>>() { @Override public List<FoodInfo.DataBean> apply(FoodInfo foodInfo) throws Exception { return foodInfo.getData(); } }).observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<List<FoodInfo.DataBean>>() { @Override public void accept(List<FoodInfo.DataBean> dataBeans) throws Exception { //执行更UI的操作. for (FoodInfo.DataBean info : dataBeans){ DemonstrateUtil.showLogResult(info.toString()); } adapter.setData(dataBeans); } }); } private void initView() { rv = (RecyclerView) findViewById(R.id.rv); bgaRl = (BGARefreshLayout) findViewById(R.id.bga_rl); bgaRl.setDelegate(new BGARefreshLayout.BGARefreshLayoutDelegate() { @Override public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) { //刷新进行的操作 DemonstrateUtil.showToastResult(MainActivity.this,"刷新成功"); refreshLayout.endRefreshing(); } @Override public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) { //加载进行的操作. return false; } }); BGAStickinessRefreshViewHolder stickinessRefreshViewHolder = new BGAStickinessRefreshViewHolder(this, true); stickinessRefreshViewHolder.setStickinessColor(R.color.colorPrimary); stickinessRefreshViewHolder.setRotateImage(R.mipmap.bga_refresh_loading12); bgaRl.setRefreshViewHolder(stickinessRefreshViewHolder); adapter = new RvAdapter(rv); rv.setLayoutManager(new LinearLayoutManager(this)); rv.setAdapter(adapter); }}//数据实体类使用快捷键创建GsonFormat//item_rv布局<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:orientation="horizontal"> <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <LinearLayout android:layout_marginLeft="20dp" android:layout_gravity="center_vertical" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:text="11111111" android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:text="2222222" android:id="@+id/tv_food" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout></LinearLayout>
阅读全文
0 0
- 刷新加Rectorfit2和Rxjava
- 导航加下划线和数据的刷新
- Retrofit和RxJava加OkHttp网络请求进行二次封装
- Rxjava +EventBus 数据库的异步操作和界面的刷新
- RecyclerView加下拉刷新
- 多线程下载加安装程序加RxJava加Fresco
- RXjava加okhttp 请求请求网络数据
- 刷新页面每次加1
- ListView 下拉刷新加优化
- Listview,顶部加viewpager,并且带有上拉刷新和下拉分页功能的展示
- 下拉刷新加ScrollView嵌套List,加暂无数据和底部 the-end布局写法
- Android Rxjava和retrofit
- RxJava介绍和使用
- Rxjava和Retrofit相结合
- 初识RxJava和Retrofit
- Rxjava和lambda语法
- rxjava和retrofit结合
- Retrofit和RxJava
- 子类,父类静态代码块,构造代码块,以及构造函数执行顺序
- Reinforcement Learning: Model-free control
- 频道管理
- 毕业后
- 每一个程序员必须知道的业内英语词汇
- 刷新加Rectorfit2和Rxjava
- LeetCode 13 Roman to Integer (easy)——python
- 购物车之订单列表
- 如何搭建gerrit服务器进行code review
- 160个练手CrackMe-044
- 3.反向代理-负载均衡
- 在eclipse导入paramiko模块
- Unity游戏状态脚本控制思考(二)
- 大数的阶乘