参考https://github.com/Kotlin/anko.
https://www.jianshu.com/p/8c2540964339
https://blog.csdn.net/qq_15059163/article/details/79356529
1.获取布局控件ID
在引用布局的类中,导入对于的xml文件索引, 列如我的xml文件叫activity_main.xml, 导入的代码为:import kotlinx.android.synthetic.main.activity_main.*(输入控件ID,导入提示包)
import kotlinx.android.synthetic.main.activity_main.*
2.按钮单击事件+吐司提示
//按钮单击事件
btn.setOnClickListener {
//吐司提示
Toast.makeText(this,"吐司提示",Toast.LENGTH_SHORT).show()
}
3.跳转页面,传值,接收值,返回
//原生跳转页面
val intent = Intent(this@MainActivity , Main2Activity::class.java)
startActivity(intent)
///原生跳转页面传值 找不到,
****************************
//Anko页面跳转
startActivity()
//Anko页面跳转传值两种方式
startActivity(
Pair("name","麦克"),
Pair("age","23")
)
startActivity(
"name" to "麦克",
"age" to "23"
)
//跳转后页面接收值
val bundle = intent.extras
val require_name = bundle.getString("name")
val require_age = bundle.getString("age")
tv2.text = require_name+" "+require_age
4.Demo:实现RecyclerView展示列表(kotlin+retrofit+glide)
本app下build.gradle依赖
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.squareup.retrofit2:converter-scalars:2.3.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.7.0'
implementation 'com.google.code.gson:gson:2.8.0'
implementation 'com.github.bumptech.glide:glide:4.2.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.2.0'
implementation 'com.android.support:recyclerview-v7:26.1.0'
AndroidManifest.xml权限
主布局:1.activity_main2.xml
子条目布局:2.main2_item.xml
api:http://www.xieast.com/api/news/news.php?page=1
:File --> Settings --> Plugins --> 搜索 JsonToKotlinClass 然后安装即可,之后按住alt+k键弹出一个框 第一个Class Name填写ImageBean就行了,这里可以随便写,JSON Text就可以放置接口里面的JSON数据,Format一下,点击settings 第一排有三个Property,Annotation,other,Property—选择—>Val—选择—>lnit with default value(avoid null)Annotation—选择—>Gson。Generate确认,就会自动生成data class类 | |
---|---|
interface PictureApi {
//http://www.xieast.com/api/news/news.php?page=1
@GET("api/news/news.php")
fun getPicture(@Query("page") page: Int) : Call
data class ImageBean(
@SerializedName("code")
val code: Int = 0,
@SerializedName("data")
val `data`: List = listOf(),
@SerializedName("msg")
val msg: String = ""
)
data class Data(
@SerializedName("author_name")
val authorName: String = "",
@SerializedName("category")
val category: String = "",
@SerializedName("date")
val date: String = "",
@SerializedName("thumbnail_pic_s")
val thumbnailPicS: String = "",
@SerializedName("thumbnail_pic_s02")
val thumbnailPicS02: String = "",
@SerializedName("thumbnail_pic_s03")
val thumbnailPicS03: String = "",
@SerializedName("title")
val title: String = "",
@SerializedName("uniquekey")
val uniquekey: String = "",
@SerializedName("url")
val url: String = ""
)
}
HttpManager
class HttpManager {
var retrofit = Retrofit.Builder()
.baseUrl("http://www.xieast.com/") //基地址
.addConverterFactory(GsonConverterFactory.create())
.build()
fun getPicture(page: Int, success: (PictureApi.ImageBean) -> Unit, fail: (String) -> Unit) {
var pictureApi: PictureApi = retrofit.create(PictureApi::class.java)
var call: Call = pictureApi.getPicture(page)
call.enqueue(object : Callback {
override fun onResponse(call: Call?, response: Response) {
success(response.body()!!)
}
override fun onFailure(call: Call?, t: Throwable?) {
fail(t.toString())
}
})
}
}
PictureAdapter
.
class PictureAdapter (private var list: List) : RecyclerView.Adapter() {
fun notifyDataChange(list: List) { //更新适配器数据
this.list = list
notifyDataSetChanged()
}.
override fun getItemCount(): Int {
return list.size
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
var view = LayoutInflater.from(parent.context).inflate(R.layout.main2_item, parent, false)
return MyViewHolder(view)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.textView.text = list[position].title //展示图片标题
Glide.with(holder.imageView) //ImageView中展示图片
.load(list[position].thumbnailPicS)
.into(holder.imageView)
//这是图片点击事件
holder.imageView.setOnClickListener {
mListener?.invoke(position)
}
}
class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var textView: TextView = itemView.findViewById(R.id.tv_item)
var imageView: ImageView = itemView.findViewById(R.id.img_item)
}
var mListener: ((pos: Int) -> Unit)? = null
fun setOnItemClickListener(listener: ((pos: Int) -> Unit)) {
mListener = listener
}
}
Main2Activity
class Main2Activity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
//适配器,默认一个空的集合,等网络请求后更新数据
var adapter = PictureAdapter(ArrayList())
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = adapter
button.setOnClickListener {
//按钮点击事件
HttpManager().getPicture(
10, //网络请求 5条数据 num表示网络请求返回的数据数量
{ adapter.notifyDataChange(it.data) }, //成功的回掉接口
{ Toast.makeText(this, "失败", Toast.LENGTH_SHORT).show() } //失败的回掉接口
)
}
adapter.setOnItemClickListener {
Toast.makeText(this, it.toString(), Toast.LENGTH_SHORT).show()
}
}
}