android – RecyclerView.getChild(index)在列表滚动时显示为空(索引被搞砸了)

我一直在为我的 Android应用程序使用SwipeableRecyclerView为我的recyclelerView启用滑动. RecyclerView包含cardViews列表.

我正在尝试为卡片执行撤消功能,当刷卡左边时会被删除(第一次滑动显示撤消,下一个滑动触发器删除)

我正在尝试以下代码(部分工作我猜)

SwipeableRecyclerViewTouchListener srvTouchListner = new SwipeableRecyclerViewTouchListener(rvTimerList,
            new SwipeableRecyclerViewTouchListener.SwipeListener(){

                @Override
                public boolean canSwipe(int i) {
                    return true;
                }

                @Override
                public void onDismissedBySwipeLeft(RecyclerView recyclerView, int[] ints) {
                    for(int position : ints){
                        View view = recyclerView.getChildAt(position);
                            if (view.getTag(R.string.card_undo) == null) {
                                if(viewStack == null) {
                                    saveToViewStack(position, view);
                                    final ViewGroup viewGroup = (ViewGroup) view.findViewById(R.id.time_card2);
                                    view.setTag(R.string.card_undo, "true");
                                    viewGroup.addView(view.inflate(TimerSummary.this, R.layout.timeslot_card_undo, null));
                                }
                            } else {
                                Log.d(TAG, "Removing Item");
                                deleteTimeSlot(timerInstanceList.get(position));
                                Toast.makeText(TimerSummary.this, "Deleted!", Toast.LENGTH_SHORT).show();
                                timerInstanceList.remove(position);
                                finalSummaryAdapter.notifyItemRemoved(position);
                            }

                    }
                    finalSummaryAdapter.notifyDataSetChanged();
                }
                @Override
                public void onDismissedBySwipeRight(RecyclerView recyclerView, int[] ints) {
                    for (int position:ints){
                        View view = recyclerView.getChildAt(position);
                        if(view.getTag(R.string.card_undo) != null && view.getTag(R.string.card_undo).equals("true")){
                            viewStack = null;
                            recyclerView.setAdapter(finalSummaryAdapter);
                        }
                    }

                }
            });

当项目更多(需要滚动)

View view = recyclerView.getChildAt(position);

返回引用应用程序崩溃的空引用.

我怀疑我使用错误的方法来观察.我应该使用与观众有关的东西,实际上我对于如何从观众获取你想要的视图感到困惑.

如果任何人可以分享任何有助于,那将是伟大的!
如果有人想要,我会很乐意提供更多的信息,

你应该使用findViewHolderForAdapterPosition.
https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html#findViewHolderForAdapterPosition(int)

请记住,如果位置未布局(例如超出或移除),它将返回null.

相关文章
相关标签/搜索