赞
踩
目录
2.Retrofit+OkHTTP+RxJava+RecycleView的实现步骤
第四步:在activity_main.xml添加Recycle,并给其设置id
被观察者通过订阅将事件按顺序依次传递给观察者
- public class MainActivity extends AppCompatActivity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //建立观察者 向被观察者发送消息
- Observable observable=Observable.create(new ObservableOnSubscribe<Object>() {
- @Override
- public void subscribe(ObservableEmitter<Object> emitter) throws Exception {
- emitter.onNext(1);
- emitter.onNext(2);
- emitter.onNext(3);
- emitter.onComplete();
- }
- });
- //建立被观察者 接收观察者的消息进行处理
- Observer observer=new Observer() {
- @Override
- public void onSubscribe(Disposable d) {
- Log.i("onSubscribe","开始连接");
- }
-
- @Override
- public void onNext(Object o) {
- Log.i("onNext","接收到的值是"+o);
- }
-
- @Override
- public void onError(Throwable e) {
- Log.i("onError","失败");
- }
-
- @Override
- public void onComplete() {
- Log.i("onComplete","完成");
- }
- };
- //被观察者向观察者进行订阅
- observable.subscribe(observer);
- }
- }
- //for rxjava
- implementation 'io.reactivex.rxjava2:rxjava:2.2.6'
- //for rxandroid
- implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
- //衔接 Retrofit & RxJava,此处一定要注意使用RxJava2的版本
- implementation 'com.squareup.retrofit2:adapter-rxjava2:2.5.0'
- //添加Retrofit依赖
- implementation 'com.squareup.retrofit2:retrofit:2.5.0'
- //添加Gson解析
- implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
- //添加图片加载库依赖
- implementation 'com.github.bumptech.glide:glide:4.12.0'
<uses-permission android:name="android.permission.INTERNET" />
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout 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:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity2"
- android:padding="15dp"
- android:orientation="vertical">
-
- <androidx.recyclerview.widget.RecyclerView
- android:id="@+id/rv_news"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="150dp"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- app:cardElevation="5dp"
- app:cardCornerRadius="5dp"
- android:layout_marginTop="10dp">
-
-
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="140dp"
- android:layout_margin="5dp">
-
-
- <ImageView
- android:id="@+id/news_pic"
- android:scaleType="fitXY"
- android:layout_width="140dp"
- android:layout_height="140dp"
- app:srcCompat="@drawable/ic_launcher_background" />
-
- <TextView
- android:id="@+id/news_title"
- android:layout_toRightOf="@+id/news_pic"
- android:layout_marginStart="20dp"
- android:textSize="16sp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="TextView" />
-
- <TextView
- android:id="@+id/news_desc"
- android:layout_marginTop="20dp"
- android:layout_alignLeft="@+id/news_title"
- android:layout_below="@+id/news_title"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="TextView" />
- </RelativeLayout>
-
- </androidx.cardview.widget.CardView>
- package com.hopu.day23_rxjava.model;
-
- import java.util.List;
-
- public class News {
- private boolean success;
- private String title;
- private String subtitle;
- private String update_time;
- private List<Data> data;
- public void setSuccess(boolean success) {
- this.success = success;
- }
- public boolean getSuccess() {
- return success;
- }
-
- public void setTitle(String title) {
- this.title = title;
- }
- public String getTitle() {
- return title;
- }
-
- public void setSubtitle(String subtitle) {
- this.subtitle = subtitle;
- }
- public String getSubtitle() {
- return subtitle;
- }
-
- public String getUpdate_time() {
- return update_time;
- }
-
- public void setUpdate_time(String update_time) {
- this.update_time = update_time;
- }
-
- public void setData(List<Data> data) {
- this.data = data;
- }
- public List<Data> getData() {
- return data;
- }
-
- public class Data {
-
- private int index;
- private String title;
- private String desc;
- private String pic;
- private String hot;
- private String url;
- private String mobilUrl;
- public void setIndex(int index) {
- this.index = index;
- }
- public int getIndex() {
- return index;
- }
-
- public void setTitle(String title) {
- this.title = title;
- }
- public String getTitle() {
- return title;
- }
-
- public void setDesc(String desc) {
- this.desc = desc;
- }
- public String getDesc() {
- return desc;
- }
-
- public void setPic(String pic) {
- this.pic = pic;
- }
- public String getPic() {
- return pic;
- }
-
- public void setHot(String hot) {
- this.hot = hot;
- }
- public String getHot() {
- return hot;
- }
-
- public void setUrl(String url) {
- this.url = url;
- }
- public String getUrl() {
- return url;
- }
-
- public void setMobilUrl(String mobilUrl) {
- this.mobilUrl = mobilUrl;
- }
- public String getMobilUrl() {
- return mobilUrl;
- }
- }
- }
- public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {
- List<News.Data> list;
-
- public void setResource(List<News.Data> list){
- this.list=list;
-
- notifyDataSetChanged();
- }
-
- @NonNull
- @Override
- public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
- View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.news_item,parent,false);
- ViewHolder viewHolder=new ViewHolder(view);
- return viewHolder;
- }
-
- @Override
- public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
- News.Data data=list.get(position);
- holder.news_title.setText(data.getTitle());
- holder.news_desc.setText(data.getDesc());
- Glide.with(holder.view).load(data.getPic()).into(holder.news_pic);
- }
-
- @Override
- public int getItemCount() {
- if (list==null){
- return 0;
- }else {
- return list.size();
- }
- }
-
- public class ViewHolder extends RecyclerView.ViewHolder {
- TextView news_title,news_desc;
- ImageView news_pic;
- View view;
- public ViewHolder(@NonNull View itemView) {
- super(itemView);
- view=itemView;
- news_desc=itemView.findViewById(R.id.news_desc);
- news_title=itemView.findViewById(R.id.news_title);
- news_pic=itemView.findViewById(R.id.news_pic);
- }
- }
- }
- private RecyclerView rv_news;
- NewsAdapter newsAdapter;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main2);
-
- rv_news=findViewById(R.id.rv_news);
- newsAdapter=new NewsAdapter();
- LinearLayoutManager layoutManager=new LinearLayoutManager(MainActivity2.this);
- rv_news.setLayoutManager(layoutManager);
- rv_news.setAdapter(newsAdapter);
-
- retrofitGet();
- }
- public interface News_Interface {
-
- @GET("api/hotlist?type=baiduRD")
- Observable<News> getNews();
- }
2. 开始网络请求
- private void retrofitGet() {
- Retrofit retrofit=new Retrofit.Builder()
- .baseUrl("https://api.vvhan.com/")
- .addConverterFactory(GsonConverterFactory.create())
- .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
- .build();
- News_Interface newsInterface=retrofit.create(News_Interface.class);
- Observable<News> observable=newsInterface.getNews();
-
- observable.subscribeOn(Schedulers.io())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(new Observer<News>() {
- @Override
- public void onSubscribe(Disposable d) {
-
- }
-
- @Override
- public void onNext(News news) {
- newsAdapter.setResource(news.getData());
- }
-
- @Override
- public void onError(Throwable e) {
- Log.i("onError","连接失败");
- }
-
- @Override
- public void onComplete() {
-
- }
- });
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。