public void displayNewIncomingMessageItem ( ApptentiveMessage message ) { messagingActionHandler . sendEmptyMessage ( MSG_REMOVE_STATUS ) ; // Determine where to insert the new incoming message. It will be in front of any eidting // area, i.e. composing, Who Card ... int insertIndex = listItems . size ( ) ; // If inserted onto the end, then the list will have grown by one. outside_loop : // Starting at end of list, go back up the list to find the proper place to insert the incoming message. for ( int i = listItems . size ( ) - 1 ; i > 0 ; i -- ) { MessageCenterListItem item = listItems . get ( i ) ; switch ( item . getListItemType ( ) ) { case MESSAGE_COMPOSER : case MESSAGE_CONTEXT : case WHO_CARD : case STATUS : insertIndex -- ; break ; default : // Any other type means we are past the temporary items. break outside_loop ; } } listItems . add ( insertIndex , message ) ; messageCenterRecyclerViewAdapter . notifyItemInserted ( insertIndex ) ; int firstIndex = messageCenterRecyclerView . getFirstVisiblePosition ( ) ; int lastIndex = messageCenterRecyclerView . getLastVisiblePosition ( ) ; boolean composingAreaTakesUpVisibleArea = firstIndex <= insertIndex && insertIndex < lastIndex ; if ( composingAreaTakesUpVisibleArea ) { View v = messageCenterRecyclerView . getChildAt ( 0 ) ; int top = ( v == null ) ? 0 : v . getTop ( ) ; updateMessageSentStates ( ) ; // Restore the position of listview to composing view messagingActionHandler . sendMessage ( messagingActionHandler . obtainMessage ( MSG_SCROLL_FROM_TOP , insertIndex , top ) ) ; } else { updateMessageSentStates ( ) ; } }
Call only from handler .