Kotlin创建类的方式与Java类似,如创建Person类:
class Person{
var name: String? = ""
var age: Int = 0
}
可以看到,Kotlin的变量声明方式略有些不同。另外,为了实现“空安全”,Kotlin对可以为空的变量和不可以为空的变量作了区分。在上述代码中,变量age不可为空,而?表明变量name可以为空。下面创建实例(注,Kotlin实例时没有关键字new,实例完就可以直接赋值了):
var person = Person()
person.name= "yoyo"
person.age = 22
构造函数在Kotlin创建非常简单(注,这个类没有任何内容可以省略大括号),如下:
class Person(var name: String?, var age: Int)
构造函数的函数体你可以写在init块中:
class Person(var name: String?, var age: Int){
init{
...
}
}
从下面图可以看出可以创建Activity、file、class的kt文件
下面编写一个RecyclerView展示集合类HomeFragment.kt,由于xml比较简单就不贴出来了:
class HomeFragment : Fragment() {
private var persons: List? = null
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater!!.inflate(R.layout.fragment_home,container,false)
}
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
getData()
val recyclerView : RecyclerView = view!!.findViewById(R.id.recycler_view) as RecyclerView
recyclerView.layoutManager = LinearLayoutManager(activity!!)
recyclerView.addItemDecoration(DividerDecoration(activity))
//java写的适配器
// val adapter: HomeAdapter = HomeAdapter(activity, persons!!)
//kotlin写的适配器
val adapter: HomeKtAdapter = HomeKtAdapter(activity, persons!!)
recyclerView.adapter = adapter
}
/**
* mock数据
*/
private fun getData(): List {
persons = listOf(
Person("html", 12),
Person("kotlin", 36),
Person("java", 12),
Person(null, 12),
Person("android", 12),
Person("ios", 12),
Person("c++", 12),
Person("php", 12)
)
return persons as List
}
}
如你所见,我们定义类一个变量并转型为RecyclerView。这里与Java有点不同,LayoutManager、adapter等会通过属性的方式被设置,而不是通过set;
List的创建也与java不一样,现在简单的可以通过使用一个函数创建一个常量的List,它就会自动推断出结果的类型。
这里为了体现kotlin与java代码互通,这里写了两个适配器,一个是用java写的HomeAdapter.java,另一个是用kotlin写的HomeKtAdapter.kt
/**
* HomeAdapter
* Created by chen_yong on 2017/5/24.
*/
public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> {
private List persons;
private LayoutInflater inflater;
public HomeAdapter(Context mContext,List persons) {
this.persons = persons;
this.inflater = LayoutInflater.from(mContext);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(inflater.inflate(R.layout.home_item,parent,false));
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Person person = persons.get(position);
holder.tv_name.setText(person.getName());
holder.tv_age.setText(String.valueOf(person.getAge()));
}
@Override
public int getItemCount() {
return persons.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
TextView tv_name;
TextView tv_age;
public ViewHolder(View itemView) {
super(itemView);
tv_name = (TextView) itemView.findViewById(R.id.name);
tv_age = (TextView) itemView.findViewById(R.id.age);
}
}
}
/**
* HomektAdapter
* Created by chen_yong on 2017/5/24.
*/
class HomeKtAdapter(mContext: Context, private val persons: List<Person>) : RecyclerView.Adapter<HomeKtAdapter.ViewHolder>() {
private val inflater: LayoutInflater = LayoutInflater.from(mContext)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(inflater.inflate(R.layout.home_item, parent, false))
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val person = persons[position]
holder.tv_name.text = person.name
holder.tv_age.text = person.age.toString()
}
override fun getItemCount(): Int {
return persons.size
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var tv_name: TextView = itemView.findViewById(R.id.name) as TextView
var tv_age: TextView = itemView.findViewById(R.id.age) as TextView
}
}