调用setVisibility()方法后,RecyclerView项的高度会发生变化

时间:2017-02-16 12:45:16

标签: android android-recyclerview

我尝试在RecyclerView中使用滑动实现删除。刷完项后,我调用notifyItemChanged()并在onBindViewHolder方法中更改视图的可见性。

v = QVariant("hello");          // The variant now contains a QByteArray
v = QVariant(tr("hello"));      // The variant now contains a QString

但是在删除项目的高度变化之后。 screenshot

item.xml:

public void onBindViewHolder(CustomViewHolder holder, int position) {
    if (isPendingRemoval(position)) {
        holder.textView.setVisibility(View.INVISIBLE);            
        holder.undoButton.setVisibility(View.VISIBLE);

        final int adapterPosition = holder.getAdapterPosition();

        holder.undoButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Runnable pendingRemovalRunnable = mPendingRunnables.get(adapterPosition);
                mHandler.removeCallbacks(pendingRemovalRunnable);
                mPendingRunnables.remove(adapterPosition);
                notifyItemChanged(adapterPosition);
            }
        });

    } else {
        holder.textView.setVisibility(View.VISIBLE);            
        holder.undoButton.setVisibility(View.INVISIBLE);
        holder.textView.setText(someText);            
    }
}

如何保存物品的高度?

1 个答案:

答案 0 :(得分:0)

你可以尝试将Button的宽度设置为match_parent,只是改变它的可见性而不是TextView的一个(显然给出了项目的高度),这样项目应保持高度和宽度,该按钮应该隐藏在TextView上可见

public void onBindViewHolder(CustomViewHolder holder, int position) {
    if (isPendingRemoval(position)) {
        // holder.textView.setVisibility(View.INVISIBLE);            
        holder.undoButton.setVisibility(View.VISIBLE);

        final int adapterPosition = holder.getAdapterPosition();

        holder.undoButton.setOnClickListener(new View.OnClickListener() {
            @Override
             public void onClick(View v) {
                Runnable pendingRemovalRunnable = mPendingRunnables.get(adapterPosition);
                mHandler.removeCallbacks(pendingRemovalRunnable);
                mPendingRunnables.remove(adapterPosition);
                notifyItemChanged(adapterPosition);
            }
        });

    } else {
        // holder.textView.setVisibility(View.VISIBLE);            
        holder.undoButton.setVisibility(View.INVISIBLE);
        holder.textView.setText(someText);            
    }
}