lolio lolio - 1 year ago 227
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:

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

which removes the item from the adapter.


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(;
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
is called, the background stays red, but i want it to be white again.

Hope someone can help me out with this.

Answer Source

Add an additional else clause:

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download