Android:重新绑定ListView或RecyclerView而不刷新标题

时间:2020-05-01 00:26:32

标签: android android-recyclerview android-listview header

我需要刷新/重新绑定ListView或RecyclerView内容,而不刷新标题项本身。

有关如何实现此目标的任何提示?

谢谢。

2 个答案:

答案 0 :(得分:1)

是的,您可以这样做。通常,标题在列表中的位置为0,因此标题位于列表的顶部。因此,对于您的列表,例如RecyclerView必须初始化适配器(在RecyclerView的情况下,您必须创建RecyclerView.Adapter类的访问器),此适配器具有许多用于更新适配器中数据的方法(notifyDataSetChanged(), notifyItemInserted()等),您可以使用其中一种方法,具体取决于您的目的。因此,您可以使用notifyItemRangeChanged(int positionStart, int itemCount)

您可以在官方documentation

中找到有关这些方法的更多信息。

答案 1 :(得分:1)

假设//your generator method or you can make your own widget class if you want that. Widget _entryField(String title, {bool isPassword = false}) { return Container( margin: EdgeInsets.symmetric(vertical: 10), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[ Text( title, style: TextStyle(fontWeight: FontWeight.bold, fontSize: 15), ), SizedBox( height: 10, ), TextField( obscureText: isPassword, decoration: InputDecoration( //labelText: title , // you can change this with the top text like you want hintText: "Please enter your $title" , border: InputBorder.none, fillColor: Color(0xfff3f3f4), filled: true)) ], ), ); } 位于header

假设您要绑定一次标头,然后在刷新后停止绑定标头:

当您调用position 0重新加载时,适配器中的notifyDataSetChanged()方法将被再次调用以刷新数据,请牢记这一点。您可以设置一个布尔值,以便将标头绑定一次。因此即使onBindViewHolder()被多次调用,标头也将绑定一次。

onBindViewHolder()
相关问题