Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android RecyclerView notifyItemRemoved()动画错误_Android_Android Fragments_Android Recyclerview - Fatal编程技术网

Android RecyclerView notifyItemRemoved()动画错误

Android RecyclerView notifyItemRemoved()动画错误,android,android-fragments,android-recyclerview,Android,Android Fragments,Android Recyclerview,我在ViewPager中的片段中有一个线性RecyclerView。我有一个onsweed()回调,允许滑动关闭UI。我想首先确认删除,因此我有一个界面,在警报对话框响应后从我的ViewPager(HomeActivity)返回布尔值。如果为true,我希望滑动设置数据集更改的动画,如果为false,则替换项目视图。但是,即使同时调用了notifyItemRemoved()和notifyItemRangeChanged(),动画也不起作用。我在使用我找到的库时遇到困难,所以我现在就这样做 有人能

我在ViewPager中的片段中有一个线性RecyclerView。我有一个
onsweed()
回调,允许滑动关闭UI。我想首先确认删除,因此我有一个界面,在警报对话框响应后从我的ViewPager(HomeActivity)返回
布尔值。如果为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;
    }
}

当您调用
communicator.remove(pos)
?Disco S2时,您的FragmentCommunicator是否返回
true
?如果用户在弹出的警报对话框中单击yes,则返回false。我已经测试了数据,它在true上被删除,但不是在animationSo
communicator上。删除(pos)
显示一个
AlertDialog
对话框,在对话框中确认您是否返回了结果?这可能就是问题所在。相反,请尝试在该对话框中显示AlertDialog,并在其onClick回调中调用notifyItemRemovedcode@Disco好的。这是有道理的。如果为true,是否使用通信器更改数据集?我试试看out@Disco那没用。几乎不知道为什么会这样