I am trying to develop a custom ViewGroup to implement sort of a gantt 
chart with vertical and horizontal scrolling.

So I went about the job and implemented following view

I find following issues with this code.

[1] when onScroll is called I see some flicker and not been able 
to successfully use _scroller.startScroll 
[2] while scrolling onMeasure and onLayout rely on _scroller to get the 
correct scrolling positions, however since there is some delay between 
onMeasure and onLayout, _scroller would provide different values for 
_scroller.getCurrX() and_scroller.getCurrY()in both the functions.So 
measure and layout may easily go out of sync.

How do I solve these two issues? For two I can think of implementing 
onMeasure and onLayout in the same method say in onLayout or in onLayout 
use the scroller position that is measured in onMeasure.

For [1] if I use view's scrollBy, I see there is no flickers but it calls 
invalidate internally and onMeasure and onLayout will not be called and my 
view will not get a layout refresh.

Any help would be greatly appreciated. 


package com.example.android.layout;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.Scroller;

public class CustomView extends ViewGroup implements OnGestureListener {

private GestureDetector gestureScanner;

private Scroller _scroller;
private int _viewPortX, _viewPortY;
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
gestureScanner = new GestureDetector(this);
_scroller = new Scroller(this.getContext());

}


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// add a child view from adapter (if view is not already present and
// needs to be shown)
// remove unwanted views
// called measure on children
// call setMeasuredDimension((widthMeasureSpec), (heightMeasureSpec));
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// Layout children here
// ...

if (_scroller.computeScrollOffset()) {
scrollNow(_scroller.getCurrX(), _scroller.getCurrY());

/* This is necessary to request layout while in layout loop */
post(new Runnable() {
public void run() {
requestLayout();
}
});
}
}

@Override
public boolean onTouchEvent(MotionEvent me) {
return gestureScanner.onTouchEvent(me);
}

public void scrollNow(float x, float y) {
_viewPortX = (int) x;
_viewPortY = (int) y;
requestLayout();
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float 
velocityX,float velocityY) {
_scroller.fling(_viewPortX, _viewPortY, (int) velocityX, (int) velocityY,
0, _maxX, 0, _maxY);
requestLayout();

return true;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {

// doing this and not reliying on scroller, and no call to 
_scroller.startScroll
int scrollToX = _viewPortX - (int) distanceX;
scrollToX = Math.min(scrollToX, _maxX);
scrollToX = Math.max(scrollToX, 0);

int scrollToY = _viewPortY - (int) distanceY;
scrollToY = Math.min(scrollToY, _maxY);
scrollToY = Math.max(scrollToY, 0);

scrollNow(scrollToX, scrollToY);

return true;
}

@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return true;
}

@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
 }

@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}


@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
 }

}
 

Thanks
harvinder

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to