Activity
package com.ailtt.kotlintest.activity
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import com.ailtt.kotlintest.R
import com.ailtt.kotlintest.adapter.MyAdapter
import com.ailtt.kt.API
import com.ailtt.kt.News
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_main2.*
import org.reactivestreams.Subscriber
import org.reactivestreams.Subscription
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
class Main2Activity : AppCompatActivity() {
var list:ArrayList<News.ResultBean.DataBean>?= ArrayList()
var adapter:MyAdapter?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
//module的gradle引入apply plugin: 'kotlin-android-extensions',不再需要findviewbyid
recylist.layoutManager=LinearLayoutManager(this)
adapter=MyAdapter(list!!)
recylist.adapter=adapter
getNews()
}
//Retrofit➕RxJava网络请求
//请求的聚合数据新闻
fun getNews(){
var retrofit= Retrofit.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("http://v.juhe.cn")
.build()
var create = retrofit.create(API::class.java)
var news = create.getNews("这里是key", "")
//匿名内部类object:开头
news.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.newThread())
.subscribe(object : Subscriber<News> {
override fun onComplete() {
Log.v("Main","调用完成")
}
override fun onNext(t: News?) {
// var data = t!!.result!!.data
//
// for (i in data!!){
// Log.v("Main",i.title)
// }
list!!.addAll(t?.result?.data!!)
adapter?.notifyDataSetChanged()
}
override fun onSubscribe(s: Subscription?) {
s?.request(Long.MAX_VALUE)
}
override fun onError(t: Throwable?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
Log.v("Main", t!!.message)
}
})
}
}
Adapter
package com.ailtt.kotlintest.adapter
import android.content.Intent
import android.net.Uri
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import com.ailtt.kotlintest.adapter.MyAdapter.MyViewHolder
import com.ailtt.kt.News
/**
* Created by pengzhihao on 2017/5/22.
*/
class MyAdapter(var list:List<News.ResultBean.DataBean>) : RecyclerView.Adapter<MyViewHolder>() {
override fun onBindViewHolder(p0: MyViewHolder?, p1: Int) {
p0?.t?.text =list.get(p1).title
p0?.t?.setOnClickListener {
var intent=Intent()
intent.action="android.intent.action.VIEW"
intent.data=Uri.parse(list.get(p1).url)
p0?.t?.context.startActivity(intent)
// Toast.makeText(p0?.t?.context,"值是${list.get(p1).title},${list.get(p1).url}",Toast.LENGTH_SHORT).show()
}
}
override fun onCreateViewHolder(p0: ViewGroup?, p1: Int): MyViewHolder {
var inflate = LayoutInflater.from(p0?.context).inflate(android.R.layout.simple_list_item_1, p0, false)
return MyViewHolder(itemView = inflate,viewGroup=p0,fs=false)
}
override fun getItemCount(): Int {
return list.size
}
class MyViewHolder(itemView: View?,viewGroup: ViewGroup?,fs:Boolean?) : RecyclerView.ViewHolder(itemView) {
var t:TextView
init {
t=itemView as TextView
}
}
}
News.kt实体类
package com.ailtt.kt
/**
* Created by pengzhihao on 2017/5/21.
*/
class News {
var reason: String? = null
var result: ResultBean? = null
var error_code: Int = 0
class ResultBean {
var stat: String? = null
var data: List<DataBean>? = null
class DataBean {
var uniquekey: String? = null
var title: String? = null
var date: String? = null
var category: String? = null
var author_name: String? = null
var url: String? = null
var thumbnail_pic_s: String? = null
var thumbnail_pic_s02: String? = null
var thumbnail_pic_s03: String? = null
}
}
}