lolio lolio - 4 months ago 31
Android Question

ItemTouchHelper callback

I have problems understanding which callback method of ItemTouchHelper is called when i swipe a carditem, but i dont finish the swipe and instead turn it back to the normal state.

What i have currently:

@Override
public void onSwiped(final RecyclerView.ViewHolder viewHolder, int direction) {
mCardItemAdapter.deleteCard(viewHolder.getAdapterPosition(), mRecyclerView);
}


which removes the item from the adapter.

And:

@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
Log.d("dx =",""+dX);
// Can be modified
if(dX < -300) {
View v = viewHolder.itemView;
RelativeLayout mLayout = (RelativeLayout) v.findViewById(R.id.card_item_layout_relative_layout);
mLayout.setBackgroundResource(R.color.red);
}
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}


What i want to achieve ? When i swipe, the background turns red, but when i release the swipe before
onSwiped
is called, the background stays red, but i want it to be white again.

Hope someone can help me out with this.

Answer

Add an additional else clause:

if(dx < -300){...} else { mLayout.setBackgroundResource(R.color.white); }

Comments