RecyclerView.getChild(指数)显示了空当列表滚动(指数得到弄糟) [英] RecyclerView.getChild(index) shows null when list is scrolled (index gets messed up)
问题描述
我一直在使用SwipeableRecyclerView我启用挥笔为我recyclerView Android应用程序。 RecyclerView包含cardViews的列表。
I've been using SwipeableRecyclerView for my android application for enabling swipes for my recyclerView. RecyclerView contains a list of cardViews.
我试图实现对卡撤消功能时,向左滑动,这将被删除(第一次刷卡显示撤销,旁边刷卡触发删除)
I was trying to implement undo functionality for cards which will get deleted when swipe to left (first swipe shows undo, next swipe triggers delete)
我想下面的code(部分工作我猜的)
I am trying the following code (partially working I guess)
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);
返回这会导致程序崩溃的空引用。
returns a null reference which causes an app crash.
我怀疑,我使用采取观点错误的方法。我应该使用与viewholder有关的东西,我其实那种困惑如何让你从viewholder所需的视图。
I doubt that I am using the wrong method for taking view. I should be using something related with viewholder, I am actually kind of confused about how to get the view which you want from viewholder.
如果任何人都可以共享任何有助于,,那将是伟大的!
我会很高兴,如果有人想它来提供更多的相关信息,
If anybody can share anything that helps,, that'll be great! I ll be happy to provide any more infos if somebody wants it,
推荐答案
您应该使用 findViewHolderForAdapterPosition
。
<一href=\"https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html#findViewHolderForAdapterPosition(int)\">https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html#findViewHolderForAdapterPosition(int)
请记住,这将返回null如果位置不布置(例如出界或删除)。
Keep in mind that it will return null if the position is not laid out (e.g. out of bounds or removed).
这篇关于RecyclerView.getChild(指数)显示了空当列表滚动(指数得到弄糟)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!