【问题标题】:RecyclerView Two Types item height wrap contentRecyclerView 两种类型的项目高度换行内容
【发布时间】:2018-06-24 09:38:25
【问题描述】:

我在adapter 中使用了两个ViewTypes 来显示不同的布局:

        case 0:
            return new Holder(LayoutInflater.from(parent.getContext()).inflate(R.layout.faq_item, parent, false));
        case 2:
            return new HolderAnswer(LayoutInflater.from(parent.getContext()).inflate(R.layout.faq_item_answer, parent, false));
        default:
            return null;
    }

默认情况下,每秒钟 layout 都有不可见的(GONE) TextView 在第一个项目点击时变成 Visible

如果我将第一个项目设置为固定height,它可以正常工作,但项目有时可能包含看不到的长文本:

<ConstraintLayout
layout_height="@dimen/..."
...>
....
</ConstraintLayout>

如果我将height 更改为wrap_content,我可以看到意外的行为:第一个和第二个元素的height 等于screen height,但在滑动到列表末尾和开头几次之后,高度适合预期措施。

我的猜测,这是因为使用了两种视图类型或错误的对齐方式。 setHasFixedSize()这里是布局:

问题.xml

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container_question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#CCEFF5">

<View
    android:id="@+id/indicator"
    android:layout_width="3dp"
    android:layout_height="20dp"
    android:background="#ff008ca5"
    android:visibility="invisible"
    app:layout_constraintBottom_toBottomOf="@+id/question"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="@+id/question" />

<com.hastee.pay.ui.view.Text
    android:id="@+id/question"
    style="@style/black_regular_15"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginStart="20dp"
    android:layout_marginTop="16dp"
    app:infont="bold"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/plus"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0" />

<ImageView
    android:id="@+id/plus"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="20dp"
    app:layout_constraintBottom_toBottomOf="@+id/question"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="@+id/question"
    app:srcCompat="@drawable/ic_plus_small" />


</android.support.constraint.ConstraintLayout>

answer.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#CCEFF5">
<com.hastee.pay.ui.view.Text
    android:id="@+id/answer"
    style="@style/black_regular_13"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="16dp"
    android:layout_marginLeft="32dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="16dp"
    android:alpha="0.7" />

<View
    android:id="@+id/divider"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_gravity="bottom"
    android:layout_marginLeft="20dp"
    android:layout_marginStart="20dp"
    android:background="@color/divider" />
</FrameLayout>

有什么想法吗?

【问题讨论】:

    标签: android android-recyclerview row-height expandablerecyclerview


    【解决方案1】:

    只需从ConstraintLayout 切换到RelativeLayout 即可解决。看起来第一个复杂列表的工作不正确。

    【讨论】:

      猜你喜欢
      • 2015-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多