package com.example.kotlintest2.activity import android.content.Intent import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.view.View import android.widget.Button import com.example.kotlintest2.R import com.example.kotlintest2.util.Preference class MainActivity : AppCompatActivity(), View.OnClickListener {//继承与实现都用" : "表示 //定义控件 private var bt1: Button? = null private var bt2: Button? = null private var bt3: Button? = null private var bt4: Button? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.main) initView()//初始化控件 } private fun initView() { bt1 = findViewById(R.id.bt1) as Button bt2 = findViewById(R.id.bt2) as Button bt3 = findViewById(R.id.bt3) as Button bt4 = findViewById(R.id.bt4) as Button //监听事件 bt1?.setOnClickListener(this) bt2?.setOnClickListener(this) bt3?.setOnClickListener(this) bt4?.setOnClickListener(this) } override fun onClick(view: View?) {//相当于java的switch when (view?.id) { R.id.bt1 ->//跳转界面 startActivity(Intent(MainActivity@ this, RecyclerViewActivity::class.java)) R.id.bt2 -> startActivity(Intent(MainActivity@ this, ListViewActivity::class.java)) R.id.bt3 -> startActivity(Intent(MainActivity@ this, FragmentsActivity::class.java)) } } } //RecyclerView的使用package com.example.kotlintest2.activity import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.support.v7.widget.LinearLayoutManager import android.support.v7.widget.RecyclerView import android.widget.TextView import com.example.kotlintest2.R import com.example.kotlintest2.adapter.RecyclerViewsAdapter2 class RecyclerViewActivity : AppCompatActivity() { private var tvTitle: TextView? = null private var rvContent: RecyclerView? = null private var list: ArrayList<String>? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) initData() initView() } private fun initData() { list = ArrayList<String>()//list的使用 for(i in 1..10){//for的使用 list?.add("列表"+i) } } private fun initView() { tvTitle = findViewById(R.id.tv_title) as TextView rvContent = findViewById(R.id.rv_content) as RecyclerView tvTitle?.text = "Kotlin RecyclerView 的用法" // var adapter = RecyclersAdapter(this, list)//第一种方法 var adapter= RecyclerViewsAdapter2(this,list)//第二种方法 rvContent?.layoutManager = LinearLayoutManager(this)//线性 rvContent?.adapter=adapter } //第一种适配器的写法package com.example.kotlintest2.adapter import android.content.Context import android.support.v7.widget.RecyclerView import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView import com.example.kotlintest2.R /** * Created by lenovo on 2017/6/19. */ class RecyclersAdapter : RecyclerView.Adapter<RecyclersAdapter.MyViewHolder> { private var context: Context? = null private var list: ArrayList<String>? = null constructor(context: Context, list: ArrayList<String>?) { this.context = context this.list = list notifyDataSetChanged() } override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): MyViewHolder { return MyViewHolder(LayoutInflater.from(context).inflate(R.layout.recyclerview_item,parent,false)) } override fun onBindViewHolder(holder: MyViewHolder?, position: Int) { holder?.tvTx!!.text=list?.get(position) } override fun getItemCount(): Int { return list?.size as Int } class MyViewHolder : RecyclerView.ViewHolder { constructor(view: View) : super(view){ tvTx=view.findViewById(R.id.tv_tx) as TextView } var tvTx: TextView } }//第二种适配器的写法package com.example.kotlintest2.adapter import android.content.Context import android.support.v7.widget.RecyclerView import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView import com.example.kotlintest2.R /** * Created by lenovo on 2017/6/19. */ class RecyclerViewsAdapter2(context: Context, list: ArrayList<String>?) : RecyclerView.Adapter<RecyclerViewsAdapter2.MyViewHolder>() { var context = context var list = list override fun getItemCount(): Int { return list?.size as Int } override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): MyViewHolder { var view: View = LayoutInflater.from(context).inflate(R.layout.recyclerview_item, parent, false) var holder = MyViewHolder(view) return holder } override fun onBindViewHolder(holder: MyViewHolder?, position: Int) { holder?.tvTx?.text=list?.get(position) } class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) { var tvTx = view.findViewById(R.id.tv_tx) as TextView } }//ListView的使用package com.example.kotlintest2.activity import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.widget.ListView import android.widget.TextView import com.example.kotlintest2.R import com.example.kotlintest2.adapter.ListViewsAdapter class ListViewActivity : AppCompatActivity() { private var tvTitle: TextView? = null private var lvContent: ListView? = null private var list:ArrayList<String>? =null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_list_view) initData() initView() } private fun initView() { tvTitle = findViewById(R.id.tv_title1) as TextView lvContent = findViewById(R.id.lv_content) as ListView tvTitle?.text = "Kotlin ListView 用法" var adapter=ListViewsAdapter(this,list) lvContent?.adapter=adapter } private fun initData(){ list=ArrayList<String>() for(i in 1..10){ list?.add("item"+i) } } }//适配器的写法package com.example.kotlintest2.adapter import android.content.Context import android.view.View import android.view.ViewGroup import android.widget.BaseAdapter import android.widget.TextView import com.example.kotlintest2.R import java.text.FieldPosition /** * Created by lenovo on 2017/6/21. */ class ListViewsAdapter(val context: Context, val list: ArrayList<String>?) : BaseAdapter() { override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View { var holder: MyViewHolder var v: View if (convertView == null) { v = View.inflate(context, R.layout.listview_item, null) holder = MyViewHolder(v) v.tag = holder } else { v = convertView holder = v.tag as MyViewHolder } holder.str.text=list?.get(position) return v } override fun getItem(position: Int): Any { return list?.get(position) as Int } override fun getItemId(position: Int): Long { return position.toLong() } override fun getCount(): Int { return list?.size as Int } class MyViewHolder(var holder: View) { var str: TextView = holder.findViewById(R.id.tv_tx1) as TextView } }//fragment的使用、切换package com.example.kotlintest2.activity import android.app.Fragment import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.support.v4.app.FragmentActivity import android.view.View import android.view.View.OnClickListener import android.widget.TextView import com.example.kotlintest2.R import com.example.kotlintest2.fragment.LeftFragment import com.example.kotlintest2.fragment.RightFragment class FragmentsActivity : FragmentActivity(), OnClickListener { private var leftFragment: Fragment? = null private var rightFragment: Fragment? = null private var tvTitle: TextView? = null private var tvLeft: TextView? = null private var tvRight: TextView? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_fragments) initView() } private fun initView() { tvTitle = findViewById(R.id.tv_title3) as TextView tvLeft = findViewById(R.id.tv_left) as TextView tvRight = findViewById(R.id.tv_right) as TextView tvTitle?.text = "fragment 的展示" tvLeft?.setOnClickListener(this) tvRight?.setOnClickListener(this) leftFragment = fragmentManager.findFragmentByTag("left") ?: LeftFragment() rightFragment = fragmentManager.findFragmentByTag("right") ?: RightFragment() fragmentManager.beginTransaction() .replace(R.id.fl_content, leftFragment, "left") .commit() } override fun onClick(v: View?) {//监听按钮进行fragment的切换 when (v?.id) { R.id.tv_left -> fragmentManager.beginTransaction() .replace(R.id.fl_content, leftFragment, "left") .commit() R.id.tv_right -> fragmentManager.beginTransaction() .replace(R.id.fl_content, rightFragment, "right") .commit() } } }package com.example.kotlintest2.fragment import android.app.Fragment import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import com.example.kotlintest2.App import com.example.kotlintest2.R import com.example.kotlintest2.entity.JavaClass import com.example.kotlintest2.util.Preference /** * Created by lenovo on 2017/6/21. */ class LeftFragment : Fragment() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) } override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { return inflater?.inflate(R.layout.left_fragment_layout, container, false) } override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) val list = arrayListOf(1, 2, 3, 4, 5)//处定义一个集合 val doubleList = list.map { it * 2 }//把它每个元素乘以2 val oddList = list.filter { it % 2 == 1 }//过滤 list.forEach { println(it) }//打印每个元素 doubleList.forEach { println(it) } oddList.forEach { println(it) } val list1 = arrayListOf(1, 3, 5, 7) val javaClass = JavaClass()//调用JAVA实体类 javaClass.anInt = 5 println(javaClass.anInt) } }
package com.example.kotlintest2.fragment import android.app.Fragment import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import com.example.kotlintest2.R /** * Created by lenovo on 2017/6/21. */ class RightFragment :Fragment() { //从SharedPreference里取出数据var aInt:Int by Preference(App.instance(),"aInt",0) fun whatever(){ println(aInt) }override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) } override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View?{ return inflater?.inflate(R.layout.right_fragment_layout,container,false) }override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState)}whatever()//使用SharedPreference
}
//SharedPreference的使用package com.example.kotlintest2.util import android.content.Context import kotlin.properties.ReadWriteProperty import kotlin.reflect.KProperty /** * Created by lenovo on 2017/6/23. */ class Preference<T>(val context: Context, val name: String, val default: T) : ReadWriteProperty<Any?, T> { val prefs by lazy { context.getSharedPreferences("default", Context.MODE_PRIVATE) } override fun getValue(thisRef: Any?, property: KProperty<*>): T { return findPreference(name, default) } override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) { putPreference(name, value) } private fun <U> findPreference(name: String, default: U): U = with(prefs) { val res: Any = when (default) { is Long -> getLong(name, default) is String -> getString(name, default) is Int -> getInt(name, default) is Boolean -> getBoolean(name, default) is Float -> getFloat(name, default) else -> throw IllegalAccessException("this type can be saved into Preferences") } res as U } private fun <U> putPreference(name: String, value: U) = with(prefs.edit()) { when (value) { is Long -> putLong(name, value) is String -> putString(name, value) is Int -> putInt(name, value) is Boolean -> putBoolean(name, value) is Float -> putFloat(name, value) else -> throw IllegalAccessException("this type can be saved into Preferences") }.apply() } }//自定义applicationpackage com.example.kotlintest2 import android.app.Application /** * Created by lenovo on 2017/6/23. */ class App:Application() { companion object{ private var instance:Application?=null fun instance()= instance!! } override fun onCreate() { super.onCreate() instance=this } }
Kotlin使用(fragment的切换与listview、recyclerView的使用)
最新推荐文章于 2022-08-18 15:37:32 发布