![](/img/trans.png)
[英]Android Recyclerview's Content ClickListener not working Properly
[英]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.