新语言Kotlin使用recyclerview展示数据+retrofit请求数据+Rxjava
2017-12-14 14:35
615 查看
依赖:
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, //200
val reason: String, //请求成功!
val result: Result
)
data class Result(
val total: Int, //15767
val limit: Int, //20
val bookList: List<Book>
)
data class Book(
val name: String, //灵神考试
val type: String, //少年漫画
val area: String, //国漫
val des: String,
val finish: Boolean, //false
val lastUpdate: Int, //20150603
val coverImg: String //http://imgs.juheapi.com/comic_xin/5559b86938f275fd560ad613.jpg
)
数据接口IGetDataBase
package com.example.muhanxi.kotlindemo
import io.reactivex.Observable
import okhttp3.ResponseBody
import 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.kotlindemo
import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.widget.Toast
import com.google.gson.Gson
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Consumer
import io.reactivex.internal.util.BackpressureHelper.add
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_main.*
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
class 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 ->
4000
} )
// iservice.getData()
// .subscribeOn(Schedulers.io())
// .observeOn(AndroidSchedulers.mainThread())
// .subscribe({
// next ->
//
// // onnext
//
// },{
// t ->
//// error
// },{
//// oncomplete
// },{
// d ->
//// d.dispose()
// })
}
}
适配器IAdapter
package com.example.muhanxi.kotlindemo
import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import 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)
// }
}
}
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, //200
val reason: String, //请求成功!
val result: Result
)
data class Result(
val total: Int, //15767
val limit: Int, //20
val bookList: List<Book>
)
data class Book(
val name: String, //灵神考试
val type: String, //少年漫画
val area: String, //国漫
val des: String,
val finish: Boolean, //false
val lastUpdate: Int, //20150603
val coverImg: String //http://imgs.juheapi.com/comic_xin/5559b86938f275fd560ad613.jpg
)
数据接口IGetDataBase
package com.example.muhanxi.kotlindemo
import io.reactivex.Observable
import okhttp3.ResponseBody
import 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.kotlindemo
import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.widget.Toast
import com.google.gson.Gson
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Consumer
import io.reactivex.internal.util.BackpressureHelper.add
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_main.*
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
class 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 ->
4000
} )
// iservice.getData()
// .subscribeOn(Schedulers.io())
// .observeOn(AndroidSchedulers.mainThread())
// .subscribe({
// next ->
//
// // onnext
//
// },{
// t ->
//// error
// },{
//// oncomplete
// },{
// d ->
//// d.dispose()
// })
}
}
适配器IAdapter
package com.example.muhanxi.kotlindemo
import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import 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)
// }
}
}
相关文章推荐
- Kotlin新语言使用RxJava+Retrofit请求网络数据+lambda表达式+RecyclerView展示+Glide展示图片
- MVP+Retrofit+RxJava请求网络数据实现RecyclerView多条目展示
- MVP+retrofit和Rxjava请求数据+recyclerview展示
- 使用MVP+Retrofit+RxJava 图片加载使用Fresco 做网络请求并展示数据
- Android使用OKHTTP网络框架请求数据,RecyclerView结合Glide展示图片,瀑布流布局样式
- 展示数据使用:recyclerview,retrofit,greendao,butterknife,eventbus,fresco。实现效果图列表。MVP模式。
- MVP+Retrofit+ReJava+Xrecyclerview请求展示数据
- Android Kotlin(二)—— Kotlin与Retrofit进行网络请求RecyclerView展示图片列表
- Retrofit网络请求+Recyclerview展示数据+Fresco显示图片
- 使用MVP+Retrofit+RxJava 图片加载使用Fresco 做网络请求并展示数据
- Retrofit网络请求+Recyclerview展示数据+Fresco显示图片
- MVP+Rxjava+Okhttp+Retrofit+XRecyclerview请求数据
- Retrofit+OkHttpClient+Fresco+XRecyclerView+RxJava实现数据展示
- Kotlin实现Retrofit网络请求RecyclerView展示图片列表
- fresco加载图片+EventBus Activity之间跳转传值+GreenDAo数据库+retrofit请求数据+recyclerview展示数据+ButterKnife找控件
- RxJava结合Retrofit使用 自定义封装类结合泛型 请求网络数据+Fresco+RecyclerView+MVP分层
- RecyclerView的多条目展示,使用OkHttp解析数据
- MVP实现用Retrofit请求网络数据Fresco加载图片,Recyclerview CheckBox显示并实现全选删除
- OkHttp+RecyclerView,利用OkHttp请求数据,并RecyclerView展示
- MVP架构分包+OkHttp网络请求数据并展示到xrecyclerview上