Home RecyclerView only displaying the top Data
Reply: 1

RecyclerView only displaying the top Data

Courtney Miller
1#
Courtney Miller Published in 2018-01-12 22:05:27Z

Chat class

private void fetchMessages() {
    rootRef.child("Messages").child(MessageSenderId).child(MessageRecieverId)
            .addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            Messages messages = dataSnapshot.getValue(Messages.class);
            messagesList.add(messages);
            messageAdapter.notifyDataSetChanged();
        }

        @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(DatabaseError databaseError) {

        }
    });
}

Adapter

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageViewHolder>{
private List<Messages> userMessageList;

public MessageAdapter (List<Messages>userMessageList)
{
    this.userMessageList = userMessageList;
}

public class MessageViewHolder extends RecyclerView.ViewHolder{
    public TextView messageText;
    public MessageViewHolder(View view)
    {
        super(view);
        messageText = (TextView)view.findViewById(R.id.message_text);
    }
}





@Override
public  int getItemCount()
{
    return userMessageList.size();
}
@Override
public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
    View V = LayoutInflater.from(parent.getContext()).inflate(R.layout.messages_layout_of_users,parent,false);
    return new MessageViewHolder(V);
}

@Override
public void onBindViewHolder(MessageViewHolder holder, int position) {
        Messages messages = userMessageList.get(position);
        holder.messageText.setText(messages.getMessage());
    }
}

Messages.class

public class Messages {
private String message;
private String type;
private boolean seen;
private long time;

public Messages(){};

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public boolean isSeen() {
    return seen;
}

public void setSeen(boolean seen) {
    this.seen = seen;
}

public long getTime() {
    return time;
}

public void setTime(long time) {
    this.time = time;
}
}

XML Item Layout

  <?xml version="1.0" encoding="utf-8"?>
  <android.support.constraint.ConstraintLayout
  xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent"
  android:layout_height="match_parent">

<TextView
    android:textSize="15dp"
    android:id="@+id/message_text"
    android:background="@drawable/messages_text_background"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>

Activity Chat layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
app:titleTextColor="#ffffff"
>
<TextView
    android:textColor="#ffffff"
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>

    <Button

    android:id="@+id/add_image"
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true" />


<EditText
    android:id="@+id/input_message"
    android:layout_width="240dp"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_toStartOf="@+id/send_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:id="@+id/send_message"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar"
android:layout_above="@+id/input_message"
android:orientation="vertical">

<android.support.v7.widget.RecyclerView
    android:id="@+id/messageList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>

</LinearLayout>
</RelativeLayout>

Fetching of the message is fine. But it wont fetch all the messages. Only the first message is displayed. why is this happening?

Shrey Gupta
2#
Shrey Gupta Reply to 2018-01-12 22:17:56Z

Have You Tried Scrolling The Page . Since your RecyclerView ListItem has layout_height and layout_width as matchparent

Every item could be taking Full Space On the Window

Try Scrolling The Window

Or set layout_height as wrap content For constraint Layout

Or Try Increasing layout_height Or recycler view To match_parent

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.305254 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO