android


How to scroll to the bottom of a RecyclerView using MVP model? setStackFromEnd doesn't work


Issue:
On user1 login the messages list at the last message position, but when user2 send a message, user1 view is not auto scrolled to user2 message.
I want to auto scroll to the recent message(recieved message, last item in the list), I tried many ways, but I cant figure out why its not working.
I use MVP model, the main classes are:
1. Interactor to get all objects on the messages node using addChildEventListener.
2. Message Presenter where I call my interactor.
3. Recycler View Adapter(CustomMessageRecyclerAdapter).
4. Chat Fragment where I call requestMessages(); from the presenter and get the messages at the recycler view adapter.
MessageInteractor:
public class MessageInteractor {
private final DatabaseReference mMessagesRef = FirebaseDatabase.getInstance()
.getReferenceFromUrl("https://FIREBASE/messages/public/");
...
public MessageInteractor(MessagePresenter pre) {
this.presenter = pre;
}
...
public void request() {
mMessagesRef.addChildEventListener(new ChildEventListener() {
final ArrayList<Message> mListOfMessages = new ArrayList<>();
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
mListOfMessages.add(dataSnapshot.getValue(Message.class));
}
.....
}
...
Message Presenter Implementation:
public class MessagePresenterImpl implements MessagePresenter {
private final MessageAdapterView adapterView;
private final MessageInteractor interactor;
public MessagePresenterImpl(MessageAdapterView view) {
this.adapterView = view;
this.interactor = new MessageInteractor(this);
}
#Override
public void sendMessageToAdapter(ArrayList<Message> message) {
adapterView.addAll(message);
}
#Override
public void requestMessages() {
interactor.request();
}
}
public interface MessagePresenter {
void sendMessageToAdapter(ArrayList<Message> messages);
void requestMessages();
}
CustomMessageRecyclerAdapter:
public class CustomMessageRecyclerAdapter extends RecyclerView.Adapter<CustomMessageRecyclerAdapter.ViewHolder> implements MessageAdapterView {
...
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_message, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mMessageTextView.getGravity();
}
#Override
public int getItemCount() {
return mMessageList.size();
}
#Override
public void addAll(ArrayList<Message> message) {
mMessageList.clear();
mMessageList.addAll(message);
notifyDataSetChanged();
}
#Override
public void request() {
presenter.requestMessages();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView mAuthorTextView;
private TextView mMessageTextView;
public ViewHolder(View itemView) {
super(itemView);
itemView.findViewWithTag(getAdapterPosition());
}
}
chat Fragment:
public class ChatFragment extends Fragment implements View.OnClickListener, ClickListnerForUserNames {
private RecyclerView mMessagesListView;
private CustomMessageRecyclerAdapter adapter;
private FirebaseChatMessagePresenterImpl presenter;
#Override
public void onAttach(Context context) {
super.onAttach(context);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_chat_service, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
createUI(view);
}
#Override
public void onStart() {
super.onStart();
fillUI();
}
private void createUI(View view) {
mMessagesListView = (RecyclerView) view.findViewById(R.id.chat_recycler_view);
}
private void fillUI() {
adapter = new CustomMessageRecyclerAdapter(getActivity().getIntent().getStringExtra("username"), getActivity().getIntent().getStringExtra("uid"));
adapter.request();
mMessagesListView.setHasFixedSize(true);
mMessagesListView.setItemAnimator(new DefaultItemAnimator());
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setStackFromEnd(true);
mMessagesListView.setLayoutManager(layoutManager);
mMessagesListView.smoothScrollToPosition(adapter.getItemCount());
mMessagesListView.setAdapter(adapter);
presenter = new FirebaseChatMessagePresenterImpl();
}
...
}
Fragment xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/chat"
tools:context="com.ui.chat.view.ChatScreenActivity">
<android.support.v7.widget.RecyclerView
android:id="#id/chat_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/chat_message_layout"
android:scrollbars="vertical"
/>
....
Thanks

Related Links

How to get a list of key frames (sync frames) time stamps for a video file in Android?
How can I get textid + 5 value?
notifyDataSetChanged on BaseAdapter without updated data in array
hide software keyboard after resume from another app
Android scroll bug > only visible on touch
How does one use Resources.getFraction()?
Using TextView resources in non activity class. MainActivity stops with “Exception despatching input event.”
Simplest way to animate button in Android menu
How to upgrade database version in android?
Is it possible to add View into included layout in xml?
How to change Android notification bar drawer running apps background color?
Open chat screen Skype from other app
getMeasuredHeight() and width returning 0 after measure()
Missing application on running apps menu on android tablet
Android Spinner - System view VS User view
This thread forbids HTTP requests

Categories

HOME
url-redirection
grok
localization
scikit-learn
webdriver
camera
google-cloud-functions
carousel
simulink
cakephp-3.4
java.util.logging
jsonpath
pybossa
android-intent
package-managers
aws-sdk
fileserver
wolframalpha
google-picker
imessage
selinux
asciidoctor-pdf
joptionpane
custom-post-type
ceylon
browser-sync
jaws
winpe
livelink
weather
cart
dumpbin
sammy.js
gojs
hunspell
batching
imx6
engine.io
sharepoint-workflow
protein-database
signals-slots
setup.py
kill
azure-arm
android-softkeyboard
datacachefactory
optimizely
ovf
rserve
lightning
singleinstance
boxing
css-transforms
google-photos
shinobi
pocketsphinx-android
winusb
windows-azure-queues
respondcms
deep
upx
libav
dynatree
visual-sourcesafe-2005
dronekit-android
git-ftp
jquery-forms-plugin
dot.js
openbabel
qtwebengine
contrast
linkedin-jsapi
create.js
mobility
ora-00904
reflector
live555
dot42
git-repo
ecslidingviewcontroller
mbunit
abstract-data-type
android-loadermanager
catransform3d
multiplatform
mbeans
umra
w3c-geolocation
inline-formset
aspmenu
build-environment
cuda.net
fdf
ifilter
blind

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App