2017-07-18 153 views
0

我已經實施了ListViewCardView s以下一些在線教程。包含列表ListView與CardViews奇怪的行爲與保證金和填充

佈局

片段

<ListView android:id="@+id/interactions_list" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentStart="true" android:layout_marginTop="10dp" android:visibility="invisible" android:layout_alignParentTop="true" android:divider="#000000" android:dividerHeight="4dp" /> 

元件佈局

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:card_view="http://schemas.android.com/apk/res-auto" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:padding="16dp" 
     android:orientation="vertical" 
    > 
        <android.support.v7.widget.CardView 
      android:background="#00000000" 
      android:layout_width="fill_parent" 
      android:layout_height="100dp" 
      android:layout_gravity="center" 
      android:layout_margin="5dp" 
      card_view:cardCornerRadius="2dp" 
      card_view:contentPadding="10dp" 
      android:id="@+id/cv"> 

             <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:padding="0dip"> 

        <TableRow android:padding="5dip"> 
         <ImageView 
          android:id="@+id/pkicon" 
          android:layout_width="50dp" 
          android:layout_height="50dp" 
          android:layout_margin="3dp"/> 

         <LinearLayout android:id="@+id/lnlayout" 
          android:layout_width="50dp" 
          android:layout_height="wrap_content" 
          android:layout_weight="1" 
          android:orientation="vertical" 
          android:layout_margin="3dip"> 

          <TextView 
           android:id="@+id/pkname" 
           android:layout_width="wrap_content" 
           android:layout_height="30dp" 
           android:textColor="#000000" 
           android:textSize="20sp"/> 

          <TextView 
           android:id="@+id/pknot" 
           android:layout_width="wrap_content" 
           android:layout_height="20dp" 
           android:textColor="#606060"/> 

         </LinearLayout> 

        </TableRow> 
       </TableLayout> 
        </android.support.v7.widget.CardView> 

    </LinearLayout> 

現在我遇到當用戶點擊一個卡,這個截圖是看到奇怪的行爲:

enter image description here

卡片顯示正確,但當輕拍時,連鎖效果將覆蓋LinearLayout元素,並且卡片具有堅實的不透明背景。

預期行爲:卡背後的背景保持白色,波紋效果僅出現在卡上。

請幫忙。

+0

您想讓卡片內部的卡片可點擊。目前在父級ListView上有一個onItemSelectedListener集,它在物品下面繪製波紋背景。也許你想用RecyclerView代替。 RecyclerView沒有這種混亂的糖。你將負責點擊監聽器。從這裏開始https://developer.android.com/training/material/lists-cards.html –

回答

0

也許鮑勃迪倫的LinearLayoutwidthheightwrap_content可以解決

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:card_view="http://schemas.android.com/apk/res-auto" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:padding="16dp" 
android:orientation="vertical"> 
+0

不幸的是沒有工作。 –

1

這種情況正在發生的問題,因爲你的父母的看法是線性佈局紋波的原因正在創建使用就可以了

app:cardBackgroundColor="?selectableItemBackground" 

android:background="?selectableItemBackground" 

android:foreground="?selectableItemBackground" 
在cardview佈局

,並設置你點擊收聽此cardview佈局不父即。 LinearLayout中。

+0

背景提示無法使用。如何在卡上設置聽衆?此刻,我在整個列表中都有一個監聽器'setOnItemClickListener'。 –

+0

我希望你使用自定義適配器這裏是參考如何設置點擊監聽器https://stackoverflow.com/a/12596403/5492047和這一個http://androidforbeginners.blogspot.in/2010/03/clicking-按鈕在列表視圖row.html如果它不起作用讓我知道。 –

+0

非常感謝!這有助於。 –