【问题标题】:ViewHolder itemView is smaller than expectedViewHolder itemView 小于预期
【发布时间】:2017-03-15 04:28:01
【问题描述】:

ViewHolder 基于此布局进行膨胀:

将监听器添加到整个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>

【问题讨论】:

  • 你能添加你的适配器和项目布局吗?
  • 添加了项目布局。
  • 看到你的布局,我认为你应该重新定义它。虽然,您可以拥有一个高度为 33dp 的 RecyclerView,但强烈建议您替换为其他视图。
  • 顺便说一句,我在您的布局中发现了其他错误。其中之一是您如何在 RelativeLayout developer.android.com/guide/topics/ui/layout/relative.html 中定位视图。另一个是,如果您使用 layout_marginStart,您的应用程序将无法在 RTL 布局中很好地显示。您应该改用 layout_marginLeft。如果您对它们之间的差异有任何疑问,请阅读此链接stackoverflow.com/questions/14904273/…
  • 明白,但你还没有回答我的问题。

标签: android android-recyclerview


【解决方案1】:

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

建议你在RecyclerView的xml中都添加这个属性

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 中而不是

&lt;android.support.v7.widget.RecyclerView 使用&lt;your.file.path.ScrollThroughRecyclerView

但是这两种解决方案都会使 RecyclerView 不可点击,当然也不可滚动。

【讨论】:

  • 有一段时间 ~3-5 秒 recycleview 无法滚动。你知道为什么吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多