简体   繁体   English

Android:单击子级时,父级的Clicklistener不起作用

[英]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.

相关问题 Android Recyclerview 的内容 ClickListener 无法正常工作 - Android Recyclerview's Content ClickListener not working Properly 尝试在Android中添加ClickListener时获取NullPointerException。为什么? - Getting NullPointerException when trying to add ClickListener in Android.. why? 接口Java / Android无法从子级到父级片段工作 - Interface java/Android not working from child to parent fragment 按钮ClickListener在LibGDX游戏中不起作用 - Button ClickListener is not working in LibGDX game 单击时onItemClick不起作用 - onItemClick not working when clicked Android中的Searchview正常运行,但单击时显示错误结果。 - Searchview in Android working but displaying incorrect result when clicked on. 单击按钮时出现崩溃错误; 指定的孩子已经有一个父母。 您必须先在孩子的父母上调用removeView() - I Have Crash Error when I Clicked Button; The specified child already has a parent. You must call removeView() on the child's parent first 当子组件中添加了其他MouseListener时,父组件的MouseListener无法在子组件内部运行 - MouseListener of parent component is not working inside child component when other MouseListener added in child Java在父母和孩子这不能按预期工作 - Java this in parent and child not working as expected Java Android-将父级转换为子级 - Java Android - Casting Parent to Child to Child
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM