我使用firebase创建了一个简单的聊天应用程序,但是在为左右位置放置气泡元素时遇到了一些麻烦.左气泡给其他人,右气泡给我.
这是我的适配器:
public class MessageAdapterCustom extends RecyclerVIEw.Adapter<MessageAdapterCustom.MessageVIEwHolder> { private List<Messages> mMessageList; private Context context; private String mBubbleposition; public MessageAdapterCustom(Context context, List<Messages> mMessageList, String mBubbleposition) { this.context = context; this.mMessageList = mMessageList; this.mBubbleposition = mBubbleposition; } @OverrIDe public MessageVIEwHolder onCreateVIEwHolder(VIEwGroup parent, int vIEwType) { VIEw v = LayoutInflater.from( parent.getContext() ) .inflate( R.layout.message_single_layout_custom, parent, false ); return new MessageVIEwHolder( v ); } public class MessageVIEwHolder extends RecyclerVIEw.VIEwHolder { public TextVIEw messageText; public circleimageVIEw profileImage; public MessageVIEwHolder(VIEw vIEw) { super( vIEw ); if(mBubbleposition.equals( "kiri" )) { relativeLayout right = (relativeLayout) vIEw.findVIEwByID( R.ID.sendingMessageLayout ); right.setVisibility( VIEw.GONE ); messageText = (TextVIEw) vIEw.findVIEwByID( R.ID.message_text_layout_kiri ); profileImage = (circleimageVIEw) vIEw.findVIEwByID( R.ID.message_profile_layout_kiri ); } else { linearLayout left = (linearLayout) vIEw.findVIEwByID(R.ID.recIEvemessageLayout); left.setVisibility(VIEw.GONE); messageText = (TextVIEw) vIEw.findVIEwByID( R.ID.message_text_layout_kanan ); profileImage = (circleimageVIEw) vIEw.findVIEwByID( R.ID.message_profile_layout_kanan ); } } } @OverrIDe public voID onBindVIEwHolder(final MessageVIEwHolder holder, int position) { final Messages msg = mMessageList.get( position ); holder.messageText.setText( msg.getMessage() ); Picasso.with( context ) .load( msg.getProfile_pic() ) .networkPolicy( NetworkPolicy.OFFliNE ) .into( holder.profileImage ); /*Picasso.with( context ) .load( msg.getProfile_pic() ) .networkPolicy( NetworkPolicy.OFFliNE ) .placeholder( R.drawable.no_profile ) .into( holder.profileImage, new Callback() { @OverrIDe public voID onSuccess() { } @OverrIDe public voID one rror() { Picasso.with( context ) .load( msg.getProfile_pic() ) .networkPolicy( NetworkPolicy.OFFliNE ) .into( holder.profileImage ); } } );*/ } @OverrIDe public int getItemCount() { return mMessageList.size(); } }
我的ChatActivity:
public class ChatsActivity extends BaseActivity { private Toolbar mToolbar; private String mChatUser, mChatUserProfileImage; private DatabaseReference mRootRef, mMessageDatabase; private FirebaseAuth mAuth; private TextVIEw mTitleVIEw, mLastSeenVIEw; private circleimageVIEw mProfileImage, mChatProfilePic; private String online, image, mCurrentUserID; private Imagebutton mChatAddBtn, mChatSendBtn; private EditText mChatMessageText; private RecyclerVIEw mMessagesList; private final List<Messages> messagesList = new ArrayList<>(); private linearlayoutmanager mlinearLayout; private MessageAdapter mAdapter; private MessageAdapterCustom mAdapterCustom; String URL_PROfile; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState ); setContentVIEw( R.layout.activity_chats ); mRootRef = FirebaseDatabase.getInstance().getReference(); /*mRootRef.keepSynced( true );*/ mAuth = FirebaseAuth.getInstance(); mCurrentUserID = mAuth.getCurrentUser().getUID(); mChatUser = getIntent().getStringExtra( "user_ID" ); mChatUserProfileImage = getIntent().getStringExtra( "thumb_image" ); /*getwindow().setSoftinputMode( WindowManager.LayoutParams.soFT_input_STATE_VISIBLE );*/ mToolbar = (Toolbar) findVIEwByID( R.ID.toolbar ); setSupportActionbar( mToolbar ); mChatAddBtn = (Imagebutton) findVIEwByID( R.ID.chat_add_btn ); mChatSendBtn = (Imagebutton) findVIEwByID( R.ID.chat_send_btn ); mChatMessageText = (EditText) findVIEwByID( R.ID.chat_message ); mChatMessageText.requestFocus(); mMessagesList = (RecyclerVIEw) findVIEwByID( R.ID.chat_messages_List ); mlinearLayout = new linearlayoutmanager( this ); mMessagesList.setHasFixedSize( true ); mMessagesList.setLayoutManager( mlinearLayout ); Actionbar actionbar = getSupportActionbar(); actionbar.setdisplayHomeAsUpEnabled( true ); actionbar.setdisplayShowCustomEnabled( true ); final String username = getIntent().getStringExtra( "user_name" ); final String thumbProfilePic = getIntent().getStringExtra( "thumb_image" ); chatUserPic( mChatUser ); LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService( Context.LAYOUT_INFLATER_SERVICE ); VIEw action_bar_vIEw = layoutInflater.inflate( R.layout.chat_custom_bar, null ); actionbar.setCustomVIEw( action_bar_vIEw ); mTitleVIEw = (TextVIEw) findVIEwByID( R.ID.custom_bar_Title ); mLastSeenVIEw = (TextVIEw) findVIEwByID( R.ID.custom_bar_seen ); mProfileImage = (circleimageVIEw) findVIEwByID( R.ID.custom_bar_image ); mChatProfilePic = (circleimageVIEw) findVIEwByID( R.ID.message_profile_layout ); mTitleVIEw.setText( username ); mRootRef.child( "Users" ).child( mCurrentUserID ).child( "online" ).setValue( "Online" ); loadMessages(); mRootRef.child( "Users" ).child( mChatUser ).addValueEventListener( new ValueEventListener() { @OverrIDe public voID onDataChange(DataSnapshot dataSnapshot) { mTitleVIEw.setText( username ); online = dataSnapshot.child( "online" ).getValue().toString(); image = dataSnapshot.child( "image" ).getValue().toString(); if (online.equals( "Online" )) { mLastSeenVIEw.setText( online ); } else { GetTimeAgo getTimeAgo = new GetTimeAgo(); long lastTime = Long.parseLong( online ); String lastSeenTime = getTimeAgo.timeAgo( lastTime, getApplicationContext() ); mLastSeenVIEw.setText( lastSeenTime ); } } @OverrIDe public voID onCancelled(DatabaseError databaseError) { } } ); mChatMessageText.setonClickListener( new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { if(mAdapterCustom != null) { mMessagesList.scrollToposition( mAdapterCustom.getItemCount() - 1 ); } } } ); mChatMessageText.setonFocuschangelistener( new VIEw.OnFocuschangelistener() { @OverrIDe public voID onFocusChange(VIEw vIEw, boolean hasFocus) { if (hasFocus) { if(mAdapterCustom != null) { mMessagesList.scrollToposition( mAdapterCustom.getItemCount() - 1 ); } } else { if(mAdapterCustom != null) { mMessagesList.scrollToposition( mAdapterCustom.getItemCount() - 1 ); } } } } ); mRootRef.child( "Chat" ).child( mCurrentUserID ).addValueEventListener( new ValueEventListener() { @OverrIDe public voID onDataChange(DataSnapshot dataSnapshot) { if (!dataSnapshot.hasChild( mChatUser )) { Map chatAddMap = new HashMap(); chatAddMap.put( "seen", false ); chatAddMap.put( "timestamp", ServerValue.TIMESTAMP ); Map chatUserMap = new HashMap(); chatUserMap.put( "Chat" + "/" + mCurrentUserID + "/" + mChatUser, chatAddMap ); chatUserMap.put( "Chat" + "/" + mChatUser + "/" + mCurrentUserID, chatAddMap ); mRootRef.updateChildren( chatUserMap, new DatabaseReference.CompletionListener() { @OverrIDe public voID onComplete(DatabaseError databaseError, DatabaseReference databaseReference) { if (databaseError != null) { Log.e( "CHAT_LOG", databaseError.getMessage().toString() ); } else { mMessagesList.scrollToposition( mAdapterCustom.getItemCount() - 1 ); } } } ); } } @OverrIDe public voID onCancelled(DatabaseError databaseError) { } } ); mChatSendBtn.setonClickListener( new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { final String message = mChatMessageText.getText().toString(); if (TextUtils.isEmpty( message )) { one rror( "Message cannot be empty!" ); } else { DatabaseReference reference = FirebaseDatabase.getInstance().getReference(); query query = reference.child( "Users" ).child( mCurrentUserID ); query.addListenerForSingleValueEvent( new ValueEventListener() { @OverrIDe public voID onDataChange(DataSnapshot dataSnapshot) { if (dataSnapshot.exists()) { final String URL_PROfile = dataSnapshot.child( "thumb_image" ).getValue().toString(); sendMessage( message, URL_PROfile ); } } @OverrIDe public voID onCancelled(DatabaseError databaseError) { } } ); } } } ); } private voID chatUserPic(String ID) { DatabaseReference reference = FirebaseDatabase.getInstance().getReference(); query query = reference.child( "Users" ).child( ID ); query.addListenerForSingleValueEvent( new ValueEventListener() { @OverrIDe public voID onDataChange(DataSnapshot dataSnapshot) { if (dataSnapshot.exists()) { final String URL_PROfile = dataSnapshot.child( "thumb_image" ).getValue().toString(); Picasso.with( getApplication() ) .load( URL_PROfile ) .networkPolicy( NetworkPolicy.OFFliNE ) .placeholder( R.drawable.no_profile ) .into( mProfileImage, new Callback() { @OverrIDe public voID onSuccess() { } @OverrIDe public voID one rror() { Picasso.with( getApplication() ) .load( URL_PROfile ) .placeholder( R.drawable.no_profile ) .into( mProfileImage ); } } ); } else { String URL_PROfile = "no_profile"; Log.i( "PAul", URL_PROfile ); } } @OverrIDe public voID onCancelled(DatabaseError databaseError) { } } ); } private voID loadMessages() { mRootRef.child( "Messages" ).child( mCurrentUserID ).child( mChatUser ).addChildEventListener( new ChildEventListener() { @OverrIDe public voID onChildAdded(DataSnapshot dataSnapshot, String s) { Messages message = dataSnapshot.getValue( Messages.class ); String currentUserID = message.current_user_ID.toString(); /*mAdapter = new MessageAdapter( getApplicationContext(), messagesList );*/ if (currentUserID.equals( mCurrentUserID )) { mAdapterCustom = new MessageAdapterCustom( getApplicationContext(), messagesList, "kanan" ); mMessagesList.setAdapter( mAdapterCustom ); messagesList.add( message ); mAdapterCustom.notifyDataSetChanged(); Log.i( "CHAT_ACTIVITY", "RIGHT" ); } else { mAdapterCustom = new MessageAdapterCustom( getApplicationContext(), messagesList, "kiri" ); mMessagesList.setAdapter( mAdapterCustom ); messagesList.add( message ); mAdapterCustom.notifyDataSetChanged(); Log.i( "CHAT_ACTIVITY", "left" ); } /*mMessagesList.scrollToposition( mAdapterCustom.getItemCount() - 1 );*/ } @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) { } } ); } private voID sendMessage(String message, String profile_pic) { String current_user_ref = "Messages" + "/" + mCurrentUserID + "/" + mChatUser; String chat_user_ref = "Messages" + "/" + mChatUser + "/" + mCurrentUserID; DatabaseReference user_message_push = mRootRef.child( "Messages" ) .child( mCurrentUserID ).child( mChatUser ).push(); String push_ID = user_message_push.getKey(); Map messageMap = new HashMap(); messageMap.put( "message", message ); messageMap.put( "seen", false ); messageMap.put( "type", "text" ); messageMap.put( "time", ServerValue.TIMESTAMP ); messageMap.put( "profile_pic", profile_pic ); messageMap.put( "current_user_ID", mCurrentUserID ); Map messageUserMap = new HashMap(); messageUserMap.put( current_user_ref + "/" + push_ID, messageMap ); messageUserMap.put( chat_user_ref + "/" + push_ID, messageMap ); mRootRef.updateChildren( messageUserMap, new DatabaseReference.CompletionListener() { @OverrIDe public voID onComplete(DatabaseError databaseError, DatabaseReference databaseReference) { if (databaseError != null) { mChatMessageText.setText( "" ); Log.e( "SEND_MESSAGE_CHAT", databaseError.getMessage().toString() ); } else { mChatMessageText.setText( "" ); mMessagesList.scrollToposition( mAdapterCustom.getItemCount() - 1 ); } } } ); } @OverrIDe protected voID onStart() { super.onStart(); mRootRef.child( "Users" ).child( mCurrentUserID ).child( "online" ).setValue( "Online" ); mMessagesList.getLayoutManager().scrollToposition( mlinearLayout.findLastVisibleItemposition() + 1000 ); }}
您会看到以下行:
if (currentUserID.equals( mCurrentUserID )) { mAdapterCustom = new MessageAdapterCustom( getApplicationContext(), messagesList, "kanan" ); mMessagesList.setAdapter( mAdapterCustom ); messagesList.add( message ); mAdapterCustom.notifyDataSetChanged(); Log.i( "CHAT_ACTIVITY", "RIGHT" );} else { mAdapterCustom = new MessageAdapterCustom( getApplicationContext(), messagesList, "kiri" ); mMessagesList.setAdapter( mAdapterCustom ); messagesList.add( message ); mAdapterCustom.notifyDataSetChanged(); Log.i( "CHAT_ACTIVITY", "left" );}
当我尝试使用两个设备时,首先,如果我本人尝试发送消息,则Log显示RIGHT,这表示气球在右侧的位置.
但是,当我使用其他设备(另一个人)答复时,适配器会将两个气泡向左移动.似乎是因为它遵循最后一个适配器命令.
我的日志
如何在每次收到消息时将适配器保持在适当的位置,而不是如图所示更改视图元素的所有结构.
-更新-
我的版面:
<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:ID="@+ID/stackoverflow" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:padding="5dp"> <linearLayout androID:ID="@+ID/recIEvemessageLayout" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content"> <de.hdodenhof.circleimagevIEw.circleimageVIEw androID:ID="@+ID/message_profile_layout_kiri" androID:layout_wIDth="55dp" androID:layout_height="55dp" androID:src="@drawable/no_profile" /> <linearLayout androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:orIEntation="vertical"> <TextVIEw androID:ID="@+ID/message_text_layout_kiri" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_marginStart="10dp" androID:layout_margintop="5dp" androID:background="@drawable/message_text_background" androID:paddingBottom="5dp" androID:paddingleft="10dp" androID:paddingRight="10dp" androID:paddingtop="5dp" androID:text="@string/message_here" androID:textcolor="@androID:color/white" /> <ImageVIEw androID:ID="@+ID/message_buble_kiri" androID:layout_wIDth="20dp" androID:layout_height="20dp" androID:layout_marginStart="10dp" androID:layout_margintop="-10dp" androID:src="@drawable/buble_left" /> </linearLayout> </linearLayout> <relativeLayout androID:ID="@+ID/sendingMessageLayout" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_below="@+ID/recIEvemessageLayout" androID:layout_margintop="5dp" androID:orIEntation="horizontal"> <de.hdodenhof.circleimagevIEw.circleimageVIEw androID:ID="@+ID/message_profile_layout_kanan" androID:layout_wIDth="55dp" androID:layout_height="55dp" androID:layout_alignParentEnd="true" androID:src="@drawable/no_profile" /> <TextVIEw androID:ID="@+ID/message_text_layout_kanan" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParenttop="true" androID:layout_marginEnd="10dp" androID:layout_margintop="5dp" androID:layout_toStartOf="@+ID/message_profile_layout_kanan" androID:background="@drawable/message_text_background" androID:paddingBottom="5dp" androID:paddingleft="10dp" androID:paddingRight="10dp" androID:paddingtop="5dp" androID:text="@string/message_here" androID:textcolor="@androID:color/white" /> <ImageVIEw androID:ID="@+ID/message_buble_kanan" androID:layout_wIDth="20dp" androID:layout_height="20dp" androID:layout_alignEnd="@+ID/message_text_layout_kanan" androID:layout_below="@+ID/message_text_layout_kanan" androID:layout_margintop="-10dp" androID:src="@drawable/buble_right" /> </relativeLayout></relativeLayout>
解决方法:
当前,您正在创建两个不同的适配器,所有消息都以相同的方向对齐.相反,您应该创建一个适配器来决定如何一次对齐每个消息.
总结以上是内存溢出为你收集整理的Java-Android-如何使适配器布局保持在原位全部内容,希望文章能够帮你解决Java-Android-如何使适配器布局保持在原位所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)