Hi I am have a question regarding CardViews
in a RecyclerView
.My CardViews basically has 2 segments i.e. the base card and the expanded card. My OnClick
just sets the setVisibility
property of each segment to either GONE
or VISIBLE
. Right now my OnClickListener
is in the OnBindViewHolder
method. I have read that it is better to put it in the ViewHolder
class, however, I am not sure how to achieve the same result at this moment. My main problem is best represented by the images below:
This is the default way the images should be displayed or "base_view"
When you click on the image, the card will show information related to that card, aka the "expanded_view".
How can I prevent the other CardViews(the middle and right card) from changing (from a square to the long rectangle) when I change the layout of one card? Example as follows:
This is my CardView XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/tools"
android:layout_gravity="center"
app:cardCornerRadius="8dp"
android:layout_margin="1dp"
android:id="@+id/cv_army">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="90dp"
android:layout_height="90dp"
android:orientation="vertical"
android:id="@+id/base_card">
<ImageView
android:layout_width="90dp"
android:layout_height="120dp"
android:layout_margin="2dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:src="@drawable/summon_cm"
android:scaleType="fitCenter"
android:id="@+id/iv_card"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/iv_card"
android:layout_marginBottom="10dp"
android:layout_centerHorizontal="true"
android:textColor="#000"
android:text="Test"
android:textAllCaps="false"
android:textSize="10dp"
android:textAlignment="center"
android:background="#ffffff"
android:backgroundTint="@color/trans_success_stroke_color"
android:id="@+id/tv_card"/>
</RelativeLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="100dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:background="@color/colorPrimaryDark"
android:id="@+id/expanded_card">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:id="@+id/card_popup_name"
android:layout_alignParentTop="true"
android:layout_marginTop="5dp"
android:layout_centerHorizontal="true"
android:text="Card Name"/>
<ImageView
android:layout_width="90dp"
android:layout_height="80dp"
android:src="@drawable/bg_summon"
android:scaleType="fitCenter"
android:id="@+id/card_popup_img"
android:layout_below="@id/card_popup_name"
android:layout_marginTop="5dp"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="35dp"
android:layout_height="wrap_content"
android:layout_below="@id/card_popup_img"
android:layout_marginTop="10dp"
android:layout_alignStart="@id/card_popup_img"
android:id="@+id/card_popup_hp"
android:text="HP"
android:textAlignment="viewEnd"
android:layout_gravity="end"/>
<TextView
android:text="123"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/card_popup_hp"
android:layout_alignStart="@id/card_popup_img"
android:layout_marginStart="45dp"
android:id="@+id/tv_hp" />
<Button
android:text="Use"
android:textSize="10dp"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="5dp"
android:layout_centerHorizontal="true"
android:id="@+id/btn_use" />
</RelativeLayout>
</RelativeLayout></android.support.v7.widget.CardView>
I put my onBindView here as well just in case it is necessary:
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
holder.base_card.setVisibility(View.VISIBLE);
holder.expanded_card.setVisibility(View.GONE);
final int p = position;
String s = card_list.get(p).card_name;
final String sx = s.substring(1); //The 1st position [index 0] stores the rarity of the card, so remove that from the string to get the name
holder.tv_card.setText(sx);
holder.iv_card.setImageResource(card_list.get(p).card_img);
//RV_Animator.animate(holder);
holder.iv_card.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.expanded_card.setVisibility(View.VISIBLE);
holder.base_card.setVisibility(View.GONE);
for (int ix =0; ix<getItemCount();ix++){
if (ix!=p){
notifyItemChanged(ix);
}
}
}
});
holder.expanded_card.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View vy) {
holder.expanded_card.setVisibility(View.GONE);
holder.base_card.setVisibility(View.VISIBLE);
}
});
holder.btn_use.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View vx) {
new SweetAlertDialog(context, SweetAlertDialog.SUCCESS_TYPE)
.setTitleText("Success!")
.setContentText("asd")
.show();
}
});
}