ViewHolder itemView小于预期

时间:2016-11-01 23:22:10

标签: android android-recyclerview

ViewHolder基于此布局而膨胀:

enter image description here

将监听器添加到整个ViewHolder,或者精确地添加到itemView

userSettingHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

但只有imageView的第一项可以激活监听器。为什么不是整个项目,而不是最后两个'sub' - recycleview

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="66dp">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="66dp"
        android:layout_height="66dp"
        android:padding="0dp" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rw1"
        android:layout_marginStart="66dp"
        android:layout_marginTop="0dp"
        android:layout_marginBottom="33dp"
        android:layout_height="33dp"
        android:layout_width="match_parent"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rw2"
        android:layout_marginStart="66dp"
        android:layout_marginTop="33dp"
        android:layout_marginBottom="0dp"
        android:layout_height="33dp"
        android:layout_width="match_parent"/>

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

当您点击 ImageView 时,触摸会传递给 itemView 的父级。但是当您点击 RecyclerView 时消耗触摸。

我建议您将此属性添加到xml

中的 RecyclerView
android:clickable="false"

如果这不起作用,那么你必须继承 RecyclerView 并覆盖onInterceptTouchEvent方法。

public class ScrollThroughRecyclerView extends RecyclerView {
public ScrollThroughRecyclerView(Context context) {
    super(context);
}

public ScrollThroughRecyclerView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public ScrollThroughRecyclerView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    return false;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    return false;
}
}

然后在你的xml而不是

<android.support.v7.widget.RecyclerView使用<your.file.path.ScrollThroughRecyclerView

但这两种解决方案都会使 RecyclerView 无法点击,而且不可滚动。

相关问题