Androidでkotlin(recyclerviewの使用)を使う

5184 ワード

Googleがkotlinを公式開発言語として発表するにつれて、Androidでkotlinを使用する傾向も明らかになりました.以下はプロジェクトでよく使われるコントロールです.では、Javaコードをkotlinコードに書き換えるにはどうすればいいのでしょうか.
1、まず私たちのレイアウトファイルです.何も言わないで、元の書き方に従えばいいです.
2、コントロールの初期化について:現在私が発見したのは2つの書き方がある. 
 1)なし apply plugin:'kotlin-android-extensions'プラグイン
var text=findViewById(R.id.test_test)as TextView//as強制変換記号
2)apply plugin:'kotlin-android-extensions'にプラグインを加えると直接id.属性、
test_test.text = price 
使用するメインレイアウト



    

    



activity      (    3 adapter       )
class Main2Activity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)

//        var adapter = RecycleAdapter(this,initData()!!)
//        var adapter = ReceycleViewAdapter(this,initData())
        var adapter = RecycleAdapter2(this,initData()!!)
        recycler_view.layoutManager = LinearLayoutManager(this)
        recycler_view.adapter = adapter
        adapter.setOnclickliseer(object :RecycleAdapter2.MyInter{     //       
            override fun testStr(str: String) {
                test_test.text = str
            }

        })

    }

    private fun initData(): ArrayList? {
        var list=ArrayList()
        for (i in 0..50){
            var test = TestVar(i.toString(),"  "+i,R.mipmap.ic_launcher)
            list.add(i,test)
        }
        Log.d("tag","list.size------"+ list.size)
        return list
    }
}

1、recycleAdapter     (        )
class RecycleAdapter : RecyclerView.Adapter {
    private var context:Context? = null
    private var list:ArrayList? = null
    constructor(context:Context,list:ArrayList){
        this.context = context
        this.list = list
        notifyDataSetChanged()
    }
    override fun onBindViewHolder(p0: MyViewHolder?, p1: Int) {
        p0?.name!!.text = list?.get(p1)?.name
        p0.image.setImageResource(list?.get(p1)?.res!!)
    }

    override fun onCreateViewHolder(p0: ViewGroup?, p1: Int): MyViewHolder {

        return  MyViewHolder(LayoutInflater.from(
                context).inflate(R.layout.recyclerview_item, p0,
                false))

    }

    override fun getItemCount(): Int {
        return list?.size as Int
    }




    class MyViewHolder : RecyclerView.ViewHolder {
       constructor(view:View) : super(view) {
            name = view.findViewById(R.id.name) as TextView
           image = view.findViewById(R.id.image) as ImageView
        }
        var name:TextView
        var image:ImageView
    }
}

2、recycleviewAdapter,       Java  ,       ,   activity   kotlin       
3、recycleadapter(      ,          ,                    )
class RecycleAdapter2(context2:Context,list2:ArrayList) : RecyclerView.Adapter() {
    var context = context2    //    
    var list = list2
    var inter:MyInter? = null
    override fun onBindViewHolder(p0: MyViewHolder?, p1: Int) {
        p0?.name?.text = list[p1].name
        p0?.image?.setImageResource(list[p1].res)

        p0!!.itemView.setOnClickListener {
            inter?.testStr(list[p1].name)
        }
    }

    fun setOnclickliseer(inter:MyInter){
        this.inter = inter
    }

    override fun onCreateViewHolder(p0: ViewGroup?, p1: Int): MyViewHolder {
        var view:View = LayoutInflater.from(context).inflate(R.layout.recyclerview_item, p0, false)
        var holder = MyViewHolder(view)
        return  holder

    }

    override fun getItemCount(): Int {
        return list?.size
    }
	//              
//    class MyViewHolder : RecyclerView.ViewHolder {
//       constructor(view:View) : super(view) {
//            name = view.findViewById(R.id.name) as TextView
//           image = view.findViewById(R.id.image) as ImageView
//        }
//        var name:TextView
//        var image:ImageView
//    }
    class MyViewHolder(view:View) : RecyclerView.ViewHolder(view) {
        var name:TextView = view.findViewById(R.id.name) as TextView
        var image:ImageView = view.findViewById(R.id.image) as ImageView

}




    interface MyInter{    //      (         )
        fun testStr(str:String)
    }
}


  ,       ,          ,       TestVar      ,
         ,       kotlin,      ,        ,