Improved views layout

This commit is contained in:
Pierre 2017-12-19 19:18:33 +01:00
parent 0f2f28ec36
commit e04ec17272
3 changed files with 83 additions and 23 deletions

View File

@ -4,6 +4,7 @@ import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.ArrayMap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -75,6 +76,12 @@ public class ConversationMessageAdapter extends ArrayAdapter<ConversationMessage
ConversationMessage message = getItem(position);
assert message != null;
//Get the previous message
ConversationMessage previousMessage = null;
if(position > 0){
previousMessage = getItem(position-1);
}
/*
Get the view of the messages
@ -82,29 +89,38 @@ public class ConversationMessageAdapter extends ArrayAdapter<ConversationMessage
*/
TextView contentView;
ImageView accountImage;
TextView userName;
if(message.getUser_id() == userID){
//Message appears on the right
convertView.findViewById(R.id.fragment_conversation_message_left).
setVisibility(View.GONE);
convertView.findViewById(R.id.fragment_conversation_message_right).
setVisibility(View.VISIBLE);
contentView = convertView.
findViewById(R.id.fragment_conversation_message_item_content_right);
accountImage = convertView.
findViewById(R.id.fragment_conversation_message_item_accountimage_right);
userName = null;
}
else {
//Message appears on the left
convertView.findViewById(R.id.fragment_conversation_message_right).
setVisibility(View.GONE);
convertView.findViewById(R.id.fragment_conversation_message_left).
setVisibility(View.VISIBLE);
contentView = convertView.
findViewById(R.id.fragment_conversation_message_item_content);
accountImage = convertView.
findViewById(R.id.fragment_conversation_message_item_accountimage);
userName = convertView.findViewById(R.id.fragment_conversation_message_item_username);
}
/*
@ -123,6 +139,27 @@ public class ConversationMessageAdapter extends ArrayAdapter<ConversationMessage
contentView.setText(message.getContent());
/*
Update user name
*/
if(userName != null){
if(user != null){
//Set the name of the user
userName.setText(user.getFullName());
userName.setVisibility(View.VISIBLE);
}
else
userName.setVisibility(View.GONE);
if(previousMessage != null){
if (message.getUser_id() == previousMessage.getUser_id()){
userName.setVisibility(View.GONE);
}
}
}
/*
Update account image
*/
@ -131,6 +168,7 @@ public class ConversationMessageAdapter extends ArrayAdapter<ConversationMessage
//Set the default image
accountImage.setImageResource(R.drawable.default_account_image);
accountImage.setVisibility(View.VISIBLE);
//Check if we can load a specific image
if(user != null) {
@ -138,6 +176,13 @@ public class ConversationMessageAdapter extends ArrayAdapter<ConversationMessage
ImageLoadManager.load(getContext(), imageURL, accountImage);
}
//Hide user image if not required
if(previousMessage != null){
if (message.getUser_id() == previousMessage.getUser_id()){
accountImage.setVisibility(View.INVISIBLE);
}
}
return convertView;
}

View File

@ -7,6 +7,8 @@
android:id="@+id/fragment_conversation_messageslist"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
android:layout_weight="1"
android:divider="@null"
/>
</LinearLayout>

View File

@ -10,8 +10,20 @@
android:paddingTop="5dp">
<!-- Messages on the left -->
<RelativeLayout
<LinearLayout
android:id="@+id/fragment_conversation_message_left"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/fragment_conversation_message_item_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="User name" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="1dp">
@ -35,10 +47,12 @@
android:paddingEnd="15dp"
android:paddingStart="15dp"
android:textColor="@color/conversation_otheruser_messages_textColor"
tools:text="A message"/>
tools:text="A message" />
</RelativeLayout>
</LinearLayout>
<!-- Messages on the right -->
<RelativeLayout
android:id="@+id/fragment_conversation_message_right"
@ -66,8 +80,7 @@
android:paddingEnd="15dp"
android:paddingStart="15dp"
android:textColor="@color/conversation_user_messages_textColor"
tools:text="A message"/>
tools:text="A message" />
</RelativeLayout>