Last active
May 17, 2021 06:58
-
-
Save chittaranjan-khuntia/42d5429ac37b7aea3cb22fb51c8729b4 to your computer and use it in GitHub Desktop.
How to disable or enable Scrolling in android ScrollView
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
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.view.KeyEvent; | |
import android.view.MotionEvent; | |
import android.widget.ScrollView; | |
public class LockableScrollView extends ScrollView { | |
// true if we can scroll (not locked) | |
// false if we cannot scroll (locked) | |
private boolean mScrollable = false; | |
public LockableScrollView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
// TODO Auto-generated constructor stub | |
} | |
public LockableScrollView(Context context, AttributeSet attrs) | |
{ | |
super(context, attrs); | |
} | |
public LockableScrollView(Context context) | |
{ | |
super(context); | |
} | |
public void setScrollingEnabled(boolean enabled) { | |
mScrollable = enabled; | |
} | |
public boolean isScrollable() { | |
return mScrollable; | |
} | |
@Override | |
public boolean onKeyDown(int keyCode, KeyEvent event) { | |
// TODO Auto-generated method stub | |
LogUtil.info("LockableScrollView", "onKeyDown onKeyDown"); | |
return false ; | |
} | |
@Override | |
public boolean onKeyUp(int keyCode, KeyEvent event) { | |
// TODO Auto-generated method stub | |
return false ; | |
} | |
@Override | |
public boolean onTouchEvent(MotionEvent ev) { | |
switch (ev.getAction()) { | |
case MotionEvent.ACTION_DOWN: | |
// if we can scroll pass the event to the superclass | |
if (mScrollable) return super.onTouchEvent(ev); | |
// only continue to handle the touch event if scrolling enabled | |
return mScrollable; // mScrollable is always false at this point | |
default: | |
return super.onTouchEvent(ev); | |
} | |
} | |
@Override | |
public boolean onInterceptTouchEvent(MotionEvent ev) { | |
// Don't do anything with intercepted touch events if | |
// we are not scrollable | |
if (!mScrollable) return false; | |
else return super.onInterceptTouchEvent(ev); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you touch and drag some button in the scrollview it is still scrollable