[英]How To Update CardView When Click on it?
我有一個RecyclerView並將CardView用作我的物品。 我想要當我單擊CardView時,該視圖更新並顯示另一個布局(或將某些布局設置為可見和不可見)。
這是我的onBindViewHolder代碼:
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);
}
});
}
這是我的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>
但是,當我單擊CardView時,Cardview消失了,在我的應用程序中將是空白..
當我單擊productInfo,我的profileUtils顯示時,該怎么辦?
您不應將整個CardView的可見性更改為已消失,而應在CardView中隱藏布局。
例如 :
viewHolder.productView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewHolder.productInfos.setVisibility(View.GONE);
viewHolder.productUtils.setVisibility(View.VISIBLE);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.