我是Android的新手,我想在我的应用中创建一个聊天,就像基于firebase的全球聊天室一样。消息通过RecyclerView显示,问题是当它加载了人物的图像时,它们的名字会以某种方式混淆。当我回到之前的活动并再次打开聊天活动时,一切都会变好。但是当添加新消息时,行再次洗牌。我确信应该很容易修复,但我不能......请帮忙。谢谢!
这是聊天活动oncreate:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_chat);
main_recycler_view = (RecyclerView) findViewById(R.id.messages_list);
main_recycler_view.setVisibility(View.GONE);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setVisibility(View.VISIBLE);
mAuth = FirebaseAuth.getInstance();
if (mAuth.getCurrentUser() != null) {
mUserRef = FirebaseDatabase.getInstance().getReference().child("Users").child(mAuth.getCurrentUser().getUid());
}
FirebaseUser currentUser = mAuth.getCurrentUser();
if (currentUser == null) {
sendToStart();
} else {
mUserRef.child("online").setValue("true");
String transp = "https://firebasestorage.googleapis.com/v0/b/chatqs-a4c10.appspot.com/o/profile_images%2Ftransp.png?alt=media&token=fc7b2315-f24b-453a-926e-364fff55e31b";
mUserRef.child("version").setValue(transp);
mToolbar = (Toolbar) findViewById(R.id.chattb);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(R.string.chat_title);
imageButton_send = (Button) findViewById(R.id.chat_send_btn);
editText_message = (EditText) findViewById(R.id.chat_message_view);
database = FirebaseDatabase.getInstance();
databaseRef = database.getReference();
mContext = (MainChatPage.this);
adapter = new MainAdapter(mContext, messageArrayList);
main_recycler_view.setAdapter(adapter);
main_recycler_view.setLayoutManager(new LinearLayoutManager(c));
databaseRef.child("the_messages").limitToLast(50).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
progressBar.setVisibility(View.GONE);
main_recycler_view.setVisibility(View.VISIBLE);
final Message new_message = dataSnapshot.getValue(Message.class);
final String userid = dataSnapshot.child("userID").getValue().toString();
mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
final String current_uid = mCurrentUser.getUid();
mDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(current_uid).child("blocked");
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.child(userid).exists()) {
messageArrayList.remove(new_message);
}
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
main_recycler_view.scrollToPosition(adapter.getItemCount() - 1);
messageArrayList.add(new_message);
adapter.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 MainAdapter(MainChatPage context, ArrayList<Message> data) {
this.data = data;
this.mContext = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.message_single_layout, null);
message_profile_layout = (CircleImageView) view.findViewById(R.id.message_profile_layout);
MyViewHolder viewHolder = new MyViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(final MyViewHolder myViewHolder, final int i) {
final Message message = data.get(i);
final String userid = message.getUserID();
mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
final String current_uid = mCurrentUser.getUid();
mDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(current_uid).child("blocked");
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(userid);
mUserDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String thumb = dataSnapshot.child("thumb_image").getValue().toString();
String nome = dataSnapshot.child("name").getValue().toString();
String dialog_name = nome;
String vers = dataSnapshot.child("version").getValue().toString();
//String proic = "https://firebasestorage.googleapis.com/v0/b/chatqs-a4c10.appspot.com/o/profile_images%2Ficonfinder-pro-icon-icon.png?alt=media&token=1ca05e32-f3e7-4f73-af54-2be17ad0fed6";
// Picasso.with(myViewHolder.mes_proicon.getContext()).load(proic)
// .placeholder(R.drawable.transp).into(myViewHolder.mes_proicon);
// myViewHolder.name_text_layout.setText(nome);
Picasso.with(myViewHolder.message_profile_layout.getContext()).load(thumb)
.placeholder(R.drawable.default_avatar).into(myViewHolder.message_profile_layout);
Picasso.with(myViewHolder.mes_proicon.getContext()).load(vers)
.placeholder(R.drawable.transp).into(myViewHolder.mes_proicon);
myViewHolder.name_text_layout.setText(nome);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
String formatted_date = SCUtils.formatted_date(message.getTimestamp());
myViewHolder.message_text_layout.setText(Html.fromHtml(message.getMessage()));
myViewHolder.time_text_layout.setText(Html.fromHtml(formatted_date));
myViewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!userid.equals(current_uid)) {
final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
LayoutInflater inflater = mContext.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.dialog, null);
TextView dialog_name = (TextView) dialogView.findViewById(R.id.dialog_name);
CircleImageView dialog_icon = (CircleImageView) dialogView.findViewById(R.id.dialog_icon);
builder.setView(dialogView);
dialog_name.setText(myViewHolder.name_text_layout.getText());
dialog_icon.setImageDrawable(myViewHolder.message_profile_layout.getDrawable());
builder.setPositiveButton(R.string.block, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
mDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(current_uid).child("blocked");
mDatabase.child(userid).push().setValue("");
}
})
.setNegativeButton(R.string.open_profile, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent chatIntent = new Intent(mContext, UserProfileActivity.class);
chatIntent.putExtra("user_id", userid);
mContext.startActivity(chatIntent);
mContext.finish();
}
});
builder.show();
} else {
Intent Intent = new Intent(mContext, ProfileFragment.class);
mContext.startActivity(Intent);
mContext.finish();
}
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public int getItemCount() {
return data.size();
} 公共类MyViewHolder扩展了RecyclerView.ViewHolder {
View mView;
protected TextView message_text_layout;
protected TextView name_text_layout;
protected TextView time_text_layout;
protected CircleImageView message_profile_layout;
protected CircleImageView mes_proicon;
public MyViewHolder(View itemView) {
super(itemView);
mView = itemView;
this.message_text_layout = (TextView) mView.findViewById(R.id.message_text_layout);
this.name_text_layout = (TextView) mView.findViewById(R.id.name_text_layout);
this.time_text_layout = (TextView) mView.findViewById(R.id.time_text_layout);
this.mes_proicon = (CircleImageView) mView.findViewById(R.id.mes_proicon);
this.message_profile_layout = (CircleImageView) mView.findViewById(R.id.message_profile_layout);
}
}
答案 0 :(得分:0)
只需删除.placeholder。我不知道为什么,但有时它会因为这种方法而发生