2016-04-04 4 views
4

RecyclerView.Adapterで次のコードを使用してアイテムを削除します。現在のスクロール位置が一番上にあれば、すべて正常に動作します。RecylcerViewが削除されたアイテムの上にスクロール

スクロール位置がリストの中央にある場合、リストは自動的にビューのトップにスクロールします。

この現象を回避するにはどうすればよいですか?あなたは yourRecylerView.scrollToPosition(position);

を使用してリサイクルビューの

public class MyAdapter extends RecyclerView.Adapter { 

... 

    public void removeItem(int pos){ 
     mDataset.remove(pos); //List with Items 
     notifyItemRemoved(pos); 
     notifyItemRangeChanged(pos,mDataset.size()); 
    } 
} 

<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="match_parent"> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 


      <android.support.v7.widget.RecyclerView 
       android:id="@+id/my_recycler_view" 
       android:scrollbars="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" /> 

    </LinearLayout> 
... 
    </FrameLayout> 
+0

'notifyItemRangeChanged(POS、mDataset.size())の背後にあるresonsどのようなものがあり;'?それはあなたのような縫い目は1つの項目を削除しているだけです – Selvin

+0

これらの2行は十分以上です mDataset.remove(pos); //アイテムを含むリスト notifyItemRemoved(pos); –

+0

は、 'notifyItemRemoved(pos)'のみを使用して試しました。同じBeviour – Marcel

答えて

3

は私のためにそれを解決し

recyclerView.setHasFixedSize(true); 
+1

これはゲットー修正のように思えますが、それは私のために働きます。 –

0

ちょうどこの位置がリストに存在するかどうかを確認してください。

-1

これを使用して:

mDataset.remove(pos); 
adapter.notifyItemRemoved(pos); 
adapter.notifyDataSetChanged(); 
1

あなたは変更されRecyclerViewに関連付けられたデータセットを通知した後に除去位置までスクロールする必要があります。

はい、mDatasetからアイテムを削除した後にnotifyDataSetChanged()に電話するだけです。だからあなたのアダプターはこのように見えます。

public class MyAdapter extends RecyclerView.Adapter { 

    //... Code goes here 

    public void removeItem(int pos){ 
     mDataset.remove(pos); 
     notifyDataSetChanged(); 
     recyclerView.scrollToPosition(pos - 1); 
    } 
} 
+0

代わりにnotifyItemRemovedまたはnotifyItemRangeRemovedを使用して、実際に変更された項目を更新するだけで済みます。https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapterを参照してください。 html – hcpl

0

この

  1. mDataset.remove(POS)を試してみてください。
  2. adapter.notifyItemRemoved(pos);
  3. adapter.notifyDataSetChanged();

これは動作するはずです。次の行を追加

0

は、このコードを試してみてください。

public void removeItem(int position){ 

    mdata.remove(position); 
    notifyItemRangeChanged(position,mdata.size()); 
} 
関連する問題