您好我正在尝试使用FireBaseRecyclerAdapter,但只运行populateViewHolder一次,而数据库FireBase有多个元素。我已经向FireBaseRecyclerAdapter添加了一个AdapterDataObserver和onItemRangeChanged方法,如果它运行的次数与我在数据库中的项目一样多,但仍然在populateView中运行。
我把我的代码:
FireBase数据库:
代码FirebaseRecyclerAdapter:
public class MyFireBaseAdapterRecycleView extends FirebaseRecyclerAdapter<ModeloDatos,MyFireBaseAdapterRecycleView.MyFireViewHolder> {
public MyFireBaseAdapterRecycleView(Class<ModeloDatos> modelClass, int modelLayout, Class<MyFireViewHolder> viewHolderClass, DatabaseReference ref) {
super(modelClass, modelLayout, viewHolderClass, ref);
}
@Override
protected void populateViewHolder(MyFireViewHolder viewHolder, ModeloDatos modelo, int position) {
viewHolder.bindDatos(modelo);
}
/** Clase ViewHolder interna */
public static class MyFireViewHolder extends RecyclerView.ViewHolder {
private TextView uidUsuario;
private TextView uidImagen;
public MyFireViewHolder(View itemView) {
super(itemView);
uidUsuario = (TextView)itemView.findViewById(R.id.tvIdUsuario);
uidImagen = (TextView)itemView.findViewById(R.id.tvIdImagen);
}
public void bindDatos(ModeloDatos datos){
uidUsuario.setText(datos.getUidUser());
uidImagen.setText(datos.getUidImagen());
}
}
Code AdapterDataObserver:
public class AdapterFireDataObserver extends RecyclerView.AdapterDataObserver {
@Override
public void onItemRangeChanged(int positionStart, int itemCount) {
super.onItemRangeChanged(positionStart, itemCount);
}
}
代码数据模型:
public class ModeloDatos {
private String uidUser;
private String uidImagen;
public ModeloDatos() {
}
public String getUidUser() {
return uidUser;
}
public void setUidUser(String uidUser) {
this.uidUser = uidUser;
}
public String getUidImagen() {
return uidImagen;
}
public void setUidImagen(String uidImagen) {
this.uidImagen= uidImagen;
}
}
来自Activty的代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_recycle);
adaptador = new MyFireBaseAdapterRecycleView(ModeloDatos.class, R.layout.fila_fire_recycle,
MyFireBaseAdapterRecycleView.MyFireViewHolder.class, FirebaseDatabase.getInstance().getReference().child("Imagenes"));
recyclerView = (RecyclerView)findViewById(R.id.rvSGOliver);
recyclerView.setHasFixedSize(true);
observer = new AdapterFireDataObserver();
adaptador.registerAdapterDataObserver(observer);
recyclerView.setAdapter(adaptador);
recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));