Android 无法更新RecyclerView
我无法更新我的RecyclerView。尝试了stackoverflow上的所有解决方案,但没有任何帮助。我尝试在代码中添加layoutmanager,硬编码项目数,创建新文件,检查是否有重复文件等。。我还调试并确认调用了“onAttachedToRecyclerView”,但是除了构造函数之外,不会调用getItemCount和适配器中列出的其他函数 适配器:Android 无法更新RecyclerView,android,android-recyclerview,kotlin,Android,Android Recyclerview,Kotlin,我无法更新我的RecyclerView。尝试了stackoverflow上的所有解决方案,但没有任何帮助。我尝试在代码中添加layoutmanager,硬编码项目数,创建新文件,检查是否有重复文件等。。我还调试并确认调用了“onAttachedToRecyclerView”,但是除了构造函数之外,不会调用getItemCount和适配器中列出的其他函数 适配器: class BaseAdapter(context: Context, val items: List<Item>
class BaseAdapter(context: Context, val items: List<Item>) :
RecyclerView.Adapter<LineBoxAdapter.BaseAdapter>() {
override fun getItemCount(): Int = items.size
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.textView.text = items[position].text
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int):
ViewHolder {
return ViewHolder(
LayoutInflater.from(parent.context).inflate(
R.layout.simple_item,
parent,
false
)
)
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val textView = itemView.findViewById<TextView>(R.id.title)
}
}
class BaseAdapter(上下文:上下文,val项:列表):
RecyclerView.Adapter(){
重写getItemCount():Int=items.size
覆盖BindViewHolder(holder:ViewHolder,位置:Int){
holder.textView.text=项目[位置].text
}
重写CreateViewHolder(父级:ViewGroup,viewType:Int):
观众{
返回视窗座(
LayoutFlater.from(parent.context).充气(
R.layout.simple_项目,
父母亲
假的
)
)
}
类ViewHolder(itemView:View):RecyclerView.ViewHolder(itemView){
val textView=itemView.findViewById(R.id.title)
}
}
更新recyclerView的函数:
val listView =
LayoutInflater.from(context).inflate(R.layout.list, viewGroup,
false)
val list = listView.findViewById<RecyclerView>(R.id.
LinearLayoutManager(context)
val adapter = BaseAdapter(context, items)
list.adapter = adapter
adapter.notifyDataSetChanged()
val列表视图=
LayoutFlater.from(上下文)。充气(R.layout.list,视图组,
(错误)
val list=listView.findViewById(R.id。
LinearLayoutManager(上下文)
val adapter=BaseAdapter(上下文、项目)
list.adapter=适配器
adapter.notifyDataSetChanged()
回收视图:
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/items_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:overScrollMode="never"
android:requiresFadingEdge="horizontal"
app:layoutManager="android.support.v7.widget.LinearLayoutManager" />
SIMPLE_ITEM:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:orientation="horizontal">
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="25" />
</LinearLayout>
简单项目:
您应该避免使用API中现有的类名 我将发布我的工作代码 简单项目:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text_simple_item"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
你能更新代码吗?我想有些东西已经被删除了。你想用
LineBoxAdapter实现什么。BaseAdapter
是一个可扩展列表还是一个视图持有者?你可能从来没有将LinearLayoutManager设置为RecyclerView的布局管理器。@EpicPandaForce LayoutManager设置在这里app:LayoutManager=
@André您是否在onCreate()中使用setContentView(R.layout.list)
?
class RecyclerAdapter(context: Context, val items: List<Item>) :
RecyclerView.Adapter<RecyclerAdapter.MyViewHolder>() {
override fun getItemCount(): Int = items.size
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.textView.text = items[position].itemName
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int):
MyViewHolder {
return MyViewHolder(
LayoutInflater.from(parent.context).inflate(
R.layout.simple_item,
parent,
false
)
)
}
class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val textView = itemView.findViewById<TextView>(R.id.text_simple_item)
}
}
class RecyclerActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_recycler)
val recyler = findViewById<RecyclerView>(R.id.rv_activity_recycler)
val list = mutableListOf(Item("Me"), Item("You"), Item("We"))
val adapter = RecyclerAdapter(this, list)
recyler.layoutManager = LinearLayoutManager(this)
recyler.adapter = adapter
}
}
class Item(val itemName: String)