简体   繁体   中英

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. The cardview is the parent.

I want it to be that when anywhere in the cardview is clicked, the event to happen. 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.

Here is my 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():

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 .

@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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM