Forked from frapontillo/CustomRecyclerViewAdapter.java
Last active
May 4, 2016 18:58
-
-
Save sreereddymenon/84549228654a0500fe67153a10263171 to your computer and use it in GitHub Desktop.
How to use SwitchCompat inside a RecyclerView item
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// omissis | |
@Override | |
public void onBindViewHolder(final CustomViewHolder holder, final int position) { | |
holder.mSwitch.setOnCheckedChangeListener( | |
new CompoundButton.OnCheckedChangeListener() { | |
@Override | |
public void onCheckedChanged(CompoundButton buttonView, final boolean isChecked) { | |
// TODO: handle your switch toggling logic here | |
} | |
}); | |
// Listen to touch events on the Switch and, when a tap or drag is starting (ACTION_DOWN), | |
// disallow the interception of touch events on the ViewParent (valid until an ACTION_UP). | |
holder.mSwitch.setOnTouchListener(new View.OnTouchListener() { | |
@Override | |
public boolean onTouch(View v, MotionEvent event) { | |
if (event.getAction() == MotionEvent.ACTION_DOWN) { | |
holder.mSwitch.getParent().requestDisallowInterceptTouchEvent(true); | |
} | |
// always return false since we don't care about handling tapping, flinging, etc. | |
return false; | |
} | |
}); | |
} | |
Another solution is | |
Use isPressed (); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
another solution is using IsPressed
viewHolder.switch_on_off_collection.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@OverRide
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {