Der*_*rek 14 android android-cardview android-recyclerview
我有一个带有几个TextView的CardView中的RecyclerView.CardView有一个OnClickListener集合,在单击TextViews时会被触发,但在单击RecyclerView时不会触发.
这是CardView的样子:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100"
android:minWidth="100dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/text1"
android:textColor="@color/abc_primary_text_material_light"
android:layout_weight="1"
android:layout_gravity="center_horizontal" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:listSelector="@color/highlighted_text_material_light"
android:layout_weight="98" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/black" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/relativeSummary"
android:orientation="horizontal"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/text2"
android:textAlignment="viewEnd"
android:textColor="@color/abc_secondary_text_material_light"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:gravity="start"
android:singleLine="true"
android:layout_alignParentLeft="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/text3"
android:textColor="@color/abc_primary_text_material_light"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:gravity="end"
android:singleLine="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/text2" />
</RelativeLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud)
我不需要在这个RecyclerView上有一个点击监听器,而且当单击RecyclerView时,实际上只需要触发父视图的click事件(OnLongClick事件也是如此).我还需要RecyclerView来滚动.RecyclerView是一些如何吃点击事件而不是将其传递给父母的?
yww*_*ynm 16
有一个更好的解决方案.也就是说,你的子类CardView:
public class InterceptTouchCardView extends CardView {
public InterceptTouchCardView(Context context) {
super(context);
}
public InterceptTouchCardView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public InterceptTouchCardView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
/**
* Intercept touch event so that inner views cannot receive it.
*
* If a ViewGroup contains a RecyclerView and has an OnTouchListener or something like that,
* touch events will be directly delivered to inner RecyclerView and handled by it. As a result,
* parent ViewGroup won't receive the touch event any longer.
*/
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
小智 11
recyclerView.setLayoutFrozen(true);
Run Code Online (Sandbox Code Playgroud)
在setAdapter for recyclerView之后,setLayoutFrozen为true
就我而言,我有一个带几个按钮的CardView和一个RecyclerView。使用ywwynm和Daryl的解决方案时,问题在于CardView将从其所有子视图(包括按钮)中拦截事件。但是我想要的是CardView仅拦截RecyclerView的触摸事件。我的解决方案如下:
public class UntouchableRecyclerView extends RecyclerView {
public UntouchableRecyclerView(Context context) {
super(context);
}
public UntouchableRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public UntouchableRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onTouchEvent(MotionEvent e) {
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6572 次 |
| 最近记录: |