Android: Turn Listview Scrolling On And Off
Solution 1:
Well I figured it out; I guess it's only fair to leave an answer behind for the next guy who stumbles onto this question. I apologize in advance to Java purists, I write Java like C#. I'm also kind of figuring out this Android thing as I go.
The solution was to create a custom ListView. The magic in it is that it has the option to turn touch-event dispatching (specifically ACTION_MOVE, which is the one that does scrolling) on and off. Here's the code:
publicclassUnScrollingListViewextendsListView {
publicUnScrollingListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); }
publicUnScrollingListView(Context context, AttributeSet attrs) { super(context, attrs); }
publicUnScrollingListView(Context context) { super(context); }
publicbooleanDisableTouchEventScroll=false;
protectedbooleanDispatchMoveEventInsteadOfConsumingIt=false;
protectedViewDispatchTarget=null;
publicvoidResetToNormalListViewBehavior() {
DisableTouchEventScroll = false;
DispatchMoveEventInsteadOfConsumingIt = false;
DispatchTarget = null;
}
publicvoidSetUpDispatch(View v) {
DisableTouchEventScroll = true;
DispatchMoveEventInsteadOfConsumingIt = true;
DispatchTarget = v;
}
protectedstaticfloat[] GetRelativeCoordinates(View innerView, MotionEvent e) {
int[] screenCoords = newint [2]; //not sure if I have to preallocate this or not.
innerView.getLocationOnScreen(screenCoords);
returnnewfloat[] {
e.getRawX() - screenCoords[0],
e.getRawY() - screenCoords[1]};
}
@OverridepublicbooleandispatchTouchEvent(MotionEvent ev){
if(DispatchMoveEventInsteadOfConsumingIt && DispatchTarget != null) {
//convert coordinate systemsfloat[] newCoords = GetRelativeCoordinates(DispatchTarget, ev);
ev.setLocation(newCoords[0], newCoords[1]);
DispatchTarget.onTouchEvent(ev);
returntrue;
}
if(DisableTouchEventScroll && ev.getAction() == MotionEvent.ACTION_MOVE) {
returntrue;
}
returnsuper.dispatchTouchEvent(ev);
}
}
This behaves like a normal listview by default. You can call SetUpDispatch(View) to tell it to stop scrolling the listview and dispatch all ACTION_MOVE events to a specific View. You can then call ResetToNormalListViewBehavior() to make it start scrolling again. With the signature capture code linked in my original post, all you need to do is change the following in the onTouchEvent:
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
...
listView.SetUpDispatch(this);
returntrue;
case MotionEvent.ACTION_UP:
listView.ResetToNormalListViewBehavior();
case MotionEvent.ACTION_MOVE:
...
Not sure if anyone will ever encounter this problem, but it seems to have some general applications, so good luck with it if you ever read this.
Solution 2:
Try this on your ListView Capture Signature Class:
publicbooleanonTouchEvent(MotionEvent e) {
super.onTouchEvent(e);
ViewParentv=this.getParent();
while (v != null){
if(v instanceof ScrollView || v instanceof HorizontalScrollView){
FrameLayoutf= (FrameLayout)v;
f.requestDisallowInterceptTouchEvent(true);
}
v = v.getParent();
}
//... rest of the code
}
Post a Comment for "Android: Turn Listview Scrolling On And Off"