繁体   English   中英

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

[英]Android: Clicklistener on parent not working when clicked on child

我有一个cardview,其中包含一些小部件,并且在card视图内有一个回收站视图。 cardview是父级。

我希望是单击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);
        }
    });

根据要求,这是我的适配器和视图支架,用于卡片视图中的回收者视图。

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);
    }
}

}

将其添加到回收者视图。这可确保子视图不会处理touch事件。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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