Farhad Navayazdan
Farhad Navayazdan

Reputation: 1121

How To Update CardView When Click on it?

I have a RecyclerView and Use CardView as my items on it. I want when i click on a CardView , That view update and show another layout ( or set some layout visible and invisable ) .

This is My onBindViewHolder code :

  public void onBindViewHolder(final ViewHolder viewHolder, int i) {
    ProductTO product = products.get(i);


    viewHolder.productUtils.setVisibility(View.GONE);

    viewHolder.productName.setText(product.getProductName());
    viewHolder.productPrice.setText(product.getProductPrice() + Constants.CURRENCY);
    viewHolder.productOldPrice.setText(product.getProductOldPrice() + Constants.CURRENCY);
    viewHolder.productOldPrice.setPaintFlags(viewHolder.productOldPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
    arcxxImageLoader.LoadImage(product.getProductImages(), viewHolder.productImage);


    viewHolder.productView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            viewHolder.productView.setVisibility(View.GONE);
            viewHolder.productUtils.setVisibility(View.VISIBLE);
        }
    });


}

And This is my Cardview_layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    android:id="@+id/productView"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_margin="5dp"
    card_view:cardCornerRadius="5dp"
    >


<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


    <LinearLayout
            android:id="@+id/productUtils"
            android:padding="10dp"
            android:orientation="vertical"
            android:gravity="right"
            android:weightSum="14"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            >


        <EditText
                android:text="test :|"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>


    </LinearLayout>


    <LinearLayout
            android:id="@+id/productInfos"
            android:padding="10dp"
            android:orientation="vertical"
            android:gravity="right"
            android:weightSum="14"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            >


        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="10"
                >


            <ImageView
                    android:id="@+id/productImage"
                    android:layout_width="110dp"
                    android:layout_height="130dp"
                    android:scaleType="centerCrop"
                    android:tint="@color/abc_primary_text_disable_only_material_dark"
                    />

        </LinearLayout>


        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="2"
                >

            <TextView
                    android:id="@+id/productName"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:focusable="true"
                    android:clickable="true"
                    android:textSize="16sp"
                    android:textColor="#2d2d2d"
                    />

        </LinearLayout>


        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                >

            <TextView
                    android:id="@+id/productOldPrice"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:focusable="true"
                    android:clickable="true"
                    android:textSize="14sp"
                    android:textColor="#fff2594e"

                    />

        </LinearLayout>


        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                >

            <TextView
                    android:id="@+id/productPrice"
                    android:gravity="center"
                    android:focusable="true"
                    android:clickable="true"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textSize="14sp"
                    android:textColor="#ff14ff11"
                    />

        </LinearLayout>


    </LinearLayout>


</RelativeLayout>

But When i Click on a CardView The Cardview Gone and it will be white Space on my app ..

How Can i do this when i click on productInfo , My profileUtils Show up ?

Upvotes: 1

Views: 1863

Answers (1)

Mohamed Allam
Mohamed Allam

Reputation: 125

You shouldn't change the visibility of the whole CardView to gone , instead you should hide a layout inside the CardView .

for Example :

viewHolder.productView.setOnClickListener(new View.OnClickListener()      {
    @Override
    public void onClick(View v) {
        viewHolder.productInfos.setVisibility(View.GONE);
        viewHolder.productUtils.setVisibility(View.VISIBLE);
    }
});

Upvotes: 1

Related Questions