2016-02-26 58 views
1

我正在使用ListView來製作此佈局(ScreenShot)。這裏當我點擊Button 1st時,它不起作用。但是,如果我點擊ListItem第一,然後我點擊按鈕它的作品。ListView中的按鈕不起作用

enter image description here

即使我用機器人:descendantFocusability = 「blocksDescendants」在佈局。 android:focusable =「false」& android:focusableInTouchMode =「false」裏面的按鈕。但沒有任何工作。

請幫我解決它。

我的代碼: XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:descendantFocusability="blocksDescendants" 
    android:orientation="vertical"> 

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:card_view="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/cardlist_item" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" 
     android:background="@drawable/curved_shape" 
     android:orientation="vertical" 
     card_view:cardElevation="7dp"> 


     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="@color/white" 
       android:orientation="horizontal"> 

       <ImageView 
        android:layout_width="80dp" 
        android:layout_height="80dp" 
        android:layout_margin="14dp" 
        android:layout_marginRight="14dp" 
        android:src="@drawable/image_4" /> 

       <LinearLayout 
        android:layout_width="wrap_content" 
        android:layout_height="match_parent" 
        android:layout_margin="14dp" 
        android:orientation="vertical"> 

        <TextView 
         android:id="@+id/cartProduct" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_marginTop="8dp" 
         android:text="Fusce veh" 
         android:textColor="#9F9F9F" /> 

        <TextView 
         android:id="@+id/cartQuantity" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_marginTop="8dp" 
         android:text="100 g" 
         android:textColor="#9F9F9F" /> 

        <LinearLayout 
         android:layout_width="225dp" 
         android:layout_height="wrap_content" 
         android:orientation="horizontal" 
         android:weightSum="1"> 

         <TextView 
          android:id="@+id/symbol" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_marginTop="8dp" 
          android:text="Rs " 
          android:textColor="#9F9F9F" /> 

         <TextView 
          android:id="@+id/cartPrice" 
          android:layout_width="0dp" 
          android:layout_height="wrap_content" 
          android:layout_marginRight="10dp" 
          android:layout_marginTop="8dp" 
          android:layout_weight="0.48" 
          android:text="19.99" 
          android:textColor="#9F9F9F" /> 

         <TextView 
          android:id="@+id/cartPriceDum" 
          android:layout_width="0dp" 
          android:layout_height="wrap_content" 
          android:layout_marginRight="10dp" 
          android:layout_marginTop="8dp" 
          android:layout_weight="0.48" 
          android:text="19.99" 
          android:textColor="#9F9F9F" 
          android:visibility="gone" /> 

         <ImageView 
          android:id="@+id/ivDecrease" 
          android:layout_width="0dp" 
          android:layout_height="wrap_content" 
          android:layout_marginLeft="10dp" 
          android:layout_weight="0.1" 
          android:focusable="false" 
          android:focusableInTouchMode="false" 
          android:src="@drawable/ic_action_expand" /> 

         <TextView 
          android:id="@+id/cartCount" 
          android:layout_width="0dp" 
          android:layout_height="wrap_content" 
          android:layout_gravity="center" 
          android:layout_marginLeft="1dp" 
          android:layout_marginRight="1dp" 
          android:layout_weight="0.2" 
          android:gravity="center" 
          android:text="1" 
          android:textSize="15dp" 
          android:textStyle="bold" /> 

         <ImageView 
          android:id="@+id/ivIncrease" 
          android:layout_width="0dp" 
          android:layout_height="wrap_content" 
          android:layout_weight="0.1" 
          android:focusable="false" 
          android:focusableInTouchMode="false" 
          android:src="@drawable/ic_action_collapse" /> 
        </LinearLayout> 
       </LinearLayout> 
      </LinearLayout> 

      <View 
       android:layout_width="match_parent" 
       android:layout_height="1dip" 
       android:background="#E4E4E4" /> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="#F8F8F8" 
       android:descendantFocusability="blocksDescendants" 
       android:orientation="horizontal" 
       android:weightSum="1"> 

       <Button 
        android:id="@+id/addTowish" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_weight="0.5" 
        android:background="#F8F8F8" 
        android:focusable="false" 
        android:focusableInTouchMode="false" 
        android:text="ADD TO WISHLIST" 
        android:textColor="#787878" /> 

       <View 
        android:layout_width="1dip" 
        android:layout_height="match_parent" 
        android:background="#E4E4E4" /> 

       <Button 
        android:id="@+id/remove" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_weight="0.5" 
        android:background="#F8F8F8" 
        android:focusable="false" 
        android:focusableInTouchMode="false" 
        android:text="REMOVE" 
        android:textColor="#787878" /> 
      </LinearLayout> 
     </LinearLayout> 

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

</LinearLayout> 
+2

處理該按鈕從適配器中單擊功能。這將解決你的問題.. – Chaitu

+0

是的。它的工作。謝謝 –

+0

有疑問@Chaitu –

回答

1

你必須clicklistener添加到您的按鈕適配器類內

public DataObjectHolder(View itemView) { 
     super(itemView); 

     CardView cardClick = (CardView) itemView.findViewById(R.id.xxx); 

     btn = (Button) itemView.findViewById(R.id.xxx); 
     Log.i(LOG_TAG, "Adding Listener"); 

     cardClick.setOnClickListener(this); 
     btn.setOnClickListener(this); 
    } 
+0

是的。它的工作。謝謝 –

+0

歡迎........ – MinnuKaAnae

+0

其解決了我的問題 –

1

請來自你的按鈕刪除android:focusable="false"android:focusableInTouchMode="false"

由於您禁用了可聚焦性,因此不會在第一次點擊時作出反應。當你點擊列表視圖時,該行獲得焦點並因此工作。

還從包含按鈕的線性佈局中刪除android:descendantFocusability="blocksDescendants"