Kotlin的初步使用

1.首先导入依赖

compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'io.reactivex.rxjava2:rxjava:2.1.7'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
//    compile 'com.google.code.gson:gson:2.8.2'
implementation 'com.github.bumptech.glide:glide:3.8.0'
compile 'com.android.support:recyclerview-v7:25.0.0'

2.添加权限

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

3.使用jsontokotlinclass生产Bean类

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
)

4.创建kotlin接口类ApiService

interface ApiService {
    @GET("/comic/book?key=f54c4c57143b8fad9bf3193cab52a81c")
    fun getData() : Observable<ResponseBody>
}

5.创建展示数据的适配器RecyclerViewAdapter

class RecyclerViewAdapter(context1 : Context) : RecyclerView.Adapter<RecyclerViewAdapter.MyAdapter>() {

    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): MyAdapter {
//        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 MyAdapter(view)
    }
    override fun onBindViewHolder(holder: MyAdapter?, position: Int) {
//        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
         holder!!.tv.setText(list.get(position).name)
        println("tv数据是:"+list.get(position).name)
        Glide.with(context).load(list.get(position).coverImg).into(holder!!.img)
    }



    override fun getItemCount(): Int {
//        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    return list.size
    }


    class MyAdapter(view: View) : RecyclerView.ViewHolder(view){

            lateinit var img : ImageView
            lateinit var tv : TextView

            init {
                img = view.findViewById(R.id.img)
                tv = view.findViewById(R.id.tv)
            }

//        constructor(view : View) : super(view) {
//            item_imageview = view.findViewById(R.id.item_imageview)
//            item_textview = view.findViewById(R.id.item_textview)
//        }
        }
    }

6.mainactivity中

class MainActivity : AppCompatActivity() {
    
    lateinit var adapter: RecyclerViewAdapter
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        recyclerview.layoutManager=LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)
        adapter = RecyclerViewAdapter(this)
        recyclerview.adapter = adapter
        getData();
    }

     fun getData() {
        var retrofit=Retrofit.Builder()
                .baseUrl("http://japi.juhe.cn")
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
         var service : ApiService = retrofit.create(ApiService::class.java)
        service.getData()
                 .subscribeOn(Schedulers.io())
                 .observeOn(AndroidSchedulers.mainThread())
                 .subscribe({
                     //onNext();
                    next ->
                   //切记,切记是next.string,不是next.tostring
                     var result =  next.string()
                     var gson = Gson()
                     var bean = gson.fromJson(result, Bean::class.java)
                     adapter.addData(bean)

                 },{
                     //onerror()
                     t->
                 })
     }
}

//好啦,代码全部粘贴出来啦,出不来的自己想办法吧!!!!!!!!!!!!!




猜你喜欢

转载自blog.csdn.net/mydtudysy/article/details/78804418
今日推荐