views:

1212

answers:

6

I loaded some images into a gallery. Now I'm able to scroll but once started scrolling the scrolling won't stop. I would like the gallery to just scroll to the next image and then stop until the user does the scroll gesture again.

this is my code

import android.widget.ImageView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener;

public class GalleryExample extends Activity {

private Gallery gallery;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

     gallery = (Gallery) findViewById(R.id.examplegallery);
     gallery.setAdapter(new AddImgAdp(this));

     gallery.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id) {

            Toast.makeText(GalleryExample.this, "Position=" + position, Toast.LENGTH_SHORT).show();
        }
    });

}

public class AddImgAdp extends BaseAdapter {
    int GalItemBg;
    private Context cont;


    private Integer[] Imgid = {
            R.drawable.a_1, R.drawable.a_2, R.drawable.a_3, R.drawable.a_4, R.drawable.a_5, R.drawable.a_6, R.drawable.a_7
    };

    public AddImgAdp(Context c) {
        cont = c;
        TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
        GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
        typArray.recycle();
    }

    public int getCount() {
        return Imgid.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imgView = new ImageView(cont);

        imgView.setImageResource(Imgid[position]);

        i.setScaleType(ImageView.ScaleType.FIT_CENTER);
        imgView.setBackgroundResource(GalItemBg);

        return imgView;
    }
}

}

and the xmlLayout file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/examplegallery"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
</LinearLayout>
A: 

I cannot exactly reproduce right now because I don't have your R.styleable handy.

Actually, do you notice the same thing when running the API demo / Views / Gallery / Photos ?

If it does it may be a hardware issue ? I have a NexusOne/Android 2.1 and did not notice what you describe. If I move slowly my finger, it only move a couple of pictures

Bob Yoplait
Thanks for your help Bob, but I want show only the next with a finger movement then stop in the image. like a pagination one by one!, thanks for your help :)
A: 

I know what you're trying to achieve Alexi; I'm trying to do the same thing. Almost a pagination where each time you swipe, it only moves one image and stops, so you could never move more than one image in one gesture.

I have tried wait()'ing onItemSelected and I've tried setEnabled(false) inside of a timer, once something is selected, but they don't really work, and they are probably hacking anyway. I wish I knew more about Android internals. I'll post back here if I find it.

Have you found the answer by chance?

jordan snyder
+1  A: 

The easiest way I have found to accomplish this is by overriding the Gallery onFling method, and providing my own velocityX value:

@Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        return super.onFling(e1, e2, 10, velocityY);
    }

It's not perfect, but it does the job. Ideally, you would probably write something custom for onFling to make it work exactly as you like.

Tim H
A: 

yeah guys messy problem indeed! I have the same very problem. The "news and weather" app in the nexus1 has the effect u are looking for. I myself haven't found out yet how to fix it. As some1 suggested on the thread overriding onFling is an alright solution:

   @Override
   public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
     return super.onFling(e1, e2, 0, velocityY);
   }

I found out 0 is the best value for velocityX but then the swipe on the screen has to be really long to trigger the transition. I dunno! has anyone found how to fix this hell of an issue?

there are nearly no app out there implementing a decent swiping through views. I wonder if it's cuz it's not a well supported behaviour or rather we all suck! ;)

nourdine
+1  A: 

I think I found a way to achieve both only scrolling 1 view at a time in the gallery and be able to have a minimal swipe length to trigger animation.

Override the onFling method of the gallery widget and instead of calling super.onFling, check to see whether or not the swipe was from left to right or right to left and call the appropriate dpad event as shown below:

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

@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;  
}
Nadewad
A: 

I found the following override on onFling worked nicely enough for small swipes and single page pagination:

@Override

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) 
{
    boolean leftScroll = isScrollingLeft(e1, e2);

    float velX;
    if(leftScroll)
    {
        velX=500;
    }
    else
    {
        velX=-500;
    }

    return super.onFling(e1, e2, velX, velocityY);
}

the vel +-500 value seems to provide a good enough result, but could be adjusted to suit your preference

Lemonsanver