2015-09-08 129 views
1

我確實在做錯事。我有一個ReclyclerView和每個單元都有一個XML home_cells.xmlAndroid onTouchListener無法正常工作,只能在Padding區域工作

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:paddingRight="12dp" 
    android:paddingLeft="12dp" 
    android:paddingTop="5dp" 
    android:paddingBottom="5dp" 
    android:clickable="true"> 

    <android.support.v7.widget.CardView 
     android:id="@+id/home_cv" 
     android:clickable="true" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <RelativeLayout 
      ...... 
      android:paddingTop="16dp"> 

      <LinearLayout 
       .... 
       android:orientation="horizontal"> 


       <ImageView 
        ... 
        android:src="@drawable/abc_ratingbar_full_material" /> 

       <LinearLayout 
        ...... 
        android:orientation="vertical"> 

        <TextView 
         ..... 
         android:textStyle="bold" /> 

        <TextView 
         .... 
         android:textColor="@color/secondaryTextColor" /> 

       </LinearLayout> 

       <ImageView 
        .... 
        android:src="@mipmap/ic_chevron_right_black_24dp" /> 
      </LinearLayout> 
     </RelativeLayout> 
    </android.support.v7.widget.CardView> 
    <!--<lay--> 
</LinearLayout> 

我有一個適配器...一切正常.....一點問題都沒有。

但是,當我嘗試實現onClickLister完成單元格視圖mView它不起作用。如果我在角落(我有填充的地方)接觸,那麼它是可行的。

我不知道發生了什麼。如果我將偵聽器設置爲單元格中的每個元素,它就會工作......但不是整個單元格。

下面是我viewHolder

class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 
     CardView cv; 
     ImageView icon; 
     TextView heading; 
     TextView subheading; 
     View mView; 


     public MyViewHolder(View itemView) { 

      super(itemView); 


      Log.d(TAG, "setting images and items"); 
      mView=itemView; 
      cv = (CardView)itemView.findViewById(R.id.home_cv); 
      icon =(ImageView) itemView.findViewById(R.id.home_rv_icon); 
      heading =(TextView) itemView.findViewById(R.id.heading); 
      subheading =(TextView) itemView.findViewById(R.id.subhead); 
      icon.setOnClickListener(this); // Working 
      mView.setOnClickListener(this); // NOT working 
      mView.setOnTouchListener(new View.OnTouchListener() { 
       @Override 
       public boolean onTouch(View arg0, MotionEvent arg1) { 

        // NOT Working 
        return true; 
       } 
      }); 
     } 

     @Override 
     public void onClick(View view) { 
      // 
      Log.d(TAG,"on click"); 
      if(clickListener!=null){ 
       Log.d(TAG,"listner notnull"); 
       clickListener.itemClick(view,getAdapterPosition()); 
      } 

     } 


    } 


    public interface RVClickListener{ 
     //OnClick will work, but for better coding standard i made this listener to call the onclick from the fragment it self.. 
     public void itemClick(View view, int position); 


    } 
+1

每個孩子視圖在'mView'你想實現an' onClickListener'或刪除longClickable = 「真」一個onTouchListener?另外,你確定它不起作用嗎?你在那裏打印任何陳述?另外,什麼是'clickListener'變量?你在哪裏初始化? –

回答

2

鬥爭的幾個小時之後,如果最終確定它。這是問題。

由於我的佈局具有子視圖(佈局),onclicklistener被他們捕獲。

因此通過簡單的解決方案解決了這個問題。我關閉了所有子視圖的clickable並將click監聽器應用到父級。

一切爲了孩子的觀點:

android:clickable="false" 
+0

謝謝!幫助我解決了類似的問題。 – Mavamaarten

0

從的CardView佈局

+0

你能詳細說明爲什麼這會解決問題嗎? – liorsolomon

+0

當您爲** CardView佈局的任何子視圖使用** longClickable =「true」**時,它們將佔用onClick的所有佈局區域,並且單擊停靠點在主CardView上工作。刪除此並在父級CardView佈局中添加** OnClickable =「true」**可解決此問題。希望能幫助到你... –