使用recyclerview on fragment类从firebase检索数据

ryevplcw  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(299)

我很难做出正确的决定 RecyclerView 工作。我已经找到了以前解决这个问题的方法,你必须评论或删除 recyclerView.setHasFixedSize(true); . 我尝试过这个方法,虽然它确实使应用程序成功运行,但结果是:

这是我的片段代码:

public class Leaderboard extends Fragment {

RecyclerView leaderboardList;
FirebaseFirestore fStore;
FirestoreRecyclerAdapter adapter;

public Leaderboard() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    fStore = FirebaseFirestore.getInstance();

    //Query
    Query query = fStore.collection("users")
            .orderBy("Score", Query.Direction.DESCENDING);

    //RecyclerOptions
    FirestoreRecyclerOptions<UserModel> options = new FirestoreRecyclerOptions.Builder<UserModel>()
            .setQuery(query, UserModel.class)
            .build();

     adapter = new FirestoreRecyclerAdapter<UserModel, UserViewHolder>(options) {
        @NonNull
        @Override
        public UserViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_leaderboard_single, parent, false);
            return new UserViewHolder(view);
        }

        @Override
        protected void onBindViewHolder(@NonNull UserViewHolder holder, int position, @NonNull UserModel model) {
            holder.username1.setText(model.getUsername());
            holder.email1.setText(model.getEmail());
            holder.score1.setText(model.getScore() + "");
        }
    };

    }

@Override
public void onStop() {
    super.onStop();
    adapter.stopListening();
}

@Override
public void onStart() {
    super.onStart();
    adapter.startListening();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_leaderboard, container, false);
    leaderboardList = view.findViewById(R.id.leaderboard_list);
    //leaderboardList.setHasFixedSize(true);
    leaderboardList.setLayoutManager(new LinearLayoutManager(getContext()));
    leaderboardList.setAdapter(adapter);

    return view;
}

private class UserViewHolder extends RecyclerView.ViewHolder {

    private TextView username1;
    private TextView email1;
    private TextView score1;

    public UserViewHolder(@NonNull View itemView) {
        super(itemView);

        username1 = itemView.findViewById(R.id.list_username);
        email1 = itemView.findViewById(R.id.list_email);
        score1 = itemView.findViewById(R.id.list_score);
    }
}

}
模型类:

public class UserModel {

private String username;
private String email;
private long score;

private UserModel(){

}

private UserModel(String username, String email, long score){
    this.username = username;
    this.email = email;
    this.score = score;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public long getScore() {
    return score;
}

public void setScore(long score) {
    this.score = score;
}

}
我在这里几乎是一个死胡同,我真的非常感谢任何帮助我可以得到。谢谢您!
编辑1:
列表\u排行榜\u single.xaml代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="20dp">

<TextView
    android:id="@+id/list_username"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Username"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/list_email"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Email"
    app:layout_constraintStart_toStartOf="parent"

    app:layout_constraintTop_toBottomOf="@id/list_username"/>

<TextView
    android:id="@+id/list_score"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Score"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/list_email"/>
</androidx.constraintlayout.widget.ConstraintLayout>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题