2017-02-16 6 views
1

私はRecyclerViewでスワイプを使ってdeleteを実装しようとしています。アイテムをスワイプした後、私はnotifyItemChanged()を呼び出して、onBindViewHolderメソッドのビューの可視性を変更します。RecyclerViewアイテムの高さがsetVisibility()メソッドの呼び出し後に変更される

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);    
    } 
} 

しかし、削除されたアイテムの高さは変更されます。 screenshot

item.xml:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingTop="8dp" 
    android:paddingBottom="8dp" 
    android:paddingRight="8dp" 
    android:paddingLeft="@dimen/activity_horizontal_margin"> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/item_text" 
     android:layout_gravity="center_vertical"/> 
    <Button 
     android:id="@+id/item_button_undo" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:text="@string/button_undo_text" 
     android:layout_gravity="end|center_vertical" 
     style="@style/Base.Widget.AppCompat.Button.Borderless"/> 
</FrameLayout> 

私はアイテムの高さを保存するために何ができますか?

+0

ここで?私はスクリーンショットで何の問題も見ません –

+0

高さを同じにしたい – Vlad

答えて

0

ボタンの幅を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);    
    } 
} 
+0

ボタンの可視性の変更後にholder.textView.setText()を使用すると機能します。 – Vlad

関連する問題