LHgreenbird
2021-11-08 22:49
看到视频6:21时的HomeFragment.kt文件如下:
```
package com.example.firstapp.ui.home
import android.graphics.BitmapFactory
import android.icu.text.ConstrainedFieldPosition
import android.icu.text.Transliterator
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import androidx.core.view.NestedScrollingParent
import androidx.fragment.app.Fragment
import com.example.firstapp.R
import java.net.Inet4Address
//关联的
class HomeFragment : Fragment(R.layout.fragment_home)
{
override fun onViewCreated(view: View, savedInstanceState: Bundle?)
{
super.onViewCreated(view, savedInstanceState)
recycle_view.layoutManager=LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false)
recycle_view.adapter=MyAdapter()
}
// inner声明 MyAddapter是HomeFragment的内部类
inner class MyAdapter:RecyclerView.Adapter<MyViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewTYpe: Int): MyViewHolder {
// 创建ViewHolder对象
val itemView =
LayoutInflater.from(context).inflate(R.layout.item_view_linear_verical, parent, false)
return MyViewHolder(itemView)
}
override fun getItemCount(): Int {
return 20
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.itemView.item_image.setImageResource(R.drawable.icon_jetpack)
holder.itemView.item_name.text = "【${position}】移动端架构师体系课"
holder.itemView.item_message.text =
"移动开发“两极分化”,没有差不多的“中间层,唯有尽早成长为架构师,你的职业道路才能走的更远更稳"
/* !!自带对象不为空,在代码中给一个image控价设置的图片的方式
holder.itemView.item_image.setImageDrawable(
ContextCompat.getDrawable(
context!!,
R.drawable.icon_jetpack))
holder.itemView.item_image.setImageBitmap(BitmapFactory
.decodeResource(context!!.resources,R.drawable.icon_jetpack))
*/
}
}
// 构造函数,需要view对象
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
}
}
```
代码错误量如下:
补充说明:recyle view报错,资源加载不出
2021Android从零入门到实战(Kotlin版)
18917 学习 · 74 问题
相似问题