2017-08-10 38 views
0

我有一個卡片視圖,其中包含一些小部件,卡片視圖中有一個回收器視圖。 Cardview是父母。Android:單擊父母時Clicklistener在點擊孩子時不起作用

我希望當點擊cardview中的任何地方時,事件就會發生。但是,現在只有當卡片視圖的頂部被點擊時才觸發,但是當我點擊回收者視圖時不會觸發。

我試着將recyclerview中的clickable設置爲false,並且它不起作用。

這是我的xml。

<android.support.v7.widget.CardView 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/card_view_today" 
    android:layout_gravity="center" 
    android:layout_width="match_parent" 
    android:layout_margin="10dp" 
    android:layout_height="wrap_content" 
    card_view:cardCornerRadius="4dp" 
    android:padding="5dp" 
    android:clickable="true" 

    > 

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="40dp" 
     android:clickable="false" 
     android:gravity="center_vertical" 
     > 
     <ImageView 
      android:layout_width="20dp" 
      android:layout_marginLeft="5dp" 
      android:layout_height="20dp" 
      android:clickable="false" 
      android:src="@drawable/today_icon_small"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:clickable="false" 
      android:text="I still need to have today" 
      android:layout_margin="5dp"/> 

    </LinearLayout> 


    <android.support.v7.widget.RecyclerView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/today_recycler_view_summary" 
     android:paddingLeft="20dp" 
     android:paddingRight="20dp" 
     android:clickable="false" 
     android:paddingBottom="10dp"> 

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

    </LinearLayout> 

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

這裏是點擊收聽我對活動做的OnCreate()的代碼:

todayCardView = (CardView) findViewById(R.id.card_view_today); 

todayCardView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Log.d(TAG, "Today card tapped"); 
      Intent intent = new Intent(MainActivity.this, TodayActivity.class); 
      startActivity(intent); 
     } 
    }); 

由於requsted,這裏是我的適配器和觀點持有者,對於回收看來,這是在卡片視圖中。

public class SummaryRecyclerViewAdapter extends RecyclerView.Adapter<SummaryRecyclerViewAdapter.ViewHolder> { 

String[] todaysItems; 

public SummaryRecyclerViewAdapter(String[] todaysItems) { 
    this.todaysItems = todaysItems; 
} 

@Override 
public SummaryRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_chip,parent,false); 

    return new SummaryRecyclerViewAdapter.ViewHolder(view); 


} 

@Override 
public void onBindViewHolder(SummaryRecyclerViewAdapter.ViewHolder holder, int position) { 

    holder.textView.setText(todaysItems[position]); 

} 

@Override 
public int getItemCount() { 
    return todaysItems.length; 
} 

public static class ViewHolder extends RecyclerView.ViewHolder{ 

    public TextView textView; 

    public ViewHolder(View v){ 

     super(v); 

     textView = (TextView)v.findViewById(R.id.chip_text_view); 
    } 
} 

}

+2

你應該顯示更多的代碼java。 – UmarZaii

+0

發佈您的ViewHolder –

+0

您可以在線性佈局而不是todayCardView上處理點擊偵聽器。 –

回答

0

添加這對於回收視圖。這可以確保子視圖將無法處理觸摸事件。

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    super.onTouchEvent(event); 
    return false; 
} 

閱讀本文以更好地瞭解觸摸輸入流程。

http://balpha.de/2013/07/android-development-what-i-wish-i-had-known-earlier/

+0

我在哪裏添加? – RJB

+0

recyclerView.setOnTouchListener(新View.OnTouchListener(){ @覆蓋 公共布爾onTouch(查看視圖,MotionEvent motionEvent){ 返回FALSE; } }); – khetanrajesh

+0

這實際上並不奏效。 – RJB

相關問題