This question is already answered but not for cardview I guess, I have already gone through most answers and experimented with them still my problem is not solved.
I have a recyclerview with adapter and a cardview now I have added a button in a cardview and I want to change the visibility of this button to invisible when the button is clicked, but when I do so the visibility of other buttons in other cards like at position=9 get affected and becomes invisible but I never clicked the button at 9th card.
I found some solutions like writing the onClick under onBindViewHolder method. I did so but still its not working please help!
Here is the code for cardview
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/shop_cardview"
android:layout_width="match_parent"
android:layout_height="280dp"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="5dp"
card_view:cardMaxElevation="5dp"
card_view:contentPadding="5dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/btnGrey">
<com.android.volley.toolbox.NetworkImageView
android:id="#+id/shopVolleyImageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#mipmap/pyaa_logo_iii"
card_view:layout_constraintBottom_toBottomOf="parent"
card_view:layout_constraintEnd_toEndOf="parent"
card_view:layout_constraintStart_toStartOf="parent"
card_view:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/shopImageNameTV"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginTop="8dp"
android:text="Json Image Name"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="20dp"
card_view:layout_constraintEnd_toEndOf="parent"
card_view:layout_constraintStart_toStartOf="parent"
card_view:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="Button"
card_view:layout_constraintBottom_toBottomOf="parent"
card_view:layout_constraintEnd_toEndOf="parent"
card_view:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Here is the code for recyclerviewadapter
public class shopRecyclerViewAdapter extends RecyclerView.Adapter<shopRecyclerViewAdapter.ViewHolder> {
Context context;
List<shopDataAdapter> dataAdapters;
ImageLoader imageLoader;
public shopRecyclerViewAdapter(List<shopDataAdapter> getDataAdapter,Context context){
super();
this.dataAdapters=getDataAdapter;
this.context=context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_shop,parent,false);
ViewHolder vh=new ViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(final ViewHolder Viewholder, int position) {
shopDataAdapter dataAdapterOBJ= dataAdapters.get(position);
imageLoader=shopImageAdapter.getInstance(context).getImageLoader();
imageLoader.get(dataAdapterOBJ.getshopImageUrl(),
ImageLoader.getImageListener(
Viewholder.VollyImageView,
R.mipmap.ic_launcher,
android.R.drawable.ic_dialog_alert
)
);
Viewholder.VollyImageView.setImageUrl(dataAdapterOBJ.getshopImageUrl(),imageLoader);
Viewholder.ImageTitleTV.setText(dataAdapterOBJ.getshopName());
Viewholder.check.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Viewholder.check.setVisibility(View.INVISIBLE);
}
});
}
#Override
public int getItemCount() {
return dataAdapters.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView ImageTitleTV;
public NetworkImageView VollyImageView;
public Button check;
public ViewHolder(View itemView) {
super(itemView);
ImageTitleTV=itemView.findViewById(R.id.shopImageNameTV);
VollyImageView=itemView.findViewById(R.id.shopVolleyImageView);
check=itemView.findViewById(R.id.button);
}
}
}
You can do like this declare global variable clickPosition = -1
public void onBindViewHolder(final ViewHolder Viewholder, final int position) {
Viewholder.check.setVisibility(View.VISIBLE);
Viewholder.check.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
clickPosition = position;
notifyDataSetChanged();
}
});
if(clickPosition == position){
Viewholder.check.setVisibility(View.INVISIBLE);
}
}
Don't forgot to write line Viewholder.check.setVisibility(View.VISIBLE); otherwise it will give strange behavior.
If you can give more explain what particular behavior you want I can give more explanation or exact solution.
Related
I design a layout and I get a problem with ScrollView. Specially, I have a ScrollView and a RecyclerView inside this ScrollView. When I select items in the last row of RecyclerView, the View does not stay at the last row, it moves up to the upper row.
For example, my view has 4 rows. When I select an item in 4th row, the view automatically moves up to 3th row. So, how I can prevent it so that when I select items in the last row, the view will stay and not move up? Thank you
Layout:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/selectPayment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:text="#string/select_payment_method"
android:textSize="#dimen/sp_20"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/choosePayment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="#dimen/sp_16"
android:text="#string/choose_payment_method"
app:layout_constraintStart_toStartOf="#+id/selectPayment"
app:layout_constraintTop_toBottomOf="#+id/selectPayment" />
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="#dimen/dp_30"
android:layout_marginBottom="10dp"
android:fadeScrollbars="false"
app:layout_constraintBottom_toTopOf="#+id/continueButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/choosePayment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/paymentMethodRV"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
<Button
android:id="#+id/continueButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/continue_in_cap"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Adapter:
public class PaymentAdapter extends RecyclerView.Adapter<PaymentAdapter.ViewHolder> {
private Context context;
private List<CardItems> cardItemsList;
private int rowIndex = -1;
public PaymentAdapter(Context context, List<CardItems> cardItemsList) {
this.context = context;
this.cardItemsList = cardItemsList;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.payment_method_items, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.paymentImage.setImageResource(cardItemsList.get(position).getCardImage());
holder.paymentTitle.setText(cardItemsList.get(position).getCardNumber());
holder.setItemClickListener(new ItemClickListener() {
#Override
public void onItemClick(int position) {
rowIndex = position;
notifyDataSetChanged();
}
});
//set highlight color when option is clicked
if (rowIndex == position){
holder.frameContainer.setBackgroundResource(R.drawable.green_rounded_button);
}
else {
holder.frameContainer.setBackgroundResource(R.drawable.white_grey_rounded_button);
}
}
#Override
public int getItemCount() {
return cardItemsList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView paymentImage;
TextView paymentTitle;
FrameLayout frameContainer;
ItemClickListener itemClickListener;
public ViewHolder(#NonNull View itemView) {
super(itemView);
paymentImage = itemView.findViewById(R.id.paymentImage);
paymentTitle = itemView.findViewById(R.id.paymentTitle);
frameContainer = itemView.findViewById(R.id.frameContainer);
itemView.setOnClickListener(this);
}
//handle click on each category
#Override
public void onClick(View view) {
itemClickListener.onItemClick(getAdapterPosition());
notifyDataSetChanged();
}
public void setItemClickListener(ItemClickListener listener){
itemClickListener = listener;
}
}
//for listening from holder
public interface ItemClickListener{
void onItemClick(int position);
}
}
I have passed the parent to onCreateViewHolder instead of null but it is not showing match parent.
Please tell me if there is any error in the code?
Here is my Main_Adapter.class:
public class Main_Adapter extends RecyclerView.Adapter<Main_Adapter.ViewHolder> {
private List<MainModel> mainModels;
private Context context;
private OnClicklisteners listener;
public Main_Adapter(List<MainModel> mainModels, Context context, OnClicklisteners onClicklisteners) {
this.mainModels = mainModels;
this.context = context;
this.listener=onClicklisteners;
}
public interface OnClicklisteners{
public void onPosClicked(View view, int pos, ImageView mainimage, ArrayList<Integer> colorlist);
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_card, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.main_text.setText(mainModels.get(position).getName());
Picasso.with(context).load(mainModels.get(position).getImageName()).fit().centerInside().into(holder.mainimage);
}
#Override
public int getItemCount() {
return mainModels.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
#BindView(R.id.main_image)
ImageView mainimage;
#BindView(R.id.main_text)
TextView main_text;
Typeface ubuntu;
int colors;
ArrayList<Integer> colorlist =new ArrayList<>();
public ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
int pos = getAdapterPosition();
listener.onPosClicked(view,pos,mainimage,colorlist);
}
}
Here is main_card.xml:
<android.support.v7.widget.CardView android:id="#+id/movie_container"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_gravity="center"
android:clickable="true"
android:layout_margin="2dp"
android:foreground="?attr/selectableItemBackground"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="2dp"
xmlns:card_view="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<ImageView
android:layout_marginTop="50dp"
android:id="#+id/main_image"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_gravity="center"
android:background="#color/white"
android:scaleType="centerCrop"
/>
<TextView
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/main_text"
android:textSize="16sp"
android:text="hello"
android:textAlignment="center"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
Here is a screenshot:
It should show full width but it's showing only a wrap content. I have found a similar post but they are telling to pass parent in onCreateViewHolder. I have done that but it didn't help me.
I am using RecyclerView and CardView. I am following the WhatsApp like UI. When the Whatsapp user long presses the Contacts in the Chat Tab of Home Screen it enables the users to select multiple Contacts at the same time.
Whatsapp Multiselect
I want the user to multiselect by clicking anywhere in the cardview just like in whatsapp screen. I am stuck at the clicking of the CardView inside recycler view. I want only the onclick of cardview, rest of the clicks of items inside to be not clickable so that they dont interfere when user is multiselecting. Any help in resolving this issue will be greatly appreciated.
CardView is also a View you can set View.OnClickListener on CardView.
public class MyViewHolder extends RecyclerView.ViewHolder {
public View view;
public MyViewHolder(View view) {
super(view);
this.view = view;
}
}
And in your onBindViewHolder()
#Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
holder.view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Your Logic
}
});
}
Or You can do this.
CardView in xml
<android.support.v7.widget.CardView
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="#dimen/card_margin"
android:elevation="3dp"
card_view:cardCornerRadius="#dimen/card_album_radius">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_margin="7dp"
android:id="#+id/thumbnail"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_centerHorizontal="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:scaleType="fitXY"/>
<TextView
android:id="#+id/channel_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/thumbnail"
android:paddingLeft="#dimen/album_title_padding"
android:paddingRight="#dimen/album_title_padding"
android:paddingTop="#dimen/album_title_padding"
android:textColor="#4c4c4c"
android:textSize="16dp"
android:text="Ary News"
android:fontFamily="sans-serif-smallcaps"
android:gravity="center_horizontal"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Watch Now"
android:layout_below="#id/channel_name"
android:fontFamily="sans-serif-smallcaps"
android:id="#+id/watch_now"
android:textStyle="bold"
android:backgroundTint="#color/colorAccent"/>
<!--This is the view-->
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/view"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
And in your RecyclerView Adapter
public class MyViewHolder extends RecyclerView.ViewHolder {
public View view;
public MyViewHolder(View view) {
super(view);
this.view = findViewById(R.id.view);
}}
In onBindViewHolder()
#Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
holder.view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context,"Card clicked",Toast.LENGTH_SHORT).show();
}
});
}
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.
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>