RecyclerView notifyItemRemoved()动画错误

时间:2016-01-04 18:05:21

标签: android android-fragments android-recyclerview

我在ViewPager的片段中有一个线性的RecyclerView。我有一个onSwiped()回调,允许滑动到关闭UI。我想首先确认删除,所以我有一个界面,在警报对话框响应后从我的ViewPager(HomeActivity)返回boolean。如果为true,我希望滑动为数据集更改设置动画,如果为false则替换项视图。但即使同时调用notifyItemRemoved()notifyItemRangeChanged(),动画也不起作用。我在使用我找到的库时遇到了麻烦,所以我现在就这样做了。

有人可以帮我找出原因吗?

ListFragment.class

public class ListFragment extends Fragment {

    private RecyclerView recyclerView;
    private SongRecyclerViewAdapter recyclerViewAdapter;
    private FragmentCommunicator communicator;

    public ListFragment() {
        // Required empty constructor
    }

    public static ListFragment newInstance() {
        return new ListFragment();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof FragmentCommunicator) {
            communicator = (FragmentCommunicator) context;
        } else {
        throw new RuntimeException(context.toString()
                + " must implement FragmentCommunicator");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_song_list, container, false);

        // Set the adapter
        if (view instanceof RecyclerView) {
            Context context = view.getContext();
            recyclerView = (RecyclerView) view;
            recyclerView.setLayoutManager(new LinearLayoutManager(context));
            recyclerViewAdapter = new SongRecyclerViewAdapter(communicator.getList(), communicator);
            recyclerView.setAdapter(recyclerViewAdapter);
        }

        SimpleCallback simpleItemCallBack = 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 direction) {
                int pos = viewHolder.getAdapterPosition();
                // this is my problem
                if (communicator.remove(pos)) { // goes to HomeActivity.class
                    // this use to work until I wrapped it in the above condition
                    recyclerViewAdapter.notifyItemRemoved(pos);
                    recyclerViewAdapter.notifyItemRangeChanged(pos, recyclerViewAdapter.getItemCount());
                }
            }
        };

        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemCallBack);
        itemTouchHelper.attachToRecyclerView(recyclerView);

        return view;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        // kill
        communicator = null;
        recyclerViewAdapter = null;
    }
}

0 个答案:

没有答案