I'm using RecyclerView with CardView to display a list of Cards. Every card has 2 linearLayout (1 for the header, and 1 for the expand, the second one became visible only when the card is pressed).
When I modify the priority of a card, the card is moved in a new position of the list (I change it's position in the "array" of item, and then call adapter.notifyItemMoved(oldPosition,newPosition) ) and it works, I also called RecyclerView.scrollToPosition(newPosition) to show the new position in the list.
Now the problem, I want to expand the card after I moved it in the new position and to do so I need the linearLayout inside the card, they are in the ViewHolder that hold the card. I'm trying to use RecyclerView.findViewHolderAtPosition(newPosition), but it returns the ViewHolder only if the card is visible where I was before the "scrollTo", if the card is moved out of these "visible cards" it returns always null. (I'm also using mRecyclerView.setItemViewCacheSize(myDataset.size()) to solve other problems so I was expecting the correct ViewHolder even when it's not in vision)
public void moveTask(int oldPosition, int position, Task task) {
if (oldPosition < position) {
myDataset.add(position, task);
myDataset.remove(oldPosition);
} else {
myDataset.remove(oldPosition);
myDataset.add(position, task);
}
mAdapter.notifyItemMoved(oldPosition, position);
mAdapter.notifyDataSetChanged();
mRecyclerView.smoothScrollToPosition(position);
MyViewHolder holder =(MyViewHolder)mRecyclerView.findViewHolderForPosition(position);
mAdapter.expandCard(holder);
}
mAdapter.notifyDataSetChanged() shouldn't be called (notifyItemMoved should be enough) but sometimes it doesn't work without it.
expandCard just set the visibility of the linearLayout "expandable" to Visible and the other linearLayout "expandable" already visible to Gone.
What can I do to make it works? I'm doing it completely wrong? I hope I have explained the problem in a comprehensible way. If i need to explain better how I'm doing something I'm always here, thanks for the help.