[英]Android: Clicklistener on parent not working when clicked on child
I have a cardview, with some widgets in, and a recycler view inside the card view. 我有一个cardview,其中包含一些小部件,并且在card视图内有一个回收站视图。 The cardview is the parent. cardview是父级。
I want it to be that when anywhere in the cardview is clicked, the event to happen. 我希望是单击cardview中的任何位置时都会发生该事件。 However, now only when the top part of the card view is clicked it triggers, but not when I click on the recycler view. 但是,现在只有在单击卡片视图的顶部时,它才会触发,而当我单击回收者视图时,它不会触发。
I tried setting clickable in the recyclerview to false, and it doesn't work. 我尝试将recyclerview中的clickable设置为false,这是行不通的。
Here is my xml. 这是我的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>
And here is the code of the click listener I do on the activity oncreate(): 这是我对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);
}
});
As requsted, here is my adapter and view holder, for the recycler view, which is in the card view. 根据要求,这是我的适配器和视图支架,用于卡片视图中的回收者视图。
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);
}
}
} }
Add this for the recycler view .This makes sure that the child view will not handle the touch event . 将其添加到回收者视图。这可确保子视图不会处理touch事件。
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
return false;
}
Read this article to better understand the touch input flow . 阅读本文可更好地了解触摸输入流程。
http://balpha.de/2013/07/android-development-what-i-wish-i-had-known-earlier/ http://balpha.de/2013/07/android-development-what-i-wish-i-had-known-earlier/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.