我正在尝试使用回收者视图在底部导航活动中显示项目,但我没有获得项目值。
我的项目activity_main.xml,list_row.xml和fragment_home.xml中有三种布局。 Recycler视图位于fragment_home.xml中,list_row.xml包含两个文本视图。我没有在代码中看到任何错误,但是在日志中,我收到了“没有附加适配器的消息”。非常感谢您的帮助。
Error:
E/RecyclerView: No adapter attached; skipping layout
Below is the HomeFragment.kt code.
class HomeFragment : Fragment() {
private var adapter:PersonListAdapter?=null
private var personList:ArrayList<Person>?=null
private var layoutManager: RecyclerView.LayoutManager?=null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_home, container, false)
personList=ArrayList<Person>()
layoutManager= LinearLayoutManager(this.context)
adapter= PersonListAdapter(personList, this.context!!)
recyclerView.layoutManager=layoutManager
recyclerView.adapter=adapter
for (i in 0..16) {
val person = Person()
person.name="Hello" + i
person.age = 20 + i
personList!!.add(person)
}
adapter!!.notifyDataSetChanged()
}
}
MainActivity.kt code
class MainActivity : AppCompatActivity() {
private var adapter:PersonListAdapter?=null
private var personList:ArrayList<Person>?=null
private var layoutManager: RecyclerView.LayoutManager?=null
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener {item->
when(item.itemId){
R.id.home -> {
println("home pressed")
replaceFragment(HomeFragment())
return@OnNavigationItemSelectedListener true
}
}
false
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
bottomNavigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
replaceFragment(HomeFragment())
}
private fun replaceFragment(fragment: Fragment){
val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.fragmentContainer, fragment)
fragmentTransaction.commit()
}
}
Data Class:
class PersonListAdapter(private val list: ArrayList<Person>?, private val context: Context): RecyclerView.Adapter<PersonListAdapter.ViewHolder>()
{
override fun getItemCount(): Int {
return list!!.size
}
override fun onCreateViewHolder(parent: ViewGroup, position: Int): ViewHolder {
val view= LayoutInflater.from(context).inflate(R.layout.list_row,parent,false)
return ViewHolder(view)
}
override fun onBindViewHolder(p0: ViewHolder, p1: Int) {
p0?.bindItem(list?.get(p1))
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
{
fun bindItem(person: Person?)
{
var name: TextView =itemView.findViewById(R.id.name) as TextView
var age: TextView =itemView.findViewById(R.id.age) as TextView
name.text=person!!.name
age.text = person.age.toString()
}
}
}
答案 0 :(得分:0)
您在HomeFragment的onCreateView()中所做的第一件事是返回膨胀的布局。因此,onCreateView方法中的所有代码都是不可修改的。相反,只需保留对膨胀布局的引用,然后将其返回末尾即可。
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_home, container, false)
val recyclerView = view.findViewById<RecyclerView>(R.id.recyclerView)
personList=ArrayList<Person>()
layoutManager= LinearLayoutManager(this.context)
adapter= PersonListAdapter(personList, this.context!!)
recyclerView.layoutManager=layoutManager
recyclerView.adapter=adapter
for (i in 0..16) {
val person = Person()
person.name="Hello" + i
person.age = 20 + i
personList!!.add(person)
}
adapter!!.notifyDataSetChanged()
return view
}