如何修复recyclerview的viewholder布局中的可见性更改

时间:2017-07-24 13:08:54

标签: android android-constraintlayout

我有一个recyclerview,使用以下xml作为每个视图的列表项

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardElevation="2dp">
    <android.support.constraint.ConstraintLayout android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView android:id="@+id/item_question_name"
            style="?android:attr/textAppearanceLarge"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/app_margin"
            app:layout_constraintBottom_toTopOf="@+id/item_question_question"
            app:layout_constraintEnd_toStartOf="@+id/item_question_needs_sync"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="This is the name" />
        <android.support.v7.widget.AppCompatImageView android:id="@+id/item_question_needs_sync"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="@+id/item_question_name"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@+id/item_question_name"
            app:srcCompat="@drawable/ic_sync" />
        <TextView android:id="@+id/item_question_question"
            style="?android:attr/textAppearanceMedium"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/app_margin"
            app:layout_constraintBottom_toTopOf="@+id/item_question_type"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="@+id/item_question_name"
            app:layout_constraintTop_toBottomOf="@+id/item_question_name"
            tools:text="this is the question" />
        <TextView android:id="@+id/item_question_type"
            style="?android:attr/textAppearanceMedium"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/app_margin"
            android:layout_marginTop="@dimen/app_margin"
            app:layout_constraintBottom_toTopOf="@+id/item_question_separator"
            app:layout_constraintEnd_toStartOf="@+id/item_question_info"
            app:layout_constraintHorizontal_weight="5"
            app:layout_constraintStart_toStartOf="@+id/item_question_question"
            app:layout_constraintTop_toBottomOf="@+id/item_question_question"
            tools:text="Four faces which are awesome" />
        <TextView android:id="@+id/item_question_info"
            style="?android:attr/textAppearanceSmall"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="@+id/item_question_type"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_weight="3"
            app:layout_constraintStart_toEndOf="@+id/item_question_type"
            app:layout_constraintTop_toTopOf="@+id/item_question_type"
            tools:text="answered ALL the questions" />
        <View android:id="@+id/item_question_separator"
            android:layout_width="0dp"
            android:layout_height="2dp"
            android:background="@color/colorAccent"
            app:layout_constraintBottom_toTopOf="@+id/item_question_buttons"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/item_question_info" />
        <android.support.constraint.ConstraintLayout android:id="@+id/item_question_buttons"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/item_question_separator">
            <Button android:id="@+id/item_question_delete"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="@string/item_question_button_delete"
                app:layout_constraintHorizontal_weight="1"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@+id/item_question_edit"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
            <Button android:id="@+id/item_question_edit"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="@string/item_question_button_edit"
                app:layout_constraintHorizontal_weight="1"
                app:layout_constraintBottom_toBottomOf="@+id/item_question_delete"
                app:layout_constraintEnd_toStartOf="@+id/item_question_view"
                app:layout_constraintStart_toEndOf="@+id/item_question_delete"
                app:layout_constraintTop_toTopOf="@+id/item_question_delete" />
            <Button android:id="@+id/item_question_view"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="@string/item_question_button_view"
                app:layout_constraintHorizontal_weight="1"
                app:layout_constraintBottom_toBottomOf="@+id/item_question_delete"
                app:layout_constraintEnd_toStartOf="@+id/item_question_start"
                app:layout_constraintStart_toEndOf="@+id/item_question_edit"
                app:layout_constraintTop_toTopOf="@+id/item_question_delete" />
            <Button android:id="@+id/item_question_start"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="@string/item_question_button_start"
                app:layout_constraintHorizontal_weight="1"
                app:layout_constraintBottom_toBottomOf="@+id/item_question_delete"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@+id/item_question_view"
                app:layout_constraintTop_toTopOf="@+id/item_question_delete" />
        </android.support.constraint.ConstraintLayout>
    </android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>

我的问题是,当用户点击视图时,我让查看者切换是否会显示@ + id / item_question_buttons:

void bind_view(Info info, boolean selected) {
   //load the data from info
    .....
   // _buttons is a View object where I stored the constraint layout with the buttons

   if (selected) {
       _buttons.setVisibility(VISIBLE);
   } else {
       _buttons.setVisibility(GONE);
   }
}

我的问题是,每隔一次(即第2个第4个第6个等)用户切换_buttons的可见性查看视图扩展到正常大小的三倍(这与按钮可以具有的最大高度相同)。

我通过使用android解决了它:maxLines =“1”但是我在徘徊,为什么会发生这种情况?并且有更好的方法来解决这个问题吗?

提前感谢您提供的任何帮助

0 个答案:

没有答案