2017-04-06 1 views
1

アダプタでnotifyItemRemoved(position)を呼び出すときに問題が発生しています。指定した位置が最後の要素ではなく、アニメーションが削除されたときは、最後の要素が常に最初に「点滅/点滅」してから、上のスクリーンショット(リンク)に示すように上に移動します。Android RecyclerViewアダプタnotifyItemRemovedアニメーション時にリストの最後の要素が点滅するようにする

MainActivity.java

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // ... 

     RecylerView recyclerView = (RecyclerView) findViewById(R.id.list_view); 
     recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false); 
     recyclerView.setItemAnimator(new DefaultItemAnimator()); 
     recyclerView.setAdapter(new ItemAdapter()); 
    } 

ItemAdapter.java

public void onBindViewHolder(final ViewHolder holder, int position) { 
     holder.mButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // mItems has a list of hard-coded elements 
       mItems.remove(holder.getAdapterPosition()); 
       notifyItemRemoved(holder.getAdapterPosition()); 
      } 
     }); 
    } 

content_main.xml

:ここ

は、私のコードの小さなスニペットです210

<?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:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.myapp.myapplication.MainActivity" 
    tools:showIn="@layout/activity_main"> 

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

list_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <Button 
     android:id="@+id/btn_click" 
     android:text="Delete" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 

は、誰もがこれを経験してきたし、どのようにこの問題を解決するために?

ありがとうございます。場合

.gif of the issue

+0

レイアウトファイルまたは「RecyclerView」を投稿できますか? – azizbekian

+0

@azizbekian申し訳ありません、私は忙しかったです。はい、XMLが追加されました。私はactivity_main.xmlを追加しませんでした。これはAndroid Studioで新しいプロジェクトを作成するときのデフォルトのXMLだからです。 – SuKhai

答えて

0

にアニメーションを使用しない場合、あなたは

((DefaultItemAnimator) recyclerViewObject.getItemAnimator()).setSupportsChangeAnimations(false); 
+0

あなたのオファーに感謝します。 :) – SuKhai

+0

私が与えた解決策は、アイテムの追加と削除のアニメーションだけを削除します。 recyclerviewの項目を追加したり削除したりするためのデフォルトのアニメーションは、fadein-fadeoutです。その点滅が見られます。あなたはそれについてもっと知ることができます。 http://frogermcs.github.io/recyclerview-animations-androiddevsum​​mit-write-up/ – CodeCameo

+0

私はそれを試しましたが、私はまだ同じ結果を得ています...方法による素晴らしい参照リンク – SuKhai

0

この行を追加するには、コードにこれを追加することができます。

recyclerView.getItemAnimator().setChangeDuration(0); 
+0

こんにちは@私はそれを試みましたが、それはうまくいかなかった。問題はまだそこにある:( – SuKhai

関連する問題