尝试添加notifyDataSetChanged()
您的删除方法
public void remove(int position) { if (position < 0 || position >= items.size()) { return; } items.remove(position); notifyItemRemoved(position); notifyDataSetChanged(); }
notifyItemRemoved(position)
通知RecyclerView适配器已在特定位置删除适配器中的数据.
notifyDataSetChanged()
通知附加的观察者基础数据已被更改,反映数据集的任何视图都应自行刷新.
UPDATE
尝试添加mRecyclerView.removeViewAt(position);
之前notifyItemRemoved(position);
这不会搞乱动画.
public class MainActivity extends AppCompatActivity { RecyclerView mRecyclerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mRecyclerView.setAdapter(new TestAdapter()); setUpItemTouchHelper(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.menu_item_add_5_items) { ((TestAdapter)mRecyclerView.getAdapter()).addItems(5); } return super.onOptionsItemSelected(item); } private void setUpItemTouchHelper() { ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) { @Override public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { return false; } @Override public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) { int swipedPosition = viewHolder.getAdapterPosition(); TestAdapter adapter = (TestAdapter)mRecyclerView.getAdapter(); adapter.remove(swipedPosition); } }; ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback); itemTouchHelper.attachToRecyclerView(mRecyclerView); } class TestAdapter extends RecyclerView.Adapter { Listitems; int lastInsertedIndex; public TestAdapter() { items = new ArrayList<>(); lastInsertedIndex = 15; // this should give us a couple of screens worth for (int i=1; i<= lastInsertedIndex; i++) { items.add("Item " + i); } } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new TestViewHolder(parent); } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { TestViewHolder viewHolder = (TestViewHolder)holder; String item = items.get(position); viewHolder.titleTextView.setText(item); } @Override public int getItemCount() { return items.size(); } public void addItems(int howMany){ if (howMany > 0) { for (int i = lastInsertedIndex + 1; i <= lastInsertedIndex + howMany; i++) { items.add("Item " + i); notifyItemInserted(items.size() - 1); } lastInsertedIndex = lastInsertedIndex + howMany; } } public void remove(int position) { if (position < 0 || position >= items.size()) { return; } items.remove(position); mRecyclerView.removeViewAt(position); notifyItemRemoved(position); } } static class TestViewHolder extends RecyclerView.ViewHolder { TextView titleTextView; public TestViewHolder(ViewGroup parent) { super(LayoutInflater.from(parent.getContext()).inflate(R.layout.row_view, parent, false)); titleTextView = (TextView) itemView.findViewById(R.id.title_text_view); } } }