Embedding ListView inside Gallery
The goal开发者_开发技巧 is to implement a Gallery whose adapter returns ListViews (in other words, vertically scrolling ListViews embedded in a horizontally scrolling Gallery). It sort of works after a bit of work, but when attempting to scroll horizontally, the ListView looks very jittery, like there is some stickiness to it being centered. I have not observed this kind of behavior with any other type of View embedded in a Gallery.
Here is what I have tried:
Initially, I found that the ListView squashed touch events, so the gesture listener on the Gallery never gets fired.
So in the onCreate() method of the Activity, I created a GestureDetector:
galleryGestureDetector = new GestureDetector(this, gallery);
Then, inside the getView() method of the Gallery adapter, after the ListView has been inflated and configured, I have some code like this:
listView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
galleryGestureDetector.onTouchEvent(event);
return true;
}
});
In this case I have even gone to the extreme step of returning true from the OnTouchListener to ensure that the onTouchEvent() method of the listView is never actually called. The same jittery behavior occurs. As a result, I think I can rule out competing onTouchEvent() implementations between the two views.
I tried abusing the TouchDelegate concept as well by extending the Gallery's touch rectangle to include the ListView and then forcing the ListView to delegate to it, but this was a futile effort as well.
I would throw up my hands and say it isn't possible currently, but the Social Networking app that packs with the DroidX somehow accomplishes it!
The problem is that ListView is intercepting touch events from the Gallery and then altering the view position itself. This is what leads to the back and forth jittering effect that I see when I use the widgets as is. I consider this a bug in the Gallery widget, but in the meantime it can be fixed by subclassing Gallery like this:
public class BetterGallery extends Gallery {
private boolean scrollingHorizontally = false;
public BetterGallery(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public BetterGallery(Context context, AttributeSet attrs) {
super(context, attrs);
}
public BetterGallery(Context context) {
super(context);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
super.onInterceptTouchEvent(ev);
return scrollingHorizontally;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
scrollingHorizontally = true;
return super.onScroll(e1, e2, distanceX, distanceY);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
scrollingHorizontally = false;
}
return super.onTouchEvent(event);
}
}
If you use BetterGallery in place of Gallery, the whole thing works just fine!
精彩评论