im is developing android chat using firebase.
I tried to investigate this problem, but I cannot find good solutions.
I hope someone can help me.
my problem: I want RecyclerView to automatically scroll if a new item is added.
here is my code.
@Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ View rootView = inflater.inflate(R.layout.chatlayoutchat, container, false); Connectivity connectivity=new Connectivity(); if(connectivity.isConnected(getActivity())) { if(getArguments() != null) { Firebase.setAndroidContext(this.getActivity()); String ccustomersid = getArguments().getString("customersid"); String creferralae = getArguments().getString("referralae"); String ccustomersname = getArguments().getString("customersname"); customersid = ccustomersid; referralae = creferralae; customersname = ccustomersname; Firebase.setAndroidContext(this.getActivity()); editText = (EditText) rootView.findViewById(R.id.editText); sendbutton = (Button) rootView.findViewById(R.id.button); mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view); RecyclerView.LayoutManager layoutmgr = new LinearLayoutManager(getActivity()); mRecyclerView.setLayoutManager(layoutmgr); mRecyclerView.setItemAnimator(new DefaultItemAnimator()); final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity()); //linearLayoutManager.setReverseLayout(true); linearLayoutManager.setStackFromEnd(true); mRecyclerView.setLayoutManager(linearLayoutManager); from_user = customersid; to_user = referralae; LoggedInUser = customersname; //Log.v("NODE CREATED:", from_user + " " + to_user); ref_chatchildnode1 = firebase_chatnode.child(from_user + " " + to_user); //ref_chatchildnode2 = firebase_chatnode.child(to_user + " " + from_user); sendbutton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { newmsg = editText.getText().toString().trim(); ChatModel m = new ChatModel(); m.setSender(LoggedInUser); m.setMessage(newmsg); ref_chatchildnode1.push().setValue(m); //ref_chatchildnode2.push().setValue(m); editText.setText(""); } }); ref_chatchildnode1.addChildEventListener(new ChildEventListener() { @Override public void onChildAdded(DataSnapshot dataSnapshot, String s) { ChatModel chatmsg = dataSnapshot.getValue(ChatModel.class); chatmsgsList.add(chatmsg); } @Override public void onChildChanged(DataSnapshot dataSnapshot, String s) { } @Override public void onChildRemoved(DataSnapshot dataSnapshot) { } @Override public void onChildMoved(DataSnapshot dataSnapshot, String s) { } @Override public void onCancelled(FirebaseError firebaseError) { } }); } }else{ nointernet(); } showGlobalContextActionBar(); return rootView; } @Override public void onStart() { super.onStart(); mFirebaseAdapter1 = new FirebaseRecyclerAdapter<ChatModel, ChatMessageViewHolder>(ChatModel.class, R.layout.textview, ChatMessageViewHolder.class, ref_chatchildnode1) { @Override protected void populateViewHolder(ChatMessageViewHolder chatMessageViewHolder, ChatModel m, int i) { chatMessageViewHolder.sender.setText(m.getSender()); chatMessageViewHolder.msg.setText(m.getMessage()); } }; mRecyclerView.setAdapter(mFirebaseAdapter1); }
source share