Created
August 2, 2017 15:35
-
-
Save Marchuck/be920955d0b91b4e0015bf37cf84ae74 to your computer and use it in GitHub Desktop.
reactive scrollview (able to detect scroll gesture)
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
package evalu.com.evalu.utils.rx; | |
import android.content.Context; | |
import android.os.Build; | |
import android.support.annotation.RequiresApi; | |
import android.util.AttributeSet; | |
import android.view.MotionEvent; | |
import android.widget.ScrollView; | |
import io.reactivex.subjects.PublishSubject; | |
import io.reactivex.subjects.Subject; | |
/** | |
* Created by Lukasz Marczak on 10.03.17. | |
*/ | |
public class ReactiveScrollView extends ScrollView { | |
public Subject<Boolean> scrolledUpSubject = PublishSubject.create(); | |
public float threshold = 20; | |
public ReactiveScrollView(Context context) { | |
super(context); | |
} | |
public ReactiveScrollView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public ReactiveScrollView(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
} | |
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) | |
public ReactiveScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { | |
super(context, attrs, defStyleAttr, defStyleRes); | |
} | |
private float mTouchPosition; | |
private float mReleasePosition; | |
@Override | |
public boolean onTouchEvent(MotionEvent event) { | |
if (event.getAction() == MotionEvent.ACTION_DOWN) { | |
mTouchPosition = event.getY(); | |
} | |
if (event.getAction() == MotionEvent.ACTION_UP) { | |
mReleasePosition = event.getY(); | |
float diff = mTouchPosition - mReleasePosition; | |
if (diff > threshold) { | |
// user scroll down | |
scrolledUpSubject.onNext(false); | |
} else if (diff < -threshold) { | |
scrolledUpSubject.onNext(true); | |
//user scroll up | |
} | |
} | |
return super.onTouchEvent(event); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment