Based on my previous question , I have a problem with onClick on CardView in my RecyclerView. I need to change the card layout when the CardView is clicked with another card layout xml. I follow this and this this but its not work. I dont too understand how to declare one CardView layout again in my code. Here my Code :
Adapter.Java
public class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public static final int view1 = 0;
public static final int view2 = 1;
private static OnRecyclerViewItemClickedListener recyclerViewItemClickedListener;
public void setOnRecyclerViewClickedListener (OnRecyclerViewItemClickedListener l) {
recyclerViewItemClickedListener = l;
}
public interface OnRecyclerViewItemClickedListener {
void OnRecyclerViewItemClicked(int position);
void OnRecyclerViewItemBind(ViewHolder holder, int position);
int OnRecyclerViewItemCount();
}
public Adapter() {
super();
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView;
if (viewType == view1) {
itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.table_item_empty, parent, false);
return new ViewHolder(itemView);
}
else if (viewType == view2) {
itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.table_item_occupied, parent, false);
return new ViewHolder2(itemView);
}
return null;
}
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
recyclerViewItemClickedListener.OnRecyclerViewItemBind((ViewHolder) holder, position);
}
#Override
public int getItemCount() {
return recyclerViewItemClickedListener.OnRecyclerViewItemCount();
}
public int getItemViewType() { return recyclerViewItemClickedListener.getItemViewType(); }
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView txt_no_table;
public TextView txt_no_table_occ;
public TextView txt_pax_occ;
public TextView txt_guest_name_occ;
public TextView txt_bill_occ;
public Chronometer txt_time_occ;
public CardView cardview_table;
public LinearLayout title_layout;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
txt_no_table = (TextView) itemView.findViewById(R.id.txt_no_table_empty);
txt_no_table_occ = (TextView) itemView.findViewById(R.id.txt_no_table);
txt_pax_occ = (TextView) itemView.findViewById(R.id.txt_pax);
txt_guest_name_occ = (TextView)itemView.findViewById(R.id.txt_guestname);
txt_bill_occ = (TextView) itemView.findViewById(R.id.txt_bill);
txt_time_occ = (Chronometer) itemView.findViewById(R.id.txt_time);
cardview_table = (CardView) itemView.findViewById(R.id.table_card_empty);
title_layout = (LinearLayout) itemView.findViewById(R.id.table_ll1);
}
#Override
public void onClick(View itemView) {
recyclerViewItemClickedListener.OnRecyclerViewItemClicked(getAdapterPosition());
}
}
public class ViewHolder2 extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView txt_no_table2;
public TextView txt_pax2;
public TextView txt_guest_name2;
public TextView txt_bill2;
public TextView txt_time2;
public ViewHolder2 (View itemView) {
super(itemView);
itemView.setOnClickListener(this);
txt_no_table2 = (TextView) itemView.findViewById(R.id.txt_no_table_);
txt_pax2 = (TextView) itemView.findViewById(R.id.txt_pax_);
txt_guest_name2 = (TextView)itemView.findViewById(R.id.txt_guestname_);
txt_bill2 = (TextView) itemView.findViewById(R.id.txt_bill_);
txt_time2 = (TextView) itemView.findViewById(R.id.txt_time_);
}
#Override
public void onClick(View itemView) {
recyclerViewItemClickedListener.OnRecyclerViewItemClicked(getAdapterPosition());
}
}
}
Here My Activity :
adapter.setOnRecyclerViewClickedListener(new Adapter.OnRecyclerViewItemClickedListener() {
#Override
public void OnRecyclerViewItemClicked(int position) {
try {
JSONObject currTable = filteredTableList.getJSONObject(position);
if (currTable.has("selected")) {
currTable.put("selected", !currTable.getBoolean("selected"));
} else {
currTable.put("selected",true);
}
adapter.notifyItemChanged(position);
} catch (JSONException e) {
e.printStackTrace();
}
try
{
Toast.makeText(TableActivity.this, filteredTableList.getJSONObject(position).getString("tischnr"), Toast.LENGTH_SHORT).show();
}
catch (JSONException e) {
e.printStackTrace();
}
}
#Override
public void OnRecyclerViewItemBind(Adapter.ViewHolder holder, int position) {
try {
JSONObject currTable = filteredTableList.getJSONObject(position);
holder.txt_no_table.setText(currTable.getString("tischnr"));
holder.txt_guest_name_occ.setText("");
holder.txt_pax_occ.setText("");
holder.txt_bill_occ.setText("");
int queasy33Index = ProgramMethod.getJSONArrayIndex(queasy33List,"number2", currTable.getInt("tischnr"));
if (queasy33Index >= 0) {
holder.txt_guest_name_occ.setText(queasy33List.getJSONObject(queasy33Index).getString("char2"));
holder.txt_pax_occ.setText(queasy33List.getJSONObject(queasy33Index).getString("number3"));
}
if (currTable.has("selected") && currTable.getBoolean("selected")) {
holder.itemView.setBackgroundResource(R.color.colorRedTableOcc);
holder.txt_no_table.setVisibility(View.INVISIBLE);
holder.txt_no_table_occ.setVisibility(View.VISIBLE);
holder.txt_bill_occ.setVisibility(View.VISIBLE);
holder.txt_pax_occ.setVisibility(View.VISIBLE);
holder.txt_time_occ.setVisibility(View.VISIBLE);
holder.txt_guest_name_occ.setVisibility(View.VISIBLE);
} else {
holder.itemView.setBackgroundResource(R.color.colorTableGreen);
holder.txt_no_table.setVisibility(View.VISIBLE);
holder.txt_no_table_occ.setVisibility(View.INVISIBLE);
holder.txt_bill_occ.setVisibility(View.INVISIBLE);
holder.txt_pax_occ.setVisibility(View.INVISIBLE);
holder.txt_time_occ.setVisibility(View.INVISIBLE);
holder.txt_guest_name_occ.setVisibility(View.INVISIBLE);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
#Override
public int OnRecyclerViewItemCount() {
return filteredTableList.length();
}
});
My table_item_empty.xml (first layout) :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/table_card_empty"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="#color/colorTableGreen"
app:cardCornerRadius="15dp"
app:cardElevation="5dp"
android:layout_margin="3dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="2dp"
android:gravity="center">
<RelativeLayout
android:id="#+id/table_rl1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/table_ll1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<TextView
android:id="#+id/txt_no_table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="15dp"
android:gravity="center"
android:textColor="#color/colorWhite"
android:visibility="invisible"/>
</LinearLayout>
<TextView
android:id="#+id/txt_pax"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="1dp"
android:text="5"
android:textSize="10dp"
android:textColor="#color/colorWhite"
android:drawableLeft="#drawable/ic_airline_seat_recline_normal_white_18dp"
android:visibility="invisible"/>
<TextView
android:id="#+id/txt_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10:00"
android:textSize="10dp"
android:textColor="#color/colorWhite"
android:drawableLeft="#drawable/ic_access_time_white_18dp"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/txt_pax"
android:layout_marginStart="2dp"
android:visibility="invisible"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/table_rl2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/table_rl1">
<TextView
android:id="#+id/txt_guestname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="1dp"
android:text="Budi"
android:textSize="10dp"
android:textColor="#color/colorWhite"
android:drawableLeft="#drawable/ic_supervisor_account_white_18dp"
android:visibility="invisible"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/table_rl2">
<TextView
android:id="#+id/txt_bill"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10000"
android:drawableLeft="#drawable/ic_attach_money_white_18dp"
android:textSize="10dp"
android:textColor="#color/colorWhite"
android:layout_marginStart="1dp"
android:visibility="invisible"/>
</RelativeLayout>
<TextView
android:id="#+id/txt_no_table_empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="12"
android:textSize="25dp"
android:gravity="center_vertical|center_horizontal"
android:textColor="#color/colorWhite"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</android.support.v7.widget.CardView>
App looks like :
My code make a first layout gone when i clicked but i want to show/change another card layout.
This is CardView1 Looks like :
Another CardView xml (CardView2) :
EDITED :
I trick it. Now when one or more item clicked, it will change the view into like this :
But i still want to try with two different layout.xml view when item clicked. Its work because i only 'play' with 'INVISIBLE' and 'VISIBLE' in element on one layout, not the xml.
[EDITED] :
I'm still try to make my RecyclerView with two Layout view. I improve my Adapter like above. I follow this but i get a litle problem. In my Acivity, i call my itemView from ViewHolder1 with holder (example: holder.txt_time....) like my code above. I want to know how i can call my second holder (ViewHolder2) in my Activity? I try to make hard code with add some listener but its not work for me :(
Maybe somebody can help and guide me to fix my code. Every answer will helpful for me. Thanks before.
Related
in a cardview i have an imageview, and two textview. I'm having problems trying to figurate out how to change the text of one of the textview when someone click on the cardview....is this even posible?
The card view are controlled by a recyclerview....in total I have to around 20 cardview.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="10dp"
app:cardCornerRadius="5dp"
android:foreground="?android:attr/selectableItemBackground">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<ImageView
android:id="#+id/image1"
android:layout_width="match_parent"
android:layout_height="130dp"
android:background="#color/transparent"
android:padding="0dp"
android:scaleType="fitCenter"
android:src="#drawable/card1" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginTop="5dp"
android:text="Text Here"
android:textColor="#color/red" />
<TextView
android:id="#+id/product"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_gravity="center"
android:gravity="center"
android:text="SUV"
android:textColor="#color/grey_font"
android:textSize="11dp" />
Here is the click of the card view, I get the posistion of the cardview, but don't know how to get the textview inside the cardview.
public void onItemClick(int position) {
Log.e(TAG, "Bien: " + position);
}
public void onBindViewHolder(#NonNull RecyclerViewHolder holder, int position) {
Items currentItem = vReyclerViewList.get(position);
String heading = currentItem.getHeading();
String title = currentItem.getTitlee();
holder.vHeading.setText(heading);
holder.vTitle.setText(title);
}
public class RecyclerViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public TextView product;
public RecyclerViewHolder(View itemView) {
super (itemView);
title = itemView.findViewById(R.id.title);
product = itemView.findViewById(R.id.product);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(vListener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
vListener.onItemClick(position);
}
}
}
});
}
}
You have to override the onBindViewHolder() method then implement as below.
Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
Model model = getItem(position);
holder.bind(model);
}
public class RecyclerViewHolder extends RecyclerView.ViewHolder {
public final TextView title;
public final TextView product;
public RecyclerViewHolder(View itemView) {
super (itemView);
title = itemView.findViewById(R.id.title);
product = itemView.findViewById(R.id.product);
}
public void bind(final Model model){
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
title.setText(model.getText());
}
});
}
}
I have a recycler inside fragment in view pager.
The problem that it put likes on user accounts in UI randomly but in DB everything is fine.
In logs, I see that random is not influence on BD. So the bug is only in UI part. After the refresh of the list, it can appear again and after that disappear.
I would like to share code but I already don't have an I idea where the problem could be. It might be in adapter/refresh list listener / XML or any other places. Please let me know what part of the code you need and I will provide it. Maybe it is a bug of recycler as itself and I can't fix it.
Adapter class code:
public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.UserViewHolder>{
private List<FsUser> fsUserList = new ArrayList<>();
private OnItemClickListener.OnItemClickCallback onItemClickCallback;
private OnItemClickListener.OnItemClickCallback onChatClickCallback;
private OnItemClickListener.OnItemClickCallback onLikeClickCallback;
private Context context;
public SearchAdapter(OnItemClickListener.OnItemClickCallback onItemClickCallback,
OnItemClickListener.OnItemClickCallback onChatClickCallback,
OnItemClickListener.OnItemClickCallback onLikeClickCallback) {
this.onItemClickCallback = onItemClickCallback;
this.onChatClickCallback = onChatClickCallback;
this.onLikeClickCallback = onLikeClickCallback;
}
public void addUsers(List<FsUser> userList) {
fsUserList.addAll(userList);
notifyItemRangeInserted(fsUserList.size() - userList.size(), fsUserList.size());
}
public void clearData(){
fsUserList.clear();
notifyDataSetChanged();
}
#NonNull
#Override
public UserViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
context = parent.getContext();
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_user, parent, false);
return new UserViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull UserViewHolder holder, int position) {
FsUser fsUser = fsUserList.get(position);
holder.bind(fsUser, position);
}
#Override
public int getItemCount() {
return fsUserList.size();
}
public String getLastItemId(){
return fsUserList.get(fsUserList.size() - 1).getUid();
}
class UserViewHolder extends RecyclerView.ViewHolder {
RelativeLayout container;
ImageView imageView, like, chat;
TextView name, country;
private LottieAnimationView animationView;
UserViewHolder(View itemView) {
super(itemView);
context = itemView.getContext();
container = itemView.findViewById(R.id.item_user_container);
imageView = itemView.findViewById(R.id.user_img);
like = itemView.findViewById(R.id.search_btn_like);
chat = itemView.findViewById(R.id.search_btn_chat);
name = itemView.findViewById(R.id.user_name);
country = itemView.findViewById(R.id.user_country);
animationView = itemView.findViewById(R.id.lottieAnimationView);
}
void bind(FsUser fsUser, int position){
ViewCompat.setTransitionName(imageView, fsUser.getName());
if (FirebaseUtils.isUserExist() && fsUser.getUid() != null) {
new FriendRepository().isLiked(fsUser.getUid(), flag -> {
if (flag) {
like.setBackground(ContextCompat.getDrawable(context, R.drawable.ic_favorite));
animationView.setVisibility(View.VISIBLE);
}
});
}
if(fsUser.getUid() != null) {
chat.setOnClickListener(new OnItemClickListener(position, onChatClickCallback));
like.setOnClickListener(new OnItemClickListener(position, onLikeClickCallback));
}
imageView.setOnClickListener(new OnItemClickListener(position, onItemClickCallback));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
if(fsUser.getImage().equals("default")){
Glide.with(context).load(context.getResources().getDrawable(R.drawable.default_avatar)).into(imageView);
} else {
Glide.with(context).load(fsUser.getImage()).thumbnail(0.5f).into(imageView);
}
name.setText(fsUser.getName());
country.setText(fsUser.getCountry());
ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f).setDuration(500);
animator.addUpdateListener(valueAnimator ->
animationView.setProgress((Float) valueAnimator.getAnimatedValue()));
if (animationView.getProgress() == 0f) {
animator.start();
} else {
animationView.setProgress(0f);
}
}
}
}
And xml file of RecyclerView item:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/item_user_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="#dimen/user_cv_width"
android:layout_height="#dimen/user_cv_height"
android:layout_margin="#dimen/dp4"
android:elevation="#dimen/dp4">
<RelativeLayout
android:id="#+id/item_user_main_relative_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/item_user_top_relative_container"
android:layout_width="#dimen/user_rl_width"
android:layout_height="#dimen/user_rl_height">
<ImageView
android:id="#+id/user_img"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop"
android:src="#drawable/default_avatar" />
<RelativeLayout
android:id="#+id/item_user_top_relative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/user_item_bg"
android:orientation="vertical">
<TextView
android:id="#+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/dp4"
android:textColor="#android:color/white"
android:textSize="#dimen/medium_text_size" />
<TextView
android:id="#+id/user_country"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:layout_marginStart="#dimen/dp4"
android:textSize="#dimen/medium_text_size" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/item_user_bottom_relative_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/dp12">
<ImageView
android:id="#+id/search_btn_like"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/heart_outline"
android:contentDescription="#string/search_btn_like_desc"/>
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/lottieAnimationView"
android:visibility="gone"
android:layout_width="#dimen/lottie_animation_view_size"
android:layout_height="#dimen/lottie_animation_view_size"
app:lottie_loop="true"
app:lottie_autoPlay="true"
app:lottie_fileName="like.json"/>
</RelativeLayout>
<ImageView
android:id="#+id/search_btn_chat"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/dp12"
android:layout_weight="1"
android:src="#drawable/message_outline" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
Override this two methods inside your adapter
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return position;
}
I made the FriendlyChat using firebase
However all the texts are appearing in same line, both from sender and receiver!
I want to apply chat bubbles with left and right alignment but have no idea where to start
I also want to wrap the text view inside chat bubble images? but i have no idea how to do that?
any help!
public class MessageAdapter extends ArrayAdapter<FriendlyMessage> {
public MessageAdapter(Context context, int resource, List<FriendlyMessage> objects) {
super(context, resource, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.item_message, parent, false);
}
ImageView photoImageView = (ImageView) convertView.findViewById(R.id.photoImageView);
TextView messageTextView = (TextView) convertView.findViewById(R.id.messageTextView);
TextView authorTextView = (TextView) convertView.findViewById(R.id.nameTextView);
FriendlyMessage message = getItem(position);
boolean isPhoto = message.getPhotoUrl() != null;
if (isPhoto) {
messageTextView.setVisibility(View.GONE);
photoImageView.setVisibility(View.VISIBLE);
Glide.with(photoImageView.getContext())
.load(message.getPhotoUrl())
.into(photoImageView);
} else {
messageTextView.setVisibility(View.VISIBLE);
photoImageView.setVisibility(View.GONE);
messageTextView.setText(message.getText());
}
authorTextView.setText(message.getName());
return convertView;
}
}
item_message.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/lp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:orientation="vertical">
<ImageView
android:id="#+id/photoImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true" />
<TextView
android:id="#+id/messageTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0"
android:background="#drawable/messenger_bubble_large_white"
android:padding="2dp"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:text="Message" />
<TextView
android:id="#+id/nameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:textAppearance="?android:attr/textAppearanceSmall"
tools:text="Name" />
</LinearLayout>
Hi #Parjanya you have have to check type and based upon condition yo can hide one and show other
for me I used 2 different viewHolder and 2 different xml. My code is below;
MessageChatAdapter.java;
public class MessageChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<ChatMessage> mChatList;
public static final int SENDER = 0;
public static final int RECIPIENT = 1;
public MessageChatAdapter(List<ChatMessage> listOfFireChats) {
mChatList = listOfFireChats;
}
#Override
public int getItemViewType(int position) {
if (mChatList.get(position).getRecipientOrSenderStatus() == SENDER) {
return SENDER;
} else {
return RECIPIENT;
}
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
RecyclerView.ViewHolder viewHolder;
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
switch (viewType) {
case SENDER:
View viewSender = inflater.inflate(R.layout.layout_sender_message, viewGroup, false);
viewHolder = new ViewHolderSender(viewSender);
break;
case RECIPIENT:
View viewRecipient = inflater.inflate(R.layout.layout_recipient_message, viewGroup, false);
viewHolder = new ViewHolderRecipient(viewRecipient);
break;
default:
View viewSenderDefault = inflater.inflate(R.layout.layout_sender_message, viewGroup, false);
viewHolder = new ViewHolderSender(viewSenderDefault);
break;
}
return viewHolder;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
switch (viewHolder.getItemViewType()) {
case SENDER:
ViewHolderSender viewHolderSender = (ViewHolderSender) viewHolder;
configureSenderView(viewHolderSender, position);
break;
case RECIPIENT:
ViewHolderRecipient viewHolderRecipient = (ViewHolderRecipient) viewHolder;
configureRecipientView(viewHolderRecipient, position);
break;
}
}
private void configureSenderView(ViewHolderSender viewHolderSender, int position) {
ChatMessage senderFireMessage = mChatList.get(position);
viewHolderSender.getSenderMessageTextView().setText(senderFireMessage.getMessage());
viewHolderSender.getmTimeStamp().setText(converteTimestamp(senderFireMessage.getTimestap()));
}
private void configureRecipientView(ViewHolderRecipient viewHolderRecipient, int position) {
ChatMessage recipientFireMessage = mChatList.get(position);
viewHolderRecipient.getRecipientMessageTextView().setText(recipientFireMessage.getMessage());
viewHolderRecipient.getmTimeStamp().setText(converteTimestamp(recipientFireMessage.getTimestap()));
}
/*==============ViewHolder===========*/
/*ViewHolder for Sender*/
public class ViewHolderSender extends RecyclerView.ViewHolder {
private TextView mSenderMessageTextView;
private TextView mTimeStamp;
public ViewHolderSender(View itemView) {
super(itemView);
mSenderMessageTextView = (TextView) itemView.findViewById(R.id.text_view_sender_message);
mTimeStamp = (TextView) itemView.findViewById(R.id.textView2);
}
public TextView getSenderMessageTextView() {
return mSenderMessageTextView;
}
public TextView getmTimeStamp() {
return mTimeStamp;
}
}
/*ViewHolder for Recipient*/
public class ViewHolderRecipient extends RecyclerView.ViewHolder {
private TextView mRecipientMessageTextView;
private TextView mTimeStamp;
public ViewHolderRecipient(View itemView) {
super(itemView);
mRecipientMessageTextView = (TextView) itemView.findViewById(R.id.text_view_recipient_message);
mTimeStamp = (TextView) itemView.findViewById(R.id.textView2);
}
public TextView getRecipientMessageTextView() {
return mRecipientMessageTextView;
}
public TextView getmTimeStamp() {
return mTimeStamp;
}
}
layout_sender_message.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/text_view_sender_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="I am doing ok"
android:padding="8dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="2dp"
android:textStyle="bold"
android:textSize="14sp"
android:textColor="#FFFFFF"
android:background="#drawable/sender_rounded_corners"
android:layout_alignParentRight="true" />
<TextView
android:id="#+id/textView2"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/text_view_sender_message"
android:layout_gravity="bottom"
android:layout_toLeftOf="#id/text_view_sender_message"
android:text="TextView"
android:textSize="12sp"
android:textStyle="italic"/>
</RelativeLayout>
layout_recipient_message.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<TextView
android:id="#+id/text_view_recipient_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Hi. How are you?"
android:padding="8dp"
android:textStyle="bold"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="2dp"
android:textColor="#727272"
android:textSize="15sp"
android:background="#drawable/recipient_rounded_corners"/>
<TextView
android:id="#+id/textView2"
android:layout_width="69dp"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/text_view_recipient_message"
android:text="TextView"
android:textSize="12sp"
android:layout_gravity="bottom"
android:textStyle="italic"/>
</LinearLayout>
Pretty basic and needs to be fixed but this will give you a headstart.
I think you should use two different custom components for chat left and right bubbles.
Set as a background of TextView and make the boolean condition for show and hide two chat bubbles.
If you want more information and demo with use of firebase then please check below link:
https://www.androidtutorialpoint.com/firebase/real-time-android-chat-application-using-firebase-tutorial/
I hope it helps you.
Thank you:)
Just two words Answer...
Use Gravity
eg
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) _chat_view.getLayoutParams();
lp.gravity = Gravity.END | Gravity.RIGHT;
_chat_view.setLayoutParams(lp);
where _chat_view is your Message (LinearLayout) container.
trying to build a chat bot, but how to alternate the TextView in RecyclerView's adapter. See the screenshot below:
As you can see, all the responses are currently aligned to the start, I'd like to move the reply from one person to the other side from the adapter of the RecyclerView. Here is the code to the adapter:
CustomAdapter.class
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.FeedsViewHolder>
{
DataHolder d1 = new DataHolder();
public class FeedsViewHolder extends RecyclerView.ViewHolder
{
private TextView chat;
private Typeface face;
FeedsViewHolder(View itemView)
{
super(itemView);
chat = (TextView)itemView.findViewById(R.id.chatMessage);
face = Typeface.createFromAsset(itemView.getContext().getAssets(), "fonts/Roboto-Regular.ttf");
chat.setTypeface(face);
}
}
private static class DataHolder
{
List<Text> feeds;
}
CustomAdapter(List<Text> feeds)
{
this.d1.feeds = feeds;
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public FeedsViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.chat_message, viewGroup, false);
FeedsViewHolder pvh = new FeedsViewHolder(v);
return pvh;
}
#Override
public void onBindViewHolder(FeedsViewHolder feedViewHolder, int i)
{
if(d1.feeds.get(i).isMachineOrHuman())
{
feedViewHolder.chat.setBackgroundResource(R.drawable.user);
feedViewHolder.chat.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
}
else
feedViewHolder.chat.setBackgroundResource(R.drawable.ais);
feedViewHolder.chat.setText(d1.feeds.get(i).getMessage());
}
#Override
public int getItemCount()
{
if(d1.feeds!=null)
{
return d1.feeds.size();
}
else
{
return 0;
}
}
}
chat_message.xml
<TextView android:id="#+id/chatMessage"
android:layout_width="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android" />
Now, I made an alternative solution, that is by keeping two TextViews in chat_message.xml and changing the visibility settings of the unused TextView . But what I am looking for is whether is it possible to do it with a single TextView. Also, the background of the TextView is a 9-patch-image , which is assigned as a background resource from the adapter, so when I move the TextView it shouldn't also spoil the background. Any thoughts would be much appreciated. :)
#Override
public void onBindViewHolder(FeedsViewHolder feedViewHolder, int i)
{
if(d1.feeds.get(i).isMachineOrHuman())
{
feedViewHolder.chat.setBackgroundResource(R.drawable.user);
**feedViewHolder.chat.setGravity(Gravity.RIGHT);** // *Sent Message*
feedViewHolder.chat.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
}
else {
feedViewHolder.chat.setBackgroundResource(R.drawable.ais);
**feedViewHolder.chat.setGravity(Gravity.LEFT);** // *Receive Message*
feedViewHolder.chat.setText(d1.feeds.get(i).getMessage());
}
Create A child layout under the parent layout for The bubble(9-patch) and set the Gravity approrpriately as the TEXT(Message).
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/message_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#drawable/message_bubble"
android:longClickable="true"
android:minHeight="40dp">
<TextView
android:id="#+id/message_body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Put your message"
android:textColor="#color/black"
android:textColorLink="#color/black"
android:textSize="?attr/TextSizeBody"
android:visibility="visible" />
</LinearLayout>
How do I archive a layout for my RecyclerView that looks like this:
I have tried creating it but it did ever look like in the giudlines.
This is taken form the Material Design Guidlines, but I could not find any xml Layouts, except Sketch and or PSDs.
Are there any ressources directl in xml?
Edit 1: I only need the single list item XML layout
Edit 2: I know how to use & implement a RecyclerView
create a .xml that have what you want inside example:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/gradient_bg"
android:orientation="horizontal"
android:layout_margin="1dp"
android:padding="1dip" >
<LinearLayout android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="17dip"
android:layout_alignParentLeft="true"
android:layout_marginRight="2dip">
<ImageView
android:id="#+id/gasImagem"
android:contentDescription="cover"
android:layout_width="100dip"
android:layout_height="100dip"
/>
</LinearLayout>
<TextView
android:id="#+id/gasTitulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thumbnail"
android:layout_toRightOf="#+id/thumbnail"
android:textColor="#040404"
android:layout_marginTop="30dp"
android:typeface="sans"
android:textSize="20sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/gasPreco"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#000000"
android:textStyle="bold"
android:layout_marginTop="95dp"
android:layout_toRightOf="#+id/thumbnail"/>
<Button
android:id="#+id/btCarro"
android:layout_width="50dp"
android:layout_height="30dp"
android:background = "#drawable/roundedbutton"
android:layout_marginTop="95dp"
android:layout_marginLeft="310dp"
android:drawableTop="#drawable/shoppingcart"
android:textAlignment="center"
/>
</RelativeLayout>
After this create an adapter example
public class MyAdaptadorRecycler extends RecyclerView.Adapter<MyAdaptadorRecycler.ViewHolder> {
private List<Produto>gasList;
private LayoutInflater layout;
public MyAdaptadorRecycler(Context c,List<Produto>l){
gasList = l;
layout = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = layout.inflate(R.layout.list_row,parent,false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.ivcapa.setImageResource(gasList.get(position).getImagem());
holder.tvtitulo.setText(gasList.get(position).getNome());
holder.tvPreco.setText(String.valueOf(gasList.get(position).getPreco()) + "€");
final int posicao = position;
holder.bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Carrinho: ", Toast.LENGTH_SHORT).show();
}
});
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "Recycle Click", Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return gasList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
protected TextView tvtitulo, tvPreco;
protected ImageView ivcapa;
protected Button bt;
public ViewHolder(View itemView) {
super(itemView);
this.tvtitulo = (TextView) itemView.findViewById(R.id.gasTitulo);
this.ivcapa = (ImageView) itemView.findViewById(R.id.gasImagem);
this.tvPreco = (TextView)itemView.findViewById(R.id.gasPreco);
this.bt = (Button)itemView.findViewById(R.id.btCarro);
}
}
}
Maybe you will need a divider exemple:
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
private final int mVerticalSpaceHeight;
public DividerItemDecoration(int mVerticalSpaceHeight) {
this.mVerticalSpaceHeight = mVerticalSpaceHeight;
}
#Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
RecyclerView.State state) {
outRect.bottom = mVerticalSpaceHeight;
//outRect.left = mVerticalSpaceHeight;
//outRect.right = mVerticalSpaceHeight;
}
}
Then in your mainActivity you need to do this:
LinearLayoutManager llm = new LinearLayoutManager(this);
this.rv.setLayoutManager(llm);
rv.addItemDecoration(new DividerItemDecoration(20));
rv.setHasFixedSize(true);
nr= 1;
this.listaPordutos = new ArrayList<Produto>();
this.adapatadorLivros = new MyAdaptadorRecycler(this, listaPordutos);
rv.setAdapter(this.adapatadorLivros);
This is just my exemples, that I use to create a program
hope this can help you, any doubt just say :)
Refer this official documentation link on how to use RecyclerView.LayoutManager http://developer.android.com/training/material/lists-cards.html