EventBus结合rxjava2和retrofit2网络获取

 

依赖:

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'

implementation 'org.greenrobot:eventbus:3.1.1'
网络权限:
<uses-permission android:name="android.permission.INTERNET" />

代码实现:
布局:
<cn.bingoogolapple.refreshlayout.BGARefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bga_rl"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.think.bga.BGARefreshLayout0Activity">

    <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>
Activity:
public class BGARefreshLayout4Activity extends AppCompatActivity {


    private RecyclerView rv;
    private OkHttpClient client;
    private RvAdapter adapter;
    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_bgarefresh_layout0);
        //第一步1,注册.
        EventBus.getDefault().register(this);
        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> builder = new Retrofit.Builder()
                        .baseUrl(baseUrl)
                        .addConverterFactory(GsonConverterFactory.create())
                        .client(client)
                        .build()
                        .create(NetInterface.class)
                        .getDataFromNet2(urlAll)
                        .execute();
                FoodInfo body = builder.body();
                //第三步,发送事件.
                EventBus.getDefault().post(body);

            }
        }).subscribeOn(Schedulers.io())//开启子线程
                .subscribe();
    }

    private void initView() {
        rv = (RecyclerView) findViewById(R.id.rv);

        adapter = new RvAdapter(rv);
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setAdapter(adapter);


    }

    //第二步,订阅事件,接收数据.
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void reciveResult(FoodInfo result) {
        adapter.setData(result.getData());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //第一步2,注销.
        EventBus.getDefault().unregister(this);
    }
}

  子布局:

 

<?xml version="1.0" encoding="utf-8"?>
<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>

 

适配器:

 

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

}

 

转载于:https://www.cnblogs.com/SongYongQian/p/8079484.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值