2015-07-22 68 views
6

我正在試圖做一個簡單的例子RecyclerView與動畫,但默認動畫不工作。這是爲什麼?這裏缺少什麼?動畫RecyclerView不工作

主要活動:

public class MainActivity extends Activity { 

List<Song> songsList; 
RecyclerView recyclerView; 
MyAdapter myAdapter; 
RecyclerView.LayoutManager layoutManager; 

Song song1; 
Song song2; 
Song song3; 
Song song4; 
Song song5; 
Song song6; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    songsList = new ArrayList<>(); 

    song1 = new Song(); 
    song1.setName("Label1"); 
    song1.setAuthor("Author1"); 
    song1.setId(1); 

    song2 = new Song(); 
    song2.setName("Label2"); 
    song2.setAuthor("Author2"); 
    song2.setId(2); 

    song3 = new Song(); 
    song3.setName("Label3"); 
    song3.setAuthor("Author3"); 
    song3.setId(3); 

    song4 = new Song(); 
    song4.setName("Label4"); 
    song4.setAuthor("Author4"); 
    song4.setId(4); 

    song5 = new Song(); 
    song5.setName("Label5"); 
    song5.setAuthor("Author5"); 
    song5.setId(5); 

    song6 = new Song(); 
    song6.setName("Label6"); 
    song6.setAuthor("Author6"); 
    song6.setId(6); 

    songsList.add(song1); 
    songsList.add(song2); 
    songsList.add(song3); 

    recyclerView = (RecyclerView) findViewById(R.id.listView); 
    recyclerView.setHasFixedSize(false); 

    layoutManager = new LinearLayoutManager(this); 
    recyclerView.setLayoutManager(layoutManager); 

    myAdapter = new MyAdapter(songsList); 
    recyclerView.setAdapter(myAdapter); 

    RecyclerView.ItemAnimator itemAnimator = new DefaultItemAnimator(); 
    recyclerView.setItemAnimator(itemAnimator); 
} 

public void onClick(View view) { 
    songsList.add(song4); 
    myAdapter.addItem(song4); 
    songsList.add(song5); 
    myAdapter.addItem(song5); 
    songsList.add(song6); 
    myAdapter.addItem(song6); 

    songsList.remove(song1); 
    myAdapter.removeItem(1); 
    songsList.remove(song2); 
    myAdapter.removeItem(2); 

    myAdapter.notifyDataSetChanged(); 
} 
} 

適配器:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { 
private List<Song> songList; 

public MyAdapter(List<Song> songList) { 
    this.songList = songList; 
} 

@Override 
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false); 
    return new ViewHolder(v); 
} 

@Override 
public void onBindViewHolder(ViewHolder viewHolder, int i) { 
    Song song = songList.get(i); 
    viewHolder.song.setText(song.getName()); 
    viewHolder.author.setText(song.getAuthor()); 
} 

@Override 
public int getItemCount() { 
    return songList.size(); 
} 

public void removeItem(int position) { 
    songList.remove(position); 
    notifyItemRemoved(position); 
} 

public void addItem(Song song) { 
    songList.add(song); 
    notifyItemInserted(songList.size()); 
} 

class ViewHolder extends RecyclerView.ViewHolder { 
    private TextView song; 
    private TextView author; 

    public ViewHolder(View itemView) { 
     super(itemView); 
     song = (TextView) itemView.findViewById(R.id.tvSong); 
     author = (TextView) itemView.findViewById(R.id.tvAuthor); 
    } 
} 
} 

類歌曲包括田詮釋的ID,字符串名稱,絃樂作者,getter和setter方法。

回答

7
  1. 不要在您的onClick()使用notifyDataSetChanged(),它將 取消所有動畫在任何時間。
  2. 您的add/removeItem()/包含notifyItemInserted/Removed(),所以請每次添加/刪除單個項目,並以這種方式動畫將正確播放。如果要同時添加/刪除多個項目,請在完成添加/刪除數據集後使用notifyItemRangeInserted/Removed(int startPos, int itemsSize)
+0

非常感謝!有用! – Ghost

4

例如,在您的適配器中覆蓋getItemId(int position)方法,並在構造函數中調用setHasStableIds(true)

+0

完美答案..! –