2017-02-18 77 views
2

最小SDK爲21.當我點擊我的回收適配器中的卡片視圖時,漣漪效應不會發生,只會進入下一個屏幕。 recyclerview在片段內。Cardview漣漪效應不起作用

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <android.support.v7.widget.CardView 
     android:id="@+id/entire_card" 
     android:layout_width="match_parent" 
     android:layout_height="265dp" 
     android:layout_margin="8dp" 
     android:foreground="?android:attr/selectableItemBackground" 
     app:cardUseCompatPadding="true" 
     app:cardCornerRadius="2dp" 
     > 


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

      <ProgressBar 
       android:id="@+id/progressbar" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerInParent="true" 
       android:layout_centerHorizontal="true" 
       android:paddingTop="120dp" 
       android:visibility="visible"/> 


      <ImageView 
       android:id="@+id/pet_image" 
       android:layout_width="match_parent" 
       android:layout_height="210dp" 
       android:layout_alignParentTop="true" 
       android:scaleType="centerCrop" 
       android:src="@drawable/placeholder" 
       android:visibility="gone" 
       /> 

      <TextView 
       android:id="@+id/pet_description" 
       android:layout_width="fill_parent" 
       android:layout_height="55dp" 
       android:layout_below="@+id/pet_image" 
       android:padding="10dp" 
       android:textColor="#FFFFFF" 
       android:visibility="gone" 
       android:textSize="20sp" 
       android:background="@color/primaryColour" 
       /> 

     </RelativeLayout> 
    </android.support.v7.widget.CardView> 

</LinearLayout> 

適配器代碼,其中我有我的onClick爲每個項目。

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 
     public TextView petInfo; 
     public ImageView imgURL; 
     public ProgressBar progressBar; 

     private Context itemContext; 
     public ViewHolder(View v){ 
      super(v); 
      imgURL = (ImageView) v.findViewById(R.id.pet_image); 
      petInfo = (TextView) v.findViewById(R.id.pet_description); 
      progressBar = (ProgressBar) v.findViewById(R.id.progressbar); 

      itemContext = v.getContext(); 

      v.setOnClickListener(this); 
     } 

     @Override 
     public void onClick(View v){ 

      Intent intent= new Intent(itemContext, DetailCardLayout.class); 
      Integer position = getAdapterPosition(); 
      intent.putExtra("CARDVIEW_POSITION", position); 
      v.getContext().startActivity(intent); 

     } 
    } 
+0

將您的CardView設置爲根佈局,然後嘗試。 –

+0

這有幫助!然而,漣漪效應只能用於兩次點擊。即點擊卡片視圖,進入下一個屏幕並返回。重複。那麼當你第三次點擊沒有更多的連鎖反應 – DessertsAndStuff

回答

1

嘗試添加

android:clickable="true" 

到您的卡片視圖XML

+0

這似乎有幫助。但是,當我滾動看到更多(2張牌佔用一個屏幕)後,前兩張牌看起來不起作用。 – DessertsAndStuff

0

嘗試用的前景屬性包裝你的形象在FrameLayoutandroid:foreground="?attr/selectableItemBackground"

例如

<FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:clickable="true" 
     android:focusable="true" 
     android:foreground="?attr/selectableItemBackground" 
     > 

     <ImageView 
      android:id="@+id/pet_image" 
      android:layout_width="match_parent" 
      android:layout_height="210dp" 
      android:layout_alignParentTop="true" 
      android:scaleType="centerCrop" 
      android:src="@drawable/placeholder" 
      android:visibility="gone" 
      /> 

    </FrameLayout> 
0

你的卡片視圖包含相對佈局覆蓋卡片視圖表面,所以寫在相對佈局連鎖反應。

<android.support.v7.widget.CardView 
    android:id="@+id/cardView" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:cardPreventCornerOverlap="false" 
    app:cardElevation="2dp" 
    app:cardUseCompatPadding="false" 
    app:cardCornerRadius="2dp" 
    android:layout_marginBottom="8dp" 
    android:layout_marginRight="8dp" 
    android:layout_marginLeft="8dp" 
    > 
    <RelativeLayout 
     android:id="@+id/rl_bookmark" 
     android:background="?attr/selectableItemBackground" 
     android:clickable="true" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> ...