【发布时间】:2021-06-18 09:08:54
【问题描述】:
需要帮助。 我在 Stack Overflow 中检查了其他一些解决方案,但我没有到达让我的 Fragment 工作。
问题:当我显示我的 Fragment 时,我的屏幕非常空旷,并且出现错误“E/RecyclerView: No adapter attach;skipping layout”
3 个问题(肯定有关联):
- E/RecyclerView:没有连接适配器;跳过布局错误
- 当我使用“两个数据列表”初始化 MyAdapter 时:没有变化
- 当我到达片段时,我可以在 logcat 中看到 onResume() 被调用但屏幕上没有变化。
片段 (XML)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/ServiceIssuesReader"
tools:context=".ui.service.ServiceIssuesReaderFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="LinearLayoutManager"
/>
</RelativeLayout>
一行数据(一天)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:contentDescription="TODO"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="@id/icon"
android:text="Description"
android:textSize="12sp" />
<TextView
android:id="@+id/firstLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/secondLine"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toRightOf="@id/icon"
android:gravity="center_vertical"
android:text="Example application"
android:textSize="16sp" />
</RelativeLayout>
片段类
class ServiceIssuesReaderFragment: Fragment() {
var mAdapter: RecyclerView.Adapter<MyAdapter.ViewHolder>? = null
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val rootView = inflater.inflate(R.layout.fragment_issues_reader, container, false)
mAdapter = MyAdapter(mutableListOf<String>())
var recyclerView = view?.findViewById<RecyclerView>(R.id.my_recycler_view) as RecyclerView
recyclerView.setHasFixedSize(true)
recyclerView.layoutManager = LinearLayoutManager(context)
recyclerView.adapter = mAdapter
return rootView
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onResume() {
super.onResume()
// Adding stub data
val input: MutableList<String> = ArrayList()
for (i in 0..99) {
Log.d("onResume:: ", ">> + [Test$i]")
input.add("Test$i")
}
(mAdapter as MyAdapter).addItems(input)
mAdapter?.notifyDataSetChanged()
}
}
适配器类
class MyAdapter (private var values: MutableList<String>): RecyclerView.Adapter<MyAdapter.ViewHolder>() {
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
inner class ViewHolder(v: View) : RecyclerView.ViewHolder(v) {
// each data item is just a string in this case
var txtHeader: TextView
var txtFooter: TextView
var layout: View
init {
layout = v
txtHeader = v.findViewById<TextView>(R.id.firstLine)
txtFooter = v.findViewById<TextView>(R.id.secondLine)
}
}
fun add(position: Int, item: String) {
values.add(position, item)
notifyItemInserted(position)
}
fun remove(position: Int) {
values.removeAt(position)
notifyItemRemoved(position)
}
fun addItems (newValues: MutableList<String>){
values.addAll(newValues)
notifyDataSetChanged()
}
// Create new views (invoked by the layout manager)
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): ViewHolder {
// create a new view
val inflater = LayoutInflater.from(
parent.context
)
//val v: View = inflater.inflate(R.layout.row_layout, parent, false)
val v: View = inflater.inflate(R.layout.row_layout_issue, parent, false)
// set the view's size, margins, paddings and layout parameters
return ViewHolder(v)
}
// Replace the contents of a view (invoked by the layout manager)
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
val name = values[position]
holder.txtHeader.text = name
holder.txtHeader.setOnClickListener { remove(position) }
holder.txtFooter.text = "Footer: $name"
}
// Return the size of your dataset (invoked by the layout manager)
override fun getItemCount(): Int {
return values.size
}
}
我尝试在 onCreateView、onCreate 和 onResume 中初始化 Recycler,但没有更好的结果。 为什么它不起作用的想法?
【问题讨论】:
标签: android kotlin android-recyclerview