Android 更改RecyclerView.Adapter时刷新片段视图
我下面介绍的是在Kotlin中实现的Silence示例应用程序,它是Google官方代码实验室面向开发人员的一部分。及 最后一步,我们被要求 修改您的神龛应用程序以更改显示的产品图像 从背景菜单中选择一个类别 在Android 更改RecyclerView.Adapter时刷新片段视图,android,kotlin,android-fragments,Android,Kotlin,Android Fragments,我下面介绍的是在Kotlin中实现的Silence示例应用程序,它是Google官方代码实验室面向开发人员的一部分。及 最后一步,我们被要求 修改您的神龛应用程序以更改显示的产品图像 从背景菜单中选择一个类别 在ProductGridFragment#onCreateView中,我在一个按钮上附加了一个onClick侦听器(类别列表,是按钮) 作为测试,我刚刚从Adapter中首先使用的列表中弹出了一个元素 theList = ProductEntry.initProductEntryList(
ProductGridFragment#onCreateView
中,我在一个按钮上附加了一个onClick侦听器(类别列表,是按钮)
作为测试,我刚刚从Adapter中首先使用的列表中弹出了一个元素
theList = ProductEntry.initProductEntryList(resources)
adapter = StaggeredProductCardRecyclerViewAdapter( theList )
val featured = view.findViewById(R.id.featured) as Button
// set on-click listener
featured.setOnClickListener {
theList.pop(1)
adapter.notifyDataSetChanged()
它不起作用。刷新视图还需要做其他事情吗?我在onCreateView
中看到的似乎只是第一次创建片段,我的意思是,当onClick
生成时,我觉得只要修改列表并在更改时通知适配器就足够了
我必须对视图对象执行任何操作吗
有什么提示吗
编辑1:
我分配了一个新的适配器,它现在可以工作了,这是非常好的;虽然我觉得它的性能不高,但因为notifyfordatachanges并没有真正按预期使用
featured.setOnClickListener {
theList = ProductEntry.initProductEntryList(resources, "featured")
this.adapter = StaggeredProductCardRecyclerViewAdapter(
theList )
view.recycler_view.adapter = this.adapter
this.adapter.notifyDataSetChanged()
}
在Java/Kotlin中,总是按值传递,而不是按引用传递。因此,当您将列表传递到交错的ProductCardRecycleServiceAdapter(列表)
时,您应该从适配器更新列表,而不是传递的列表,例如:
类交错ProductCardRecycleServiceAdapter(私有值列表:列表){
private var listOfItems=列表
乐趣移除项(位置:Int){
listOfItems=listOfItems.remove(位置)
notifyDatasetChanged()
}
}
val featured=view.findViewById(R.id.featured)作为按钮
//单击设置侦听器
featured.setOnClickListener{
适配器。拆卸项目(1)
}
编辑:
我去了代码库,您在交错的ProductCardRecycleServiceAdapter
中的不可变列表上操作。例如,如果要根据“特色”筛选列表中的项目,应执行以下操作:
类交错ProductCardRecycleServiceAdapter(private val initList:List?):RecycleView.Adapter(){
私有变量productList:List?=initList
覆盖getItemViewType(位置:Int):Int{
返回位置%3
}
覆盖CreateViewHolder(父级:ViewGroup,viewType:Int):交错产品卡ViewHolder{
var layoutId=R.layout.shr\U交错\U产品\U卡\U优先
如果(viewType==1){
layoutId=R.layout.shr\u交错\u产品\u卡\u秒
}else if(viewType==2){
layoutId=R.layout.shr\U交错\U产品\U卡\U第三
}
val layoutView=LayoutInflater.from(parent.context)。充气(layoutId,parent,false)
返回交错的ProductCardViewHolder(布局视图)
}
覆盖BindViewHolder(holder:StaggeredProductCardViewHolder,位置:Int){
if(productList!=null&&position
在ProductGridFragment中:
。。。
featured.setOnClickListener{
replaceList(ProductEntry.initProductEntryList(参考资料,“特色”))
}
在Java/Kotlin中,您总是按值传递,而不是按引用传递。因此,当您将列表传递到交错的ProductCardRecycleServiceAdapter(列表)
时,您应该从适配器更新列表,而不是传递的列表,例如:
类交错ProductCardRecycleServiceAdapter(私有值列表:列表){
private var listOfItems=列表
乐趣移除项(位置:Int){
listOfItems=listOfItems.remove(位置)
notifyDatasetChanged()
}
}
val featured=view.findViewById(R.id.featured)作为按钮
//单击设置侦听器
featured.setOnClickListener{
适配器。拆卸项目(1)
}
编辑:
我去了代码库,您在交错的ProductCardRecycleServiceAdapter
中的不可变列表上操作。例如,如果要根据“特色”筛选列表中的项目,应执行以下操作:
类交错ProductCardRecycleServiceAdapter(private val initList:List?):RecycleView.Adapter(){
私有变量productList:List?=initList
覆盖getItemViewType(位置:Int):Int{
返回位置%3
}
覆盖CreateViewHolder(父级:ViewGroup,viewType:Int):交错产品卡ViewHolder{
var layoutId=R.layout.shr\U交错\U产品\U卡\U优先
如果(viewType==1){
layoutId=R.layout.shr\u交错\u产品\u卡\u秒
}else if(viewType==2){
layoutId=R.layout.shr\U交错\U产品\U卡\U第三
}
val layoutView=LayoutInflater.from(parent.context)。充气(layoutId,parent,false)
返回交错的ProductCardViewHolder(布局视图)
}
覆盖BindViewHolder(holder:StaggeredProductCardViewHolder,位置:Int){
if(productList!=null&&position
在ProductGridFragment中:
。。。
featured.setOnClickListener{
adapter.replaceList(ProductEntry.initProductEntryLi
supportFragmentManager.beginTransaction().detach(yourFragment).commitNow()
supportFragmentManager.beginTransaction().attach(yourFragment).commitNow()