Recycler View Item Gone无法正常工作

时间:2018-02-08 14:01:22

标签: android android-recyclerview android-viewholder

当用户切换切换时,我正试图在回收器中隐藏部分视图。 但是当我切换开关时,有时它会以一种奇怪的方式表现并且只隐藏我的部分视图。它发生在每4次切换点击(show-hide-show-hide),我无法解决这个问题。有什么建议我做错了什么? 有properly displayed viewawkwardly hidden view的屏幕截图。如您所见,在第二个屏幕截图中,具有2个图像视图的搜索栏未完全隐藏。 更新:问题解决了。问题在于包含标签。它有点错误,项目文件中的相同布局工作正常。

以下是隐藏逻辑的代码:

itemView.light_switch.setOnCheckedChangeListener { _, isChecked ->
            itemView.brightness.visibility = if (isChecked) View.VISIBLE else View.GONE
            lightCallback(lamp, isChecked)
        }

这是我的布局(回收商项目):

 <android.support.constraint.ConstraintLayout 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"
    android:paddingBottom="8dp"
    android:background="#1a1a1a">


    <ImageView
        android:id="@+id/light_icon_image_view"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/prod_foot" />

    <TextView
        android:id="@+id/light_name_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginStart="8dp"
        android:fontFamily="sans-serif-medium"
        android:textColor="#ffffff"
        android:textSize="16sp"
        android:textStyle="normal"
        app:layout_constraintBottom_toBottomOf="@+id/light_icon_image_view"
        app:layout_constraintStart_toEndOf="@+id/light_icon_image_view"
        app:layout_constraintTop_toTopOf="@+id/light_icon_image_view"
        tools:text="Floodlight 24 368 (01)" />

    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif"
        android:text="No group"
        android:textColor="#7fffffff"
        android:textSize="14sp"
        android:textStyle="normal"
        app:layout_constraintStart_toStartOf="@+id/light_name_text_view"
        app:layout_constraintTop_toBottomOf="@+id/light_name_text_view" />

    <android.support.v7.widget.SwitchCompat
        android:id="@+id/light_switch"
        style="@style/SwitchCompatStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="@+id/light_icon_image_view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/light_icon_image_view"
        app:thumbTint="@android:color/white" />


    <include
        android:id="@+id/brightness"
        layout="@layout/brightness_seekbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:visibility="visible"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/light_icon_image_view">

    </include>


    <ImageView
        android:id="@+id/light_gradient_circle"
        android:layout_width="22dp"
        android:layout_height="22dp"
        android:layout_marginTop="40dp"
        app:layout_constraintStart_toStartOf="@+id/light_icon_image_view"
        app:layout_constraintTop_toTopOf="@+id/light_icon_image_view"
        app:srcCompat="@drawable/badge_color_wheel_active_block" />


</android.support.constraint.ConstraintLayout>

下面提供了包含布局(有2张图片的搜索栏):

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout 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:background="#1a1a1a"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="15dp"
        android:layout_height="15dp"
        android:layout_marginStart="16dp"
        app:layout_constraintBottom_toBottomOf="@+id/seekbar"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/seekbar"
        app:srcCompat="@drawable/ic_dim_min" />

    <SeekBar
        android:id="@+id/seekbar"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginEnd="11dp"
        android:layout_marginStart="11dp"
        android:layout_marginTop="8dp"
        android:max="255"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/imageView2"
        app:layout_constraintStart_toEndOf="@+id/imageView"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        app:layout_constraintBottom_toBottomOf="@+id/seekbar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/seekbar"
        app:srcCompat="@drawable/ic_dim_max" />
</android.support.constraint.ConstraintLayout>

更新:适配器完整代码

class IndividualLightsAdapter(private val context: Context,
                              private val data: MutableList<Lamp>,
                              private val lightCallback: (Lamp, Boolean) -> Unit,
                              private val lightBrightnessCallback: (lamp: Lamp, brightness: Int) -> Unit)
    : RecyclerView.Adapter<IndividualLightsAdapter.LightViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LightViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        val view = inflater.inflate(R.layout.light_item_new, parent, false)
        return LightViewHolder(view)
    }

    override fun getItemCount(): Int = data.size

    override fun onBindViewHolder(holder: LightViewHolder, position: Int) {
        holder.bind(position)
    }

    inner class LightViewHolder(view: View) : RecyclerView.ViewHolder(view) {

        fun bind(position: Int) {
            val lamp = data[position]
            itemView.light_icon_image_view.setImageResource(lamp.imageId)
            itemView.light_name_text_view.text = lamp.customName

            itemView.light_gradient_circle.visibility = if (lamp is SmartLamp) View.VISIBLE else View.INVISIBLE

            itemView.light_switch.isChecked = lamp.isTurnedOn

            itemView.light_switch.setOnCheckedChangeListener { _, isChecked ->
                itemView.brightness.visibility = if (isChecked) View.VISIBLE else View.GONE
                lightCallback(lamp, isChecked)
            }

            setSeekBar()

            itemView.seekbar.progress = lamp.brightness
            itemView.seekbar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
                override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                    lightBrightnessCallback(lamp, progress)
                }

                override fun onStartTrackingTouch(seekBar: SeekBar) {
                }

                override fun onStopTrackingTouch(seekBar: SeekBar) {
                }
            })

        }


        private fun setSeekBar() {

            val gradient = GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,
                    intArrayOf(ContextCompat.getColor(context, R.color.grad_start), (ContextCompat.getColor(context, R.color.grad_end))))
            gradient.cornerRadius = context.resources.getDimension(R.dimen.corner_radius_outer)

            val progressLayer = ClipDrawable(gradient, Gravity.START, ClipDrawable.HORIZONTAL)

            val background = GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP, intArrayOf(Color.BLACK, Color.BLACK))
            background.cornerRadius = context.resources.getDimension(R.dimen.corner_radius_inner)

            // TODO Calculate padding dynamically

            val backgroundLayer = InsetDrawable(background,
                    context.resources.getDimension(R.dimen.padding_inset_left).toInt(),
                    context.resources.getDimension(R.dimen.padding_inset_top).toInt(),
                    context.resources.getDimension(R.dimen.padding_inset_right).toInt(),
                    context.resources.getDimension(R.dimen.padding_inset_bottom).toInt())


            itemView.seekbar.thumb.setTint((ContextCompat.getColor(context, R.color.color_thumb)))

            itemView.seekbar.progressDrawable = LayerDrawable(arrayOf<Drawable>(backgroundLayer, progressLayer))

        }
    }

}

1 个答案:

答案 0 :(得分:1)

您不应该隐藏作为recyclerView项目的视图。

由于其回收机制,它无法正常工作。

您只需要删除/添加项目即可以执行相同的效果。

您应该在RecyclerView adapter

上使用以下方法
   private void deleteItem(int position) {
        if (position != RecyclerView.NO_POSITION) {
            myList.remove(position);
            notifyItemRemoved(position);
        }
    }

    private void addItem(int position, MyModel model) {
        if (position != RecyclerView.NO_POSITION) {
            myList.add(position, model);
            notifyItemInserted(position);
        }
    }