簡體   English   中英

添加項目時防止recyclerview刷新項目

[英]Prevent recyclerview from refreshing items when an item is added

我想在 RecyclerView 中顯示總結果(總項目)。 RecyclerView 的項目可能會根據用戶選擇的過濾器而有所不同。 問題是,當我更改 Firestore 數據庫中的某些內容時,項目總數不再基於篩選結果,相反,Firestore 返回整個集合中的項目總數。

private void load() {
    Query query = createQuery();
    FirestoreRecyclerOptions<Student> options = new FirestoreRecyclerOptions.Builder<Student>()
            .setQuery(query, Student.class)
            .build();

    myStudentAdapter = new MyStudentAdapter(options) {
        @Override
        public void onDataChanged() {
            super.onDataChanged();
            tv_total.setText(String.valueOf(getItemCount()));
        }
    };

    rv.setAdapter(myStudentAdapter);
    myStudentAdapter.startListening();
}

private Query createQuery() {
    if (!search.isEmpty()) {
        if (!course.isEmpty() && !year.isEmpty() && !block.isEmpty()) {
            return db.collection("Student")
                    .orderBy("fullName")
                    .whereEqualTo("course", course)
                    .whereEqualTo("year", year)
                    .whereEqualTo("block", block)
                    .startAt(search)
                    .endAt(search + '\uf8ff');
        } else if (!course.isEmpty() && !year.isEmpty() && block.isEmpty()) {
            return db.collection("Student")
                    .orderBy("fullName")
                    .whereEqualTo("course", course)
                    .whereEqualTo("year", year)
                    .startAt(search)
                    .endAt(search + '\uf8ff');
        } else if (!course.isEmpty() && year.isEmpty() && !block.isEmpty()) {
            return db.collection("Student")
                    .orderBy("fullName")
                    .whereEqualTo("course", course)
                    .whereEqualTo("block", block)
                    .startAt(search)
                    .endAt(search + '\uf8ff');
        } else if (course.isEmpty() && !year.isEmpty() && !block.isEmpty()) {
            return db.collection("Student")
                    .orderBy("fullName")
                    .whereEqualTo("year", year)
                    .whereEqualTo("block", block)
                    .startAt(search)
                    .endAt(search + '\uf8ff');
        } else if (!course.isEmpty() && year.isEmpty() && block.isEmpty()) {
            return db.collection("Student")
                    .orderBy("fullName")
                    .whereEqualTo("course", course)
                    .startAt(search)
                    .endAt(search + '\uf8ff');
        } else if (course.isEmpty() && !year.isEmpty() && block.isEmpty()) {
            return db.collection("Student")
                    .orderBy("fullName")
                    .whereEqualTo("year", year)
                    .startAt(search)
                    .endAt(search + '\uf8ff');
        } else if (course.isEmpty() && year.isEmpty() && !block.isEmpty()) {
            return db.collection("Student")
                    .orderBy("fullName")
                    .whereEqualTo("block", block)
                    .startAt(search)
                    .endAt(search + '\uf8ff');
        } else {
            return db.collection("Student")
                    .orderBy("fullName")
                    .startAt(search)
                    .endAt(search + '\uf8ff');
        }
    } else {
        if (!course.isEmpty() && !year.isEmpty() && !block.isEmpty()) {
            return db.collection("Student")
                    .orderBy("fullName")
                    .whereEqualTo("course", course)
                    .whereEqualTo("year", year)
                    .whereEqualTo("block", block);
        } else if (!course.isEmpty() && !year.isEmpty() && block.isEmpty()) {
            return db.collection("Student")
                    .orderBy("fullName")
                    .whereEqualTo("course", course)
                    .whereEqualTo("year", year);
        } else if (!course.isEmpty() && year.isEmpty() && !block.isEmpty()) {
            return db.collection("Student")
                    .orderBy("fullName")
                    .whereEqualTo("course", course)
                    .whereEqualTo("block", block);
        } else if (course.isEmpty() && !year.isEmpty() && !block.isEmpty()) {
            return db.collection("Student")
                    .orderBy("fullName")
                    .whereEqualTo("year", year)
                    .whereEqualTo("block", block);
        } else if (!course.isEmpty() && year.isEmpty() && block.isEmpty()) {
            return db.collection("Student")
                    .orderBy("fullName")
                    .whereEqualTo("course", course);
        } else if (course.isEmpty() && !year.isEmpty() && block.isEmpty()) {
            return db.collection("Student")
                    .orderBy("fullName")
                    .whereEqualTo("year", year);
        } else if (course.isEmpty() && year.isEmpty() && !block.isEmpty()) {
            return db.collection("Student")
                    .orderBy("fullName")
                    .whereEqualTo("block", block);
        } else {
            return db.collection("Student")
                    .orderBy("fullName");
        }
    }
}

我想在 RecyclerView 中顯示總結果(總項目)。

如果您想知道查詢返回的文檔總數,那么您必須在查詢object 上調用count() ,它:

返回一個查詢,該查詢對該查詢的結果集中的文檔進行計數。

如果您需要在適配器 class 中進行計數,那么您必須覆蓋FirestoreRecyclerAdapter class 中存在的getItemCount() ,其中:

返回適配器中快照的總數。

如果將另一個查詢傳遞給options object 后需要刷新結果,則必須調用updateOptions(@NonNull FirestoreRecyclerOptions options)才能生效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM