新语言Kotlin使用recyclerview展示数据+retrofit请求数据+Rxjava
来源:互联网 发布:php erp管理系统 编辑:程序博客网 时间:2024/06/02 01:30
依赖:
compile 'com.squareup.retrofit2:retrofit:2.3.0' compile 'com.squareup.retrofit2:converter-gson:2.3.0' compile 'com.squareup.retrofit2:converter-scalars:2.3.0' compile 'com.android.support:recyclerview-v7:26.1.0' compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0' compile 'io.reactivex.rxjava2:rxandroid:2.0.1' compile 'com.squareup.okhttp3:okhttp:3.9.0' compile 'io.reactivex.rxjava2:rxjava:2.1.7' compile 'com.github.bumptech.glide:glide:3.7.0'
权限:
<uses-permission android:name="android.permission.INTERNET"/>
布局
主布局activity_main
<?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" android:orientation="vertical" tools:context="com.example.muhanxi.kotlindemo.MainActivity"> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/recyclerview"/></LinearLayout>
适配器布局layout
<?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"> <ImageView android:layout_width="120dp" android:layout_height="120dp" android:id="@+id/item_imageview"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/item_textview" android:textColor="#FF0000"/></LinearLayout>
bean类
package com.example.muhanxi.kotlindemo/** * Created by muhanxi on 17/12/14. */data class Bean(val error_code: Int, //200val reason: String, //请求成功!val result: Result)data class Result(val total: Int, //15767val limit: Int, //20val bookList: List<Book>)data class Book(val name: String, //灵神考试val type: String, //少年漫画val area: String, //国漫val des: String,val finish: Boolean, //falseval lastUpdate: Int, //20150603val coverImg: String //http://imgs.juheapi.com/comic_xin/5559b86938f275fd560ad613.jpg)
数据接口IGetDataBase
package com.example.muhanxi.kotlindemoimport io.reactivex.Observableimport okhttp3.ResponseBodyimport retrofit2.http.GET/** * Created by muhanxi on 17/12/13. */interface IService { @GET("/comic/book?key=f54c4c57143b8fad9bf3193cab52a81c") fun getData() : Observable<ResponseBody>}
MainActivity主类
package com.example.muhanxi.kotlindemoimport android.app.Activityimport android.content.Contextimport android.os.Bundleimport android.support.v7.widget.LinearLayoutManagerimport android.view.Viewimport android.widget.Toastimport com.google.gson.Gsonimport io.reactivex.Observableimport io.reactivex.Observerimport io.reactivex.android.schedulers.AndroidSchedulersimport io.reactivex.functions.Consumerimport io.reactivex.internal.util.BackpressureHelper.addimport io.reactivex.schedulers.Schedulersimport kotlinx.android.synthetic.main.activity_main.*import retrofit2.Responseimport retrofit2.Retrofitimport retrofit2.adapter.rxjava2.RxJava2CallAdapterFactoryclass MainActivity : Activity() { lateinit var adapter : IAdapter override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) recyclerview.layoutManager = LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false) adapter = IAdapter(this) recyclerview.adapter =adapter getData() } fun getData(){ var retrofit = Retrofit.Builder() .baseUrl("http://japi.juhe.cn") .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build() var iservice : IService = retrofit.create(IService::class.java) iservice.getData() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( { next -> var result = next.string() println(result) var gson = Gson() var bean = gson.fromJson(result,Bean::class.java) adapter.addData(bean) } , { t -> } )// iservice.getData()// .subscribeOn(Schedulers.io())// .observeOn(AndroidSchedulers.mainThread())// .subscribe({// next ->//// // onnext//// },{// t ->//// error// },{//// oncomplete// },{// d ->//// d.dispose()// }) }}
适配器IAdapter
package com.example.muhanxi.kotlindemoimport android.content.Contextimport android.support.v7.widget.RecyclerViewimport android.view.LayoutInflaterimport android.view.Viewimport android.view.ViewGroupimport android.widget.ImageViewimport android.widget.TextViewimport com.bumptech.glide.Glide/** * Created by muhanxi on 17/12/14. */class IAdapter(context1 : Context) : RecyclerView.Adapter<IAdapter.IViewHodler>() { var context : Context = context1 var list : ArrayList<Book> = ArrayList() fun addData(bean : Bean) { list.addAll(bean.result.bookList) notifyDataSetChanged() } override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): IViewHodler {// TODO("not implemented") //To change body of created functions use File | Settings | File Templates. var view = LayoutInflater.from(context).inflate(R.layout.layout,parent,false) return IViewHodler(view) } override fun onBindViewHolder(holder: IViewHodler?, position: Int) {// TODO("not implemented") //To change body of created functions use File | Settings | File Templates. holder!!.item_textview.setText(list.get(position).name) Glide.with(context).load(list.get(position).coverImg).into(holder!!.item_imageview) } override fun getItemCount(): Int {// TODO("not implemented") //To change body of created functions use File | Settings | File Templates. return list.size } class IViewHodler(view : View) : RecyclerView.ViewHolder(view) { lateinit var item_imageview : ImageView lateinit var item_textview : TextView init { item_imageview = view.findViewById(R.id.item_imageview) item_textview = view.findViewById(R.id.item_textview) }// constructor(view : View) : super(view) {// item_imageview = view.findViewById(R.id.item_imageview)// item_textview = view.findViewById(R.id.item_textview)// } }}
阅读全文
0 0
- Kotlin新语言使用RxJava+Retrofit请求网络数据+lambda表达式+RecyclerView展示+Glide展示图片
- 新语言Kotlin使用recyclerview展示数据+retrofit请求数据+Rxjava
- MVP+retrofit和Rxjava请求数据+recyclerview展示
- MVP+Retrofit+RxJava请求网络数据实现RecyclerView多条目展示
- Rxjava+Retrofit 请求数据
- 使用MVP+Retrofit+RxJava 图片加载使用Fresco 做网络请求并展示数据
- Retrofit网络请求+Recyclerview展示数据+Fresco显示图片
- Retrofit网络请求+Recyclerview展示数据+Fresco显示图片
- Retrofit+RxJava+Fresco简单实现数据展示
- RxJava结合Retrofit使用 自定义封装类结合泛型 请求网络数据+Fresco+RecyclerView+MVP分层
- Rxjava观察者模式+Retrofit请求GSON数据+Fresco处理图片+Xrecyclerview展示数据+MVP模式
- MVP框架使用Retrofit+Rxjava请求网络数据
- MVP+Rxjava+Okhttp+Retrofit+XRecyclerview请求数据
- MVP+Rxjava+Retrofit+Fresco网络请求数据
- Android Kotlin(二)—— Kotlin与Retrofit进行网络请求RecyclerView展示图片列表
- MVP+Retrofit+ReJava+Xrecyclerview请求展示数据
- fresco加载图片+EventBus Activity之间跳转传值+GreenDAo数据库+retrofit请求数据+recyclerview展示数据+ButterKnife找控件
- Retrofit+OkHttpClient+Fresco+XRecyclerView+RxJava实现数据展示
- Python基础-获取对象信息的常用函数
- Centos6.4下Yum命令安装Mysql数据库及配置
- 时间处理
- Super关键字理解
- ListView同时相应长按点击事件和单击事件的解决方法
- 新语言Kotlin使用recyclerview展示数据+retrofit请求数据+Rxjava
- mysql5.7官网直译InnoDB表优化--磁盘I/O的优化
- 30秒让你知道悲观锁和乐观锁的区别
- null
- redis支持的五种数据类型及其底层实现
- Windows下SQLMAP的安装图解
- linux0.00内核剖析之2.保护模式内存管理
- Tensorflow练习1-对评论进行分类
- javeWeb springMvc获取到的参数附带特殊符号,接收后被转义