第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

安卓 - 火庫無限分頁與聽眾

安卓 - 火庫無限分頁與聽眾

慕森王 2022-09-07 16:11:19
        我正在制作帶有firebase firestore數(shù)據(jù)庫的Android聊天應(yīng)用程序,我需要與偵聽器進(jìn)行無限分頁以進(jìn)行數(shù)據(jù)更改(新按摩,刪除按摩...我發(fā)現(xiàn)用kotlin和corse搜索的firebase文檔寫的博客文章,最終得到了這個(gè)代碼:    // firstTime variable shows if function is called from pagination or initially    private void addMessagesEventListener(boolean firstTime) {            // get collection            CollectionReference messagesCollection =                         chatsCollection.document(chat.getId()).collection(Constants.FIREBASE_MESSAGES_PATH);    // create query    Query query = messagesCollection.orderBy("timestamp", Query.Direction.DESCENDING);    // if NOT first time add startAt    if (!firstTime) {        query.startAt(startTimestamp);    }    //limit to 20 messages    query.limit(20).get().addOnSuccessListener(queryDocumentSnapshots -> {        if (!firstTime) {            endTimestamp = startTimestamp;        }        startTimestamp = (long) queryDocumentSnapshots.getDocuments().get(queryDocumentSnapshots.size() - 1).get("timestamp");        Query innerQuery = messagesCollection.orderBy("timestamp").startAt(startTimestamp);        if(!firstTime) {            innerQuery.endBefore(endTimestamp);        }        ListenerRegistration listener = innerQuery                .addSnapshotListener((queryDocumentSnapshots1, e) -> {                    if (e != null) {                        Log.w(TAG, "listen:error", e);                        return;                    }
查看完整描述

2 回答

?
慕姐8265434

TA貢獻(xiàn)1813條經(jīng)驗(yàn) 獲得超2個(gè)贊

試試這個(gè)


 @Override

 public void onStart() {

        super.onStart();


        loadFirstQuery();

 }



    public void loadFirstQuery() {



        if (firebaseAuth.getCurrentUser() != null) {


            contentListDashboard.clear();


            String currentUserId = firebaseAuth.getCurrentUser().getUid();              


            // what we do when recycler reach bottom

            recyclerProfileDashboard.addOnScrollListener(new RecyclerView.OnScrollListener() {

               @Override

               public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {

                   super.onScrolled(recyclerView, dx, dy);


                   // horizontal

                   //Boolean reachBottom = !recyclerView.canScrollHorizontally(-1);


                   // for vertical recycler

                   Boolean reachBottom = !recyclerView.canScrollVertically(-1);



                   if (reachBottom) {

                       loadMorePost(); // do load more post

                   }

               }

           });


            // RETRIEVING FIRST Query

            Query firstQuery = firebaseFirestore

                    .collection("ProfileDashboard")

                    .document(currentUserId)

                    .collection("ProfileInfo")

                    .orderBy("timestamp", Query.Direction.DESCENDING)

                    .limit(20);    


            firstQuery.addSnapshotListener(new EventListener<QuerySnapshot>() {

                @Override

                public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {


                    if (!documentSnapshots.isEmpty()) {

                        // please add if doc not empty

                        if (isFirstPageFirstLoad) {

                            lastVisible = documentSnapshots.getDocuments().get(documentSnapshots.size() - 1); // array 0, 1, 2

                        }


                        for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {


                            if (doc.getType() == DocumentChange.Type.ADDED) {


                                //String postId = doc.getDocument().getId();

                                contentProfileDashboard = doc.getDocument().toObject(ContentProfileDashboard.class);   



                                // if first page firest load true

                                if (isFirstPageFirstLoad) {

                                    contentListDashboard.add(contentProfileDashboard);

                                } else {

                                    contentListDashboard.add(0, contentProfileDashboard);

                                }



                                // fire the event

                                adapterProfileDashboard.notifyDataSetChanged();

                            }

                        }


                        isFirstPageFirstLoad = false;

                    }

                }

            });

        }

    }


   // Method to load more post

   public void loadMorePost() {


        if (firebaseAuth.getCurrentUser() != null) {


            String currentUserId = firebaseAuth.getCurrentUser().getUid();


            Query nextQuery = firebaseFirestore

                    .collection("ProfileDashboard")

                    .document(currentUserId)

                    .collection("ProfileInfo")

                    .orderBy("timestamp", Query.Direction.DESCENDING)

                    .startAfter(lastVisible)

                    .limit(20);


            nextQuery.addSnapshotListener(new EventListener<QuerySnapshot>() {

                @Override

                public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {


                    if (!documentSnapshots.isEmpty()) {


                        lastVisible = documentSnapshots.getDocuments().get(documentSnapshots.size() - 1);

                        for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {


                            if (doc.getType() == DocumentChange.Type.ADDED) {


                                //String postId = doc.getDocument().getId();

                                // contentSeen = doc.getDocument().toObject(ContentProfile.class);

                                // contentList.add(contentSeen);


                                contentProfileDashboard = doc.getDocument().toObject(ContentProfileDashboard.class);

                                contentListDashboard.add(contentProfileDashboard);


                                //adapterSeen.notifyDataSetChanged();

                                adapterProfileDashboard.notifyDataSetChanged();


                            }

                        }

                    }

                }

            });

        }

   }

任何問題?


查看完整回答
反對(duì) 回復(fù) 2022-09-07
?
白豬掌柜的

TA貢獻(xiàn)1893條經(jīng)驗(yàn) 獲得超10個(gè)贊

我發(fā)現(xiàn)了錯(cuò)誤。工作代碼是:


private void addMessagesEventListener(boolean firstTime) {

    CollectionReference messagesCollection = chatsCollection.document(chat.getId()).collection(Constants.FIREBASE_MESSAGES_PATH);

    Query query = messagesCollection.orderBy("timestamp", Query.Direction.DESCENDING);

    if (!firstTime) {

        query = query.startAt(startListen);

    }

    query.limit(20).get().addOnSuccessListener(queryDocumentSnapshots -> {

        if (!firstTime) {

            endListen = startListen;

        }

        startListen = queryDocumentSnapshots.getDocuments().get(queryDocumentSnapshots.size() - 1);


        Query innerQuery = messagesCollection.orderBy("timestamp").startAt(startListen);

        if(!firstTime) {

            innerQuery = innerQuery.endBefore(endListen);

        }

        ListenerRegistration listener = innerQuery

                .addSnapshotListener((queryDocumentSnapshots1, e) -> {

                    if (e != null) {

                        Log.w("SASA", "listen:error", e);

                        return;

                    }


                    for (DocumentChange dc : queryDocumentSnapshots1.getDocumentChanges()) {

                        Message message = dc.getDocument().toObject(Message.class);

                        switch (dc.getType()) {

                            case ADDED:

                                // add new message to list

                                messageListAdapter.addMessage(message);

                                if (firstTime) {

                                    messagesList.smoothScrollToPosition(0);

                                }

                                break;

                            case REMOVED:

                                // remove message from list

                                messageListAdapter.removeMessage(message);

                                break;

                        }

                    }

                });

        listeners.add(listener);

    });

}

錯(cuò)誤在于query = query.startAt(startListen)innerQuery = innerQuery.endBefore(endListen)


startListen & endListen 是 DocumentSnapshot type

我正在使用 sortedList 在我的適配器中

你應(yīng)該添加


private void detachListeners() {

    for(ListenerRegistration registration : listeners) {

        registration.remove();

    }

}

在 onDestroy 中分離所有偵聽器。


代碼正在偵聽添加新消息和刪除舊消息。


查看完整回答
反對(duì) 回復(fù) 2022-09-07
  • 2 回答
  • 0 關(guān)注
  • 98 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)