Guys, this question: is there any possibility to set the sound when a new record appears in RecyclerView (in my case, this message). There was an attempt to fasten the whole thing to the appearance of a new element on the screen, but the sound also appears just when scrolling correspondence (which was to be expected). The whole thing is tied to the FireBaseRecyclerAdapter.

canPlaySound = false;// разрешено ли воспроизводить звук mFBAdapter = new FirebaseRecyclerAdapter<ChatMessage, FirechatMsgViewHolder>( ChatMessage.class, R.layout.message, FirechatMsgViewHolder.class, mDatabaseReference.child(romName) ) { @Override protected void populateViewHolder(FirechatMsgViewHolder firechatMsgViewHolder, ChatMessage chatMessage, int i) { mProgressBar.setVisibility(ProgressBar.INVISIBLE); if (chatMessage.getUid().equals(mUid)) { firechatMsgViewHolder.setIsSender(true); } else { firechatMsgViewHolder.setIsSender(false); } firechatMsgViewHolder.msgText.setText(chatMessage.getText()); firechatMsgViewHolder.userText.setText(chatMessage.getName()); mUsername = mFirebaseUser.getDisplayName(); if (mPhotoUrl.equals("")) { mPhotoUrl = mFirebaseUser.getPhotoUrl().toString(); } Glide.with(ChatFragment.this). load(chatMessage.getPhotoUrl()).into(firechatMsgViewHolder.userImage); // если при входе в чат все элементы прогруженны - canPlaySound = true; if (canPlaySound) { sp.play(soundIdShot, 1, 1, 0, 0, 1); sp.play(soundIdExplosion, 1, 1, 0, 0, 1); } } }; mFBAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() { @Override public void onItemRangeInserted(int positionStart, int itemCount) { super.onItemRangeInserted(positionStart, itemCount); int chatMsgCount = mFBAdapter.getItemCount(); int lastVisiblePosition = mLayoutManager.findLastCompletelyVisibleItemPosition(); if (lastVisiblePosition == -1 || (positionStart >= (chatMsgCount - 1) && lastVisiblePosition == (positionStart - 1))) { mMsgRecyclerView.scrollToPosition(positionStart); } } }); mMsgRecyclerView.setLayoutManager(mLayoutManager); mMsgRecyclerView.setAdapter(mFBAdapter); mUsername = mFirebaseUser.getDisplayName().toString(); canPlaySound = true; 

available methods for inheritance

    1 answer 1

    You can inherit from FirebaseRecyclerAdapter and make your own adapter . As soon as the number of elements changes for the most part, you lose the sound. In order to track this change, you need to override the onDataChanged method in your adapter and create a variable that will compare the current number of elements with the new number in the adapter

     int mCurrentItemsCount = 0; @Override protected void onDataChanged() { super.onDataChanged(); if (mCurrentItemsCount < getItemCount()) { //проигрываем звук } mCurrentItemsCount = getItemCount(); } 

    update: An example of creating your own adapter, which is inherited from Firebase

     public class MessageAdapter extends FirebaseRecyclerAdapter<MessageModel, MessageHolder> { int mCurrentItemsCount = 0; public MessageAdapter(Query query) { super(MessageModel.class, R.layout.item_list, MessageHolder.class, query); } @Override protected void populateViewHolder(MessageHolder viewHolder, MessageModel model, int position) { //do something } @Override protected void onDataChanged() { super.onDataChanged(); if (mCurrentItemsCount < getItemCount()) { //проигрываем звук } mCurrentItemsCount = getItemCount(); } } 

    Call example:

     DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference(); Query query = databaseReference.child("messages") .limitToFirst(1000).orderByKey(); MessageAdapter adapter = new MessageAdapter(query); recyclerView.setAdapter(adapter); 
    • It is not entirely clear what you mean. those. I can only get the onDataChanged method by accessing the databaseReference.addValueEventListener (new ValueEventListener (), etc. I did not find this method in the FirebaseRecyclerAdapter .. ( - Dmitry Samoylov
    • You need to create your adapter that will inherit from Firebase. Then you get access to the overridable methods. Like this -> public class MessageAdapter extends FirebaseRecyclerAdapter <MessageModel, MessageHolder> {...} - Andrew Grow
    • Added in response to the example of creating your own adapter, which is inherited from Firebase. - Andrew Grow
    • I do not understand what I'm doing wrong .... all the same story. Attached to the question screen with all available methods for inheritance. - Dmitry Samoylov
    • Dmitry, you do not read carefully. You create an adapter directly from the FirebaseRecyclerAdapter. And you need to create a class that will inherit from the FirebaseRecyclerAdapter. And only in your class, you can do all these manipulations with overrides. - Andrew Grow