Kotlin学习之ListView

Kotlin学习之ListView


前言

kotlin一直都被宣称将要替代java在android中的地位,很多国外的开发者,纷纷使用kotlin进行开发,在国内还很少,大部分还是偏向于使用java,java是一门很好的语言,也一直占据着编程语言排行榜的第一位。作为一个使用java开发的android开发者,我接触到kotlin以后,就感觉喜欢上它的代码的简洁,虽然还有很多方面不是很习惯。比如没有new,语句结尾没有‘;’号。从另一方面看,这些无疑减少了平时开发中出现bug,又不易察觉的地方。

好了,废话不多说。下面开始进入正题吧。

一、创建xml

我们创建一个名为activity_main.xml,里面放一个listview控件,id为lvTodo

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.fm.fangming.todokotlin.MainActivity">

    <ListView
        android:id="@+id/lvTodo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

二、创建ListView的adapter

这里我们自定义一个名为ToDoListAdapter的adapter,继承BaseAdapter

package com.fm.todokotlin.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import com.fm.fangming.todokotlin.R
import kotlinx.android.synthetic.main.todolist_item.view.*

/**
 * @author:fangming
 * @date:Created on 2018/7/16
 * @description:
 */
class ToDoListAdapter(dl: List<String>, mContext: Context) : BaseAdapter() {

    var todoList: List<String>? = null
    var mViewHolder:MyViewHolder? = null
    var mContext:Context

    init {
        todoList = dl
        this.mContext=mContext
    }


    override fun getItem(position: Int): Any {
        return todoList!![position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getCount(): Int {
        return todoList?.size ?: 0
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var mConvertView=convertView
        if(convertView==null){
            mConvertView=LayoutInflater.from(mContext).inflate(R.layout.todolist_item,null)
            mViewHolder=MyViewHolder(mConvertView)
            mConvertView!!.tag=mViewHolder
        }else{
            mViewHolder= convertView.getTag() as MyViewHolder
        }
        mViewHolder!!.mContentTv!!.text= todoList!![position]

        return mConvertView!!
    }

    inner class MyViewHolder(view:View){
        var mContentTv:TextView?=null

        init {
            mContentTv=view.mContentTv
        }

    }

}

从这个类中我们可以简单的看到
1、kotlin中类的构造函数可以直接写在类名的后面,初始化可以在init中进行。
2、kotlin中继承使用:,这个和现在很多现代化的语言相似
3、方法名像javascript一样使用

fun 方法名(): 返回类型{}
这里可以看出它借鉴了很多的语言的优势。

4、集合取值,竟然可以直接这么玩

todoList!![position]

竟然像数组一样,!!表示在todoList不为null下执行。
在java中通常使用todoList.get(position)
5、看getCount()方法里面的三目表达式 ?:
只需要一句话搞定if else

todoList?.size ?: 0

如果todoList为null就返回0,否则就取todoList的size
在java中通常会写

if(todoList!=null){
    return todoList.size;
}else{
    return 0
}

6、在getview方法中,我们在java中通常会这样写

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        String item = todoList.get(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.layout_item_exam, null);
            mViewHolder = new ViewHolder(convertView);
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (ViewHolder) convertView.getTag();
        }
        mViewHolder.mContentTv.setText(item );
        return convertView;
}

不管使用java还是kotlin思想上还是一样的。只是语言的表达形式不一样。
我们使用kotlin时,不能直接使用convertView,因为在kotlin中val对象是只可读的。如果我们改变convertView,就会报错

val can’t be reasgined

这里我们定义一个局部对象来使用

var mConvertView=convertView

这里和java中的思想是一样的,在java中,如果final类型的作为形参。我们使用时也是经常这样

String aa=形参

这样就可以随便操作aa了。而实参的值不会被改变。
7、内部函数前可加inner关键字,标明内部函数
8、各种非空检查 ?和!!,感觉这里有点烦,但是看别人说是避免bug

三、在mainactivity中设置listview的adapter

package com.fm.fangming.todokotlin

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import com.fm.todokotlin.adapter.ToDoListAdapter
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*

class MainActivity : AppCompatActivity() {

    var dataList = ArrayList<String>()
    var mAdapter:ToDoListAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initData()
    }

    fun initData() {
        for (i in 0..10) {
            dataList.add("hello")
        }
        mAdapter= ToDoListAdapter(dataList,this.baseContext)
        lvTodo.adapter = mAdapter

    }
}

好了,设置完以后,运行一下,就可以看到界面了。
这里写图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值