RecyclerView仅显示顶部数据

时间:2018-01-12 22:05:28

标签: android firebase firebase-realtime-database android-recyclerview

聊天课

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) {

        }
    });
}

适配器

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 项目布局

  <?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>

活动聊天布局

<?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>

获取消息很好。但它不会获取所有消息。仅显示第一条消息。为什么会这样?

1 个答案:

答案 0 :(得分:0)

你有没有试过滚动页面。 由于RecyclerView ListItem的layout_height和layout_width为matchparent

  

每件物品都可以在窗户上占据整个空间

     

尝试滚动窗口

     

或者将layout_height设置为包含内容用于约束布局

     

或尝试增加layout_height或者回收者视图到match_parent