I am making a chat app where I need to scroll to the bottom of the recycler view to show the last message. The main point of posting this question is to ask how to scroll to the bottom of the recyclerView. I have already tried this below line of code in the readMessage() method but still it is not working.
groupChatRecyclerView.smoothScrollToPosition(messageAdapter.getItemCount());
and for the recyclerView: groupChatRecyclerView.setHasFixedSize(true);
and for the LinearLayoutManager:
linearLayoutManager = new LinearLayoutManager(getApplicationContext()); linearLayoutManager.setStackFromEnd(true); linearLayoutManager.setReverseLayout(false); groupChatRecyclerView.setLayoutManager(linearLayoutManager);
Please can anyone help me? Code:
private void readMessage() {
mchat = new ArrayList<>();
reference = groupReference.child("Messages");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
mchat.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
GroupChats chat = snapshot.getValue(GroupChats.class);
mchat.add(chat);
messageAdapter = new GroupMessageAdapter(GroupMessageActivity.this, mchat, (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE));
groupChatRecyclerView.setAdapter(messageAdapter);
//linearLayoutManager.smoothScrollToPosition(groupChatRecyclerView, null, messageAdapter.getItemCount()-1);
// groupChatRecyclerView.smoothScrollToPosition(messageAdapter.getItemCount());
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
GroupMessageAdapter code:
package com.example.chats.Adapter;
public class GroupMessageAdapter extends
RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context mContext;
public static final int MSG_TYPE_LEFT = 0;
public static final int MSG_TYPE_RIGHT = 1;
public static final int MSG_TYPE_IMAGE_LEFT = 2;
public static final int MSG_TYPE_IMAGE_RIGHT = 3;
private List<GroupChats> mChat;
FirebaseUser fuser;
LayoutInflater inflater;
DatabaseReference userRef = FirebaseDatabase.getInstance().getReference("Users");
public GroupMessageAdapter(Context mContext, List<GroupChats> mChat, LayoutInflater inflater) {
this.mContext = mContext;
this.mChat = mChat;
this.inflater = inflater;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder = null;
inflater = LayoutInflater.from(parent.getContext());
switch (viewType){
case MSG_TYPE_LEFT:
View textOnlyLViewHolder = inflater.inflate(R.layout.group_item_left, parent,false);
viewHolder = new LeftMessageG(textOnlyLViewHolder);
break;
case MSG_TYPE_RIGHT:
View textOnlyRViewHolder = inflater.inflate(R.layout.chat_item_right, parent,false);
viewHolder = new RightMessageG(textOnlyRViewHolder);
break;
case MSG_TYPE_IMAGE_LEFT:
View imageOnlyLViewHolder = inflater.inflate(R.layout.group_image_left, parent,false);
viewHolder = new LeftImageG(imageOnlyLViewHolder);
break;
case MSG_TYPE_IMAGE_RIGHT:
View imageOnlyRViewHolder = inflater.inflate(R.layout.image_right, parent,false);
viewHolder = new RightImageG(imageOnlyRViewHolder);
break;
default:
}
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
GroupChats chat = mChat.get(position);
try{
String mess = chat.getMessage();
userRef.child(chat.getSender()).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapsh) {
if (snapsh.exists()){
final String userImage = snapsh.child("imageURL").getValue().toString();
switch (holder.getItemViewType()){
case MSG_TYPE_RIGHT:
RightMessageG rightM = (RightMessageG) holder;
rightM.show_messageR.setText(mess);
rightM.show_messageR.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, mess, Toast.LENGTH_SHORT).show();
}
});
rightM.text_seen.setVisibility(View.INVISIBLE);
break;
case MSG_TYPE_LEFT:
LeftMessageG leftM = (LeftMessageG) holder;
leftM.show_messageL.setText(mess);
Glide.with(mContext).load(userImage).into(leftM.photoGMember);
break;
case MSG_TYPE_IMAGE_RIGHT:
RightImageG rightI = (RightImageG) holder;
rightI.text_seen.setVisibility(View.INVISIBLE);
rightI.show_imageR.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, FullImage.class);
intent.putExtra("image", mess);
mContext.startActivity(intent);
}
});
Glide.with(rightI.show_imageR).load(mess).into(rightI.show_imageR);
break;
case MSG_TYPE_IMAGE_LEFT:
LeftImageG leftI = (LeftImageG) holder;
Glide.with(mContext).load(userImage).into(leftI.photoGMember);
Glide.with(mContext).load(mess).into(leftI.show_imageL);
break;
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}catch (Exception e){
}
}
@Override
public int getItemCount() {
return mChat.size();
}
@Override
public int getItemViewType(int position) {
fuser = FirebaseAuth.getInstance().getCurrentUser();
if (mChat.get(position).getSender().equals(fuser.getUid())){
if (mChat.get(position).getType().equals("text")){
return MSG_TYPE_RIGHT;
}else {
return MSG_TYPE_IMAGE_RIGHT;
}
}
else {
if (mChat.get(position).getType().equals("text")){
return MSG_TYPE_LEFT;
}else {
return MSG_TYPE_IMAGE_LEFT;
}
}
}
public class LeftMessageG extends RecyclerView.ViewHolder{
public TextView show_messageL;
public CircleImageView text_seen,photoGMember;
public LeftMessageG(@NonNull View itemView) {
super(itemView);
show_messageL = itemView.findViewById(R.id.show_messageLL);
text_seen = itemView.findViewById(R.id.text_seen);
photoGMember = itemView.findViewById(R.id.photoGMember);
}
}
public class RightMessageG extends RecyclerView.ViewHolder{
public TextView show_messageR;
public CircleImageView text_seen;
public RightMessageG(@NonNull View itemView) {
super(itemView);
show_messageR = itemView.findViewById(R.id.show_messageR);
text_seen = itemView.findViewById(R.id.text_seen);
}
}
public class RightImageG extends RecyclerView.ViewHolder{
public ImageView show_imageR;
public CircleImageView text_seen;
public RightImageG(@NonNull View itemView) {
super(itemView);
show_imageR = itemView.findViewById(R.id.show_imageR);
text_seen = itemView.findViewById(R.id.text_seen);
}
}
public class LeftImageG extends RecyclerView.ViewHolder{
public ImageView show_imageL;
public CircleImageView text_seen, photoGMember;
public LeftImageG(@NonNull View itemView) {
super(itemView);
show_imageL = itemView.findViewById(R.id.show_imageL);
text_seen = itemView.findViewById(R.id.text_seen);
photoGMember = itemView.findViewById(R.id.photoGMember);
}
}
}