小白一个,第一次接触RecyclerView,看了官方文档,并且下载了官方的代码,看的一头雾水。并且走了一些歪路。特此记录一下他的”真正的“使用方法。
第一步,放弃看官方的代码,
按照下图步骤创建一个fragment List
创建成功之后会多出5个文件。
从上往下分别是
- 数据模型类(Model)
- 数据处理与展示类(View)
- 布局类(xml)
首先吧新建的TestFragment 设置为你想要做的页面。
因为我是用的是BottomNavigationView,我是在这里这样设置的。
第二步,看上面新增的五个文件代码。
PlaceholderContent
该类的详细介绍,都写在代码注释里。
object PlaceholderContent {
/**
* An array of sample (placeholder) items.用来创建测试数据- 不用管它
*/
val ITEMS: MutableList<PlaceholderItem> = ArrayList()
/**
* A map of sample (placeholder) items, by ID. 用来创建测试数据- 不用管它
*/
val ITEM_MAP: MutableMap<String, PlaceholderItem> = HashMap()
private val COUNT = 25
init {
// Add some sample items. 用来创建测试数据
for (i in 1..COUNT) {
addItem(createPlaceholderItem(i))
}
}
private fun addItem(item: PlaceholderItem) { 用来创建测试数据- 不用管它
ITEMS.add(item)
ITEM_MAP.put(item.id, item)
}
//用来创建测试数据- 不用管它
private fun createPlaceholderItem(position: Int): PlaceholderItem {
return PlaceholderItem(position.toString(), "Item " + position, makeDetails(position))
}
//用来创建测试数据 - 不用管它
private fun makeDetails(position: Int): String {
val builder = StringBuilder()
builder.append("Details about Item: ").append(position)
for (i in 0..position - 1) {
builder.append("\nMore details information here.")
}
return builder.toString()
}
/**
//这是数据模型。这个页面最重要的就是他,当然你也可以创建自己的数据模型
* A placeholder item representing a piece of content.
*/
data class PlaceholderItem(val id: String, val content: String, val details: String) {
override fun toString(): String = content
}
}
TestItemRecyclerViewAdapter
该类的详细介绍,都写在代码注释里。
class TestItemRecyclerViewAdapter(
//使用该类时传递进来的数据源 PlaceholderItem 就是上面的数据模型,可以更换为自己的数据模型
private val values: List<PlaceholderItem>
) : RecyclerView.Adapter<TestItemRecyclerViewAdapter.ViewHolder>() {
//TestItemRecyclerViewAdapter.ViewHolder 他的作用请看 ViewHolder的注释
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(
FragmentTestItemBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
)
}
//ViewHolder:下方ViewHolder函数,用来读取到函数内部的组件(控件TextFiled什么的)
//position:列表下标
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = values[position]
holder.idView.text = item.id
holder.contentView.text = item.content
}
override fun getItemCount(): Int = values.size
//ViewHolder的作用是获取控件并赋值给一个变量方便 onBindViewHolder获取
inner class ViewHolder(binding: FragmentTestItemBinding) :
RecyclerView.ViewHolder(binding.root) {
val idView: TextView = binding.itemNumber
val contentView: TextView = binding.content
override fun toString(): String {
return super.toString() + " '" + contentView.text + "'"
}
}
}
TestFragment
该类的详细介绍,都写在代码注释里。
这个类主要看:adapter = TestItemRecyclerViewAdapter(getData())这段代码
class TestFragment : Fragment() {
private var columnCount = 1
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
columnCount = it.getInt(ARG_COLUMN_COUNT)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_test_list, container, false)
// Set the adapter
if (view is RecyclerView) {
with(view) {
layoutManager = when {
columnCount <= 1 -> LinearLayoutManager(context)
else -> GridLayoutManager(context, columnCount)
}
/*
*PlaceholderContent.ITEMS 是创建一组测试数据.
* 不用理会,我们要做的就是把自己的数据传递进去
*
* */
// adapter = TestItemRecyclerViewAdapter(PlaceholderContent.ITEMS)
adapter = TestItemRecyclerViewAdapter(getData())
}
}
return view
}
fun getData() :List<PlaceholderItem>{
var tempList: MutableList<PlaceholderItem> = mutableListOf()
for (i in 0..10){
tempList.add(PlaceholderItem(id = "id_$i", content = "content_$i", details = "details_$i"))
}
return tempList;
}
companion object {
// TODO: Customize parameter argument names
const val ARG_COLUMN_COUNT = "column-count"
// TODO: Customize parameter initialization
@JvmStatic
fun newInstance(columnCount: Int) =
TestFragment().apply {
arguments = Bundle().apply {
putInt(ARG_COLUMN_COUNT, columnCount)
}
}
}
}
代码编译之后:
fragment_test_item fragment_test_list
这两个是布局文件, fragment_test_item是用来展示列表中的每一个item(iOS中称为Cell)。