android-fragments 如何为Fragment重写此代码以设置回收程序视图?

3b6akqbq  于 2022-11-13  发布在  Android
关注(0)|答案(1)|浏览(123)

我正在尝试在片段中设置回收器视图。我有一个适合MainActivity的代码。请帮助我将其更改为片段。![在此处输入图像描述][1]

class MainActivity2 : AppCompatActivity() {

private var recyclerView: RecyclerView? = null
private var charItem: ArrayList<CharItem>? = null
private var gridLayoutManager: GridLayoutManager? = null
private var alphaAdapters: AlphaAdapters? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    recyclerView = findViewById(R.id.recycler_view_item)
    gridLayoutManager =
        GridLayoutManager(applicationContext, 3, LinearLayoutManager.VERTICAL, false)
    recyclerView?.layoutManager = gridLayoutManager
    recyclerView?.setHasFixedSize(true)

    charItem = ArrayList()
    charItem = setAlphas()
    alphaAdapters = AlphaAdapters(applicationContext, charItem!!)
    recyclerView?.adapter = alphaAdapters

}

private fun setAlphas(): ArrayList<CharItem> {

    var arrayList: ArrayList<CharItem> = ArrayList()

    arrayList.add(CharItem(R.mipmap.entertainment_pic_foreground, "A Latter"))
    arrayList.add(CharItem(R.mipmap.general_pic_foreground, "B Latter"))
    arrayList.add(CharItem(R.mipmap.health_pic_foreground, "C Latter"))
    arrayList.add(CharItem(R.mipmap.science_pic_foreground, "D Latter"))
    arrayList.add(CharItem(R.mipmap.technology_pic_foreground, "E Latter"))

    return arrayList
}
pqwbnv8z

pqwbnv8z1#

Class MainFragment2: Fragment() {
     private var recyclerView: RecyclerView? = null
     private var charItem: ArrayList<CharItem>? = null
     private var gridLayoutManager: GridLayoutManager? = null
     private var alphaAdapters: AlphaAdapters? = null
override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    return inflater.inflate(
        R.layout.fragment_category, container, false
    )
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    initViews()
}
private fun initViews() {
    recyclerView = findViewById(R.id.recycler_view_item)
    gridLayoutManager =
        GridLayoutManager(applicationContext, 3, LinearLayoutManager.VERTICAL, false)
    recyclerView?.layoutManager = gridLayoutManager
    recyclerView?.setHasFixedSize(true)
    charItem = ArrayList()
    charItem = setAlphas()
    alphaAdapters = AlphaAdapters(applicationContext, charItem!!)
    recyclerView?.adapter = alphaAdapters
}

private fun setAlphas(): ArrayList<CharItem> {
    var arrayList: ArrayList<CharItem> = ArrayList()
    arrayList.add(CharItem(R.mipmap.entertainment_pic_foreground, "A Latter"))
    arrayList.add(CharItem(R.mipmap.general_pic_foreground, "B Latter"))
    arrayList.add(CharItem(R.mipmap.health_pic_foreground, "C Latter"))
    arrayList.add(CharItem(R.mipmap.science_pic_foreground, "D Latter"))
    arrayList.add(CharItem(R.mipmap.technology_pic_foreground, "E Latter"))
    return arrayList
}
}

相关问题