MessageAdapter类
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageViewHolder>{
private List<Messages> mMessagesList;
private FirebaseAuth mAuth;
public MessageAdapter (List<Messages>mMessagesList)
{
this.mMessagesList = mMessagesList;
}
@Override
public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View V = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_chat_custom,parent,false);
mAuth = FirebaseAuth.getInstance();
return new MessageViewHolder(V);
}
public class MessageViewHolder extends RecyclerView.ViewHolder{
public TextView messageText;
public MessageViewHolder(View view)
{
super(view);
messageText = (TextView)view.findViewById(R.id.message_text_layout);
}
}
@SuppressLint("ResourceType")
@Override
public void onBindViewHolder(MessageViewHolder holder, int position) {
String current_user_id = mAuth.getInstance().getCurrentUser().getUid();
Messages messages = mMessagesList.get(position);
String from_user = messages.getFrom();
// if (from_user!=null && from_user.equals(current_user_id)){
// holder.messageText.setBackgroundResource(R.drawable.text_background1);
// holder.messageText.setTextColor(Color.red(R.color.red));
// }else {
// holder.messageText.setBackgroundResource(R.drawable.text_background2);
// holder.messageText.setTextColor(Color.red(R.color.black));
// }
holder.messageText.setText(messages.getMessage());
}
@Override
public int getItemCount() {
return mMessagesList.size();
}
}
Chat.java
private RecyclerView messageList;
private List<Messages> messagesList = new ArrayList<>();
private LinearLayoutManager linearLayoutManager;
private MessageAdapter mAdapter;
String MessageSenderId;
String MessageRecieverId;
final DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
MessageSenderId = mAuth.getCurrentUser().getUid();
MessageRecieverId = getIntent().getStringExtra("Recievers_Id");
messageList = (RecyclerView) findViewById(R.id.message_list);
linearLayoutManager = new LinearLayoutManager(this);
mAdapter = new MessageAdapter(messagesList);
// messageList.setHasFixedSize(true);
messageList.setLayoutManager(linearLayoutManager);
messageList.setAdapter(mAdapter);
private void fetchMessages() {
rootRef.child("Messages").child(MessageSenderId).child(MessageRecieverId).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Messages message = dataSnapshot.getValue(Messages.class);
messagesList.add(message);
mAdapter.notifyDataSetChanged();
Log.d("Tag", dataSnapshot.child("Message").getValue().toString());
}
@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) {
}
});
}
XML布局
ActivityChat
<?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="@color/white"
android:id="@+id/name"
android:layout_width="275dp"
android:layout_height="match_parent" />
</android.support.v7.widget.Toolbar>
<android.support.v7.widget.RecyclerView
android:id="@+id/message_list"
android:layout_above="@+id/send_message"
android:layout_below="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
<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:layout_marginLeft="5dp"
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" />
</RelativeLayout>
自定义布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/message_single_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp">
<TextView
android:id="@+id/message_text_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="@drawable/messages_text_background"
android:padding="10dp"
android:text="Textview"
android:textSize="20dp" />
</RelativeLayout>
在使用list {之前代替recyclerview
之前,它就像预期的那样工作得很好......然后由于显而易见的原因,我切换到了回收视图,我遇到了这个问题
屏幕截图= https://ibb.co/gcve67
在logcat中我尝试使用Log.d获取数据并且它工作了...所以我知道数据被提取但是却无法在recyclerview
中显示... Recyclerview规则有自从我上次使用以来我也不知道我的使用方式是否正确也有点变化...如果这是正确的我坚信布局有问题但是无法弄清楚...请帮忙......坚持了几个星期