我正在尝试在片段中设置回收器视图。我有一个适合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
}
1条答案
按热度按时间pqwbnv8z1#