如何在Gallery Widget中停止滚动?

时间:2010-03-03 17:48:53

标签: android scroll android-widget gallery

我将一些图片加载到图库中。现在我可以滚动但是一旦开始滚动滚动就不会停止。我希望画廊只是滚动到下一个图像,然后停止,直到用户再次进行滚动手势。

这是我的代码

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;
    }
}

}

和xmlLayout文件

<?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>

7 个答案:

答案 0 :(得分:48)

我想我找到了一种方法,既可以在画廊中同时滚动1个视图,又可以使用最小的滑动长度来触发动画。

覆盖gallery小部件的onFling方法,而不是调用super.onFling,检查滑动是从左到右还是从右到左,并调用相应的dpad事件,如下所示:

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;  
}

答案 1 :(得分:12)

这是适合我的代码。

Nadewad的解决方案+一些动画速度调整:

创建扩展Gallery的类,并覆盖此metods:

  @Override
  public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    setAnimationDuration(600);
    return super.onScroll(e1, e2, distanceX, distanceY);
  }

  @Override
  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    float velMax = 2500f;
    float velMin = 1000f;
    float velX = Math.abs(velocityX);
    if (velX > velMax) {
      velX = velMax;
    } else if (velX < velMin) {
      velX = velMin;
    }
    velX -= 600;
    int k = 500000;
    int speed = (int) Math.floor(1f / velX * k);
    setAnimationDuration(speed);

    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;
  }

享受!

答案 2 :(得分:3)

简单方法如下:

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

你也可以在这里查看: Android Infinite Loop Gallery

答案 3 :(得分:2)

我发现实现这一目标的最简单方法是重写Gallery onFling方法,并提供我自己的velocityX值:

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

它并不完美,但它能完成这项工作。理想情况下,您可能会为onFling编写一些自定义内容,以使其完全按照您的喜好工作。

答案 4 :(得分:2)

我发现onFling上的以下覆盖功能非常适合小滑动和单页分页:

@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);
}

velX + -500值似乎提供了足够好的结果,但可以根据您的喜好进行调整。

(注意:这是使用@Nadewad's answer

中提供的isScrollingLeft方法

答案 5 :(得分:2)

无需做任何事情只需返回false

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

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

它对我有用

答案 6 :(得分:1)

感谢上帝! 天哪,我把它插入到一些代码中并且它已经工作了!呃,48小时直,并认为我只需要完美的谷歌搜索我的答案。从字面上看,即插即用。非常感谢。

新代码

public class CustomGallery extends Gallery {

public CustomGallery(Context context) {
    super(context);
}

public CustomGallery(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomGallery(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

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;  
}
}

旧代码:仅用于比较目的

 public class CustomGallery extends Gallery {

public CustomGallery(Context context) {
    super(context);
}

public CustomGallery(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomGallery(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
        float distanceY) {
    //Do something specific here.
    return super.onScroll(e1, e2, distanceX, distanceY);
}
}