brian
brian

Reputation: 6912

How to change gallery's scrollview

MyGallery gallery = (MyGallery)findViewById(R.id.gallery_photo);
PhonePhotoViewerAdapter = new PhonePhotoViewerAdapter(this, FilePath);
gallery.setAdapter(PhonePhotoViewerAdapter);
gallery.setSelection(0);

Above is my code to show photo with scrollview.
But if I want to focus on next, I have to move finger on screen with a larger distance.
I want to let it change focus while a smaller distance moved
How can I do it?
I had modify class Gallery as below:

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (e1.getX() - e2.getX() > 50  && Math.abs(velocityX) > 100) {    
// Fling left 
} else if (e2.getX() - e1.getX() > 50 && Math.abs(velocityX) > 100) {    
// Fling right 
}  
return false;
}

Below code is my modify:

gallery.setLongClickable(false);
gallery.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            int action = event.getAction();
            if((action == MotionEvent.ACTION_DOWN) && !Clicking) {
                StartX = event.getX();
                StartIndex = gallery.getSelectedItemPosition();
                Clicking = true;
            }
            else if((action == MotionEvent.ACTION_UP) && Clicking) {
                EndX = event.getX();
                EndIndex = gallery.getSelectedItemPosition();
                Clicking = false;
                if(((EndX - StartX) > 50) && (StartIndex == EndIndex)) {
                    if(EndIndex > 0) {
                        gallery.setSelection(EndIndex - 1);
                    }
                }
                else if(((StartX - EndX) > 50) && (StartIndex == EndIndex)) {
                    if(EndIndex < count - 1) {
                        gallery.setSelection(EndIndex + 1);
                    }
                }               
            }
            return false;
        }
    });

But there are some abnormal in showing.

Upvotes: 1

Views: 242

Answers (1)

brian
brian

Reputation: 6912

I find the solution of my problem.
That is define MyGallery as below:

public class MyGallery extends Gallery {
    public MyGallery(Context context, AttributeSet attrSet) {
        super(context, attrSet);
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        int kEvent;    
        if(isScrollingLeft(e1, e2)) {   
            //Check if scrolling left       
            kEvent = KeyEvent.KEYCODE_DPAD_LEFT;    
        }  
        else {   
            //Otherwise scrolling right      
            kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;     
        }    
        onKeyDown(kEvent, null);    
        return true;    
    }

    private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {
        return e2.getX() > e1.getX();   
    }  

    @Override
    protected android.view.ViewGroup.LayoutParams generateLayoutParams (android.view.ViewGroup.LayoutParams p) {
        return super.generateLayoutParams(p);
    }
}

Upvotes: 1

Related Questions