Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView.getChild(index) shows null when list is scrolled (index gets messed up)

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)

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);
                        }
                    }

                }
            });

when Items are more (needs scrolling)

View view = recyclerView.getChildAt(position);

returns a null reference which causes an app crash.

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,

like image 957
P-RAD Avatar asked Jun 16 '15 07:06

P-RAD


People also ask

Is there an addHeaderView equivalent for RecyclerView?

addHeaderView() but you can achieve this by adding a type to your adapter for header. everything seems to be OK and it should work, but however make the recycler view MATCH_PARENT see if anything changes. also if its possible make the recycler view the root of that layout (its good for performance).

What is LinearLayoutManager in RecyclerView?

RecyclerView provides these built-in layout managers: LinearLayoutManager shows items in a vertical or horizontal scrolling list. GridLayoutManager shows items in a grid. StaggeredGridLayoutManager shows items in a staggered grid.

What does notifyDataSetChanged do in RecyclerView?

notifyDataSetChanged. Notify any registered observers that the data set has changed. There are two different classes of data change events, item changes and structural changes. Item changes are when a single item has its data updated but no positional changes have occurred.


1 Answers

You should use findViewHolderForAdapterPosition. https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html#findViewHolderForAdapterPosition(int)

Keep in mind that it will return null if the position is not laid out (e.g. out of bounds or removed).

like image 195
yigit Avatar answered Oct 16 '22 09:10

yigit