我如何在片段中使用 RecyclerView
How i can use RecyclerView inside a fragment
我尝试在片段中实现 recyclerView。当我尝试打开我的应用程序时,它崩溃了。我有一个 BottomNavigationMenu,我想在其中一个片段中实现 recyclerView。这是我的代码:
HomeFragment.kt
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.fragment_home.*
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
private var layoutManager: RecyclerView.LayoutManager? = null
private var adapter:
RecyclerView.Adapter<RecyclerAdapter.ViewHolder>? = null
class HomeFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
}
layoutManager = LinearLayoutManager(context)
recycler.layoutManager = layoutManager
adapter = RecyclerAdapter()
recycler.adapter = adapter
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_home, container, false)
}
companion object {
@JvmStatic
fun newInstance(param1: String, param2: String) =
HomeFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}
我如何正确实施它
将您对 RecyclerView
的通话从 onCreate
移至 onViewCreated
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
layoutManager = LinearLayoutManager(context)
recycler.layoutManager = layoutManager
adapter = RecyclerAdapter()
recycler.adapter = adapter
}
我尝试在片段中实现 recyclerView。当我尝试打开我的应用程序时,它崩溃了。我有一个 BottomNavigationMenu,我想在其中一个片段中实现 recyclerView。这是我的代码:
HomeFragment.kt
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.fragment_home.*
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
private var layoutManager: RecyclerView.LayoutManager? = null
private var adapter:
RecyclerView.Adapter<RecyclerAdapter.ViewHolder>? = null
class HomeFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
}
layoutManager = LinearLayoutManager(context)
recycler.layoutManager = layoutManager
adapter = RecyclerAdapter()
recycler.adapter = adapter
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_home, container, false)
}
companion object {
@JvmStatic
fun newInstance(param1: String, param2: String) =
HomeFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}
我如何正确实施它
将您对 RecyclerView
的通话从 onCreate
移至 onViewCreated
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
layoutManager = LinearLayoutManager(context)
recycler.layoutManager = layoutManager
adapter = RecyclerAdapter()
recycler.adapter = adapter
}