我有一个活动,其中显示了一些从Firebase检索到的数据。我有一个方法可以通过编程方式将回收者视图滚动到今天的活动,但是如果我需要这种方法才能正常工作,则需要两次进行我的活动……如何解决?
Planning.java:
public void setUpRecyclerView(){
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
private void loadDataFromFirebase(){
db = FirebaseFirestore.getInstance();
if(eventsArrayList.size()>0){
eventsArrayList.clear();}
db.collection("Planning").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
for(DocumentSnapshot querySnapshot: task.getResult()){
Events Nom = new Events(querySnapshot.getString("Nom"),querySnapshot.getString("Lieu"),querySnapshot.getString("Heure"),querySnapshot.getString("Minute"),querySnapshot.getString("Day"));
eventsArrayList.add(Nom);
}
tvLoad.setVisibility(View.GONE);
progressBar.setVisibility(View.GONE);
adapter = new MyRecyclerViewAdapter(Planning.this, eventsArrayList);
recyclerView.setAdapter(adapter);
pos = MyRecyclerViewAdapter.getPos();
recyclerView.scrollToPosition(pos);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(Planning.this, "Erreur(s) :", Toast.LENGTH_SHORT).show();
Log.v("---]---", e.getMessage());
}
});
}
@Override
protected void onStart(){
super.onStart();
pos = MyRecyclerViewAdapter.getPos();
recyclerView.scrollToPosition(pos);
}
MyRecyclerViewAdapter:
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewHolder> {
Planning planning;
ArrayList<Events> eventsArrayList;
static int pos;
public MyRecyclerViewAdapter(Planning planning, ArrayList<Events> eventsArrayList) {
this.planning = planning;
this.eventsArrayList = eventsArrayList;
}
@NonNull
@Override
public MyRecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
LayoutInflater layoutInflater = LayoutInflater.from(planning.getBaseContext());
View view = layoutInflater.inflate(R.layout.list_item, viewGroup, false);
return new MyRecyclerViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyRecyclerViewHolder holder, int position) {
Calendar cal = Calendar.getInstance();
holder.tvLieu.setVisibility(View.VISIBLE);
holder.tvDate.setVisibility(View.VISIBLE);
holder.cardView.setCardBackgroundColor(Color.WHITE);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) holder.tvNom.getLayoutParams();
params.gravity = Gravity.START;
holder.tvNom.setLayoutParams(params);
holder.tvNom.setText(eventsArrayList.get(position).getNom());
holder.tvLieu.setText(eventsArrayList.get(position).getLieu());
holder.tvDate.setText(eventsArrayList.get(position).getHeure() + "h" + eventsArrayList.get(position).getMinute());
if(TextUtils.isEmpty(eventsArrayList.get(position).getHeure())){
holder.tvDate.setVisibility(View.GONE);
holder.tvLieu.setVisibility(View.GONE);
params.gravity = Gravity.CENTER;
holder.tvNom.setLayoutParams(params);
holder.cardView.setCardBackgroundColor(Color.parseColor("#227c97"));
}
if(TextUtils.equals(eventsArrayList.get(position).getDay(), String.valueOf(cal.get(Calendar.DAY_OF_MONTH)))){
pos = position;
}
}
public static int getPos(){
return pos;
}
@Override
public int getItemCount() {
return eventsArrayList.size();
}
}