我正在尝试在 viewpager2 的每个布局中创建一个 RecyclerView
对于 viewpager2 我正在使用 RecyclerView 适配器来膨胀布局
1.主要布局
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/id_view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/id_settings_package_tab_layout" />
2.我正在调用适配器
id_view_pager.adapter = MultiViewAdapter(this, list)
3. 多视图适配器
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
when (viewType) {
R.layout.fragment_pkg_set_type -> {
var view: View = layoutInflater.inflate(R.layout.fragment_layout, parent, false)
Log.d("log", "onCreateViewHolder : Layout inflating is called")
return ViewHolder1(view)
}
else -> {
var view: View =
layoutInflater.inflate(R.layout.fragment_layout_others, parent, false)
return ViewHolder2(view)
}
}
}
4. 片段布局
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/id_pkg_set_type_list_rv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="@layout/fragment_pkg_type_list_ui"/>
<EditText
android:id="@+id/id_pkg_type_value"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:hint="Add Type"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/id_pkg_type_add"
app:layout_constraintStart_toStartOf="parent" />
- 现在我得到了所有膨胀的布局并且
EditText
是可见的 - 现在为了加载 RecyclerView 我又写了一个文件对应
fragment_layout
文件
5.LayoutFragment.kt
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
var myDataset = arrayOf("one", "two", "three", "four", "five")
var viewManager = LinearLayoutManager(context)
var viewAdapter = PackageTypeAdapter(context, myDataset)
viewManager.orientation = LinearLayoutManager.VERTICAL
id_pkg_set_type_list_rv.setHasFixedSize(true)
id_pkg_set_type_list_rv.layoutManager = viewManager
id_pkg_set_type_list_rv.adapter = viewAdapter
viewAdapter.notifyDataSetChanged()
}
class PackageTypeAdapter(context: Context?, data: Array<String>) :
RecyclerView.Adapter<PackageTypeAdapter.MyViewHolder>() {
var mData: Array<String>
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): PackageTypeAdapter.MyViewHolder {
var layoutInflater = LayoutInflater.from(parent.context)
var view = layoutInflater.inflate(R.layout.fragment_pkg_type_list_ui, parent, false)
return MyViewHolder(view)
}
override fun getItemCount(): Int {
return mData.size
}
override fun onBindViewHolder(holder: PackageTypeAdapter.MyViewHolder, position: Int) {
holder.text_value.text = mData[position]
}
inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var text_value: TextView
init {
text_value = itemView.findViewById(R.id.id_text_value)
}
}
init {
mData = data
}
}
- 不幸的是它没有读取这个文件
- 所以,我没有得到 RecyclerView 列表
如何将嵌套的 RecyclerView 加载到使用 RecyclerView 将布局膨胀到 viewpager2 的 Viewpager2 中?