A day's plan lies in the morning. Come on!
Summary
In this paper, kotlin implements a recycler view list, item click events.
demo has been written long ago, but it will be forgotten after a long time. Still need to review frequently, is not there a memory curve, so this article is my review, as the saying goes, good memory is better than bad pen!
To configure
build.gradle of app
apply plugin: 'kotlin-android'
apply plugin:'kotlin-android-extensions'
apply plugin: 'android-apt'
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
compile 'org.jetbrains.anko:anko:0.10.0'
build.gradle of project
buildscript {
ext.kotlin_version = '1.1.2-5' //version number
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
//To configure
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
Introduction to Kotlin Foundation
This is just the basis for this article. I feel that learning a new language, or writing demo s or projects, is more profound. A lot of things but look at grammar, forget very quickly (anyway I am), in a specific situation to learn the fastest, the deepest memory.
- var and val
var is a variable val is a constant
It can be empty.
The third option is NPE-partners. We can use b!!, which returns a non-empty B or throws an NPE with B empty.
//In the recyclerview adapter hodler
//Here you need to write itemView directly from the previous one. Calls are not possible
var txtName: TextView = itemView!!.findViewById(R.id.text_kotlin) as TextView;
- Using reflection
//Open an activity
var intent=Intent()
//:: Using reflection
intent.setClass(this,Main2Activity::class.java)
startActivity(intent)
- Function returns an Arraylist set
/**
* False data
* Returns an ArrayList < String > collection
* */
fun getData( ) : ArrayList<String>{
val arrayList = ArrayList<String>()
val list = listOf<String>("a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "l", "m", "n", "o")
//for loop
for (k in list){
arrayList.add(k)
}
return arrayList
}
- for loop
//for loop
for (k in list){
arrayList.add(k)
}
- Log Printing and Toast
var a : Int=2//var variable val constant
var b : String="It's a learning. Kotlin Opportunities"
Log.e("MainActivity","a=>>>${a}______b=>>>${b}")//Log Printing
toast("a=>>>${b}")//
Implementing Recyclerview List
android native implementations will be written by everyone. Here we just say that the following kotlin implementations are different from android native implementations.
- Be careful:
//This line of code helps you find all the controls in the layout.
//You can use shortcut keys to import
import kotlinx.android.synthetic.main.activity_main.*
- Setting List Styles and Adapters
//Native mode:
linearLayoutManager = new LinearLayoutManager(getActivity());
mRec.setLayoutManager(linearLayoutManager);
adapter = new OnlineRedAreaListAdapter(getActivity(), redAreaList, this);
mRec.setAdapter(adapter);
//kotlin mode
val adapter = KotlinRecAdapter(this, getData()!!)
recyclerview.layoutManager=LinearLayoutManager(this)
recyclerview.adapter=adapter;
//Is it a bright feeling?
//getData()!! Represents non-empty
getData():
/**
* False data
* Returns an ArrayList < String > collection
* */
fun getData( ) : ArrayList<String>{
val arrayList = ArrayList<String>()
val list = listOf<String>("a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "l", "m", "n", "o","p")
//for loop
for (k in list){
arrayList.add(k)
}
return arrayList
}
- Writing of adapter
Biography
//Data transmission
val adapter = KotlinRecAdapter(this, getData()!!)
KotlinRecAdapter
class KotlinRecAdapter(mCtx:Context,mList:ArrayList<String>) :RecyclerView.Adapter<KotlinRecAdapter.MHolder>(){
//You can put the data directly after the class name and receive it with variables.
private var context:Context = mCtx
private var list:ArrayList<String> = mList
ViewHolder can also be written in a variety of ways:
/**
* The first way of writing
* The first one is used by default when using shortcut keys.
* ?Representation can be empty
* !! The third option is NPE-partners. We can use b!!, which returns a non-empty B or throws an NPE with B empty.
* */
class MHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {
//Here you need to write itemView directly from the previous one. Calls are not possible
var txtName: TextView = itemView!!.findViewById(R.id.text_kotlin) as TextView;
var linearlayout: LinearLayout = itemView!!.findViewById(R.id.linearlayout) as LinearLayout;
}
/**
* The second way of writing
* */
class MHolder: RecyclerView.ViewHolder {
constructor(view : View) : super(view){
}
}
Rewrite the method in Recyclerview's adapter:
override fun onBindViewHolder(holder: MHolder?, position: Int) {
// holder?.txtName? Must we add one? Number can be invoked ?Representation can be empty
holder?.txtName?.text=list.get(position)
holder?.linearlayout?.setOnClickListener {
//item click event
litener?.onItemClick(position)
}
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): MHolder {
return MHolder(LayoutInflater.from(context).inflate(R.layout.kotlin_item,parent,false))
}
override fun getItemCount(): Int {
return list.size
}
Custom interface for item click events
/**
* Custom interface to handle click events
* It's basically the same as java
* */
interface OnItemClickLitener {
fun onItemClick(position : Int)
}
var litener:OnItemClickLitener? =null
fun setOniteClickListener(litener : OnItemClickLitener ){
this.litener=litener
}
- Enclosed is the overall code of the adapter:
class KotlinRecAdapter(mCtx:Context,mList:ArrayList<String>) :RecyclerView.Adapter<KotlinRecAdapter.MHolder>(){
//You can put the data directly after the class name and receive it with variables.
private var context:Context = mCtx
private var list:ArrayList<String> = mList
override fun onBindViewHolder(holder: MHolder?, position: Int) {
// holder?.txtName? Must be added? Number can be invoked? Representation can be empty
holder?.txtName?.text=list.get(position)
holder?.linearlayout?.setOnClickListener {
litener?.onItemClick(position)
}
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): MHolder {
return MHolder(LayoutInflater.from(context).inflate(R.layout.kotlin_item,parent,false))
}
override fun getItemCount(): Int {
return list.size
}
/**
* The first way of writing
* The first one is used by default when using shortcut keys.
* ?Representation can be empty
* !! The third option is NPE-partners. We can use b!!, which returns a non-empty B or throws an NPE with B empty.
* */
class MHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {
//Here you need to write itemView directly from the previous one. Calls are not possible
var txtName: TextView = itemView!!.findViewById(R.id.text_kotlin) as TextView;
var linearlayout: LinearLayout = itemView!!.findViewById(R.id.linearlayout) as LinearLayout;
}
/**
* The second way of writing
* */
//class MHolder: RecyclerView.ViewHolder {
// constructor(view : View) : super(view){
//
// }
// }
/**
* Custom interface to handle click events
* It's basically the same as java
* */
interface OnItemClickLitener {
fun onItemClick(position : Int)
}
var litener:OnItemClickLitener? =null
fun setOniteClickListener(litener : OnItemClickLitener ){
this.litener=litener
}
}
The way activity implements interfaces has also changed
class MainActivity : AppCompatActivity() ,KotlinRecAdapter.OnItemClickLitener{
override fun onItemClick(position: Int) {
}
intent transfers data
//Similar to java, the way to turn on activity has changed
var intent=Intent()
//:: Using reflection
intent.setClass(this,Main2Activity::class.java)
intent.putExtra("position",""+position)
startActivity(intent)
So far, the whole function has been realized!
demo download address
Learn a little every day and add up. There will always be returns, as the saying goes, dripping through stones!