刷新加Rectorfit2和Rxjava

//要添加的依赖

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);

    }
}
//实现代码Rxjava
import 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值