【问题标题】:How to stop scrolling in a Gallery Widget?如何停止在图库小部件中滚动?
【发布时间】:2011-01-23 08:04:15
【问题描述】:

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

这是我的代码

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>

【问题讨论】:

  • 你应该提供更多关于你的问题的信息以获得答案
  • 好的,我有几张图片加载到画廊中,当我向右滚动时,它永远不会停止,我想在每张图片中停止。
  • Janusz 我确定你知道如何解决这个问题! :D
  • 贴出你已有的代码

标签: android scroll android-widget gallery


【解决方案1】:

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

覆盖图库小部件的 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;  
}

【讨论】:

  • 您,先生,是一只聪明的狐狸。感谢您提供这个优雅的解决方案 - 正是我需要让我的画廊可用。
  • 正是我想要的。只是为了澄清这个解决方案,我无法弄清楚如何设置 onFling() 内联,而不得不求助于创建自定义画廊。这个解决方案帮助我走到了最后:stackoverflow.com/questions/4582123/…
  • 这对我来说是完美的,除了如果我尝试滚动(不扔)图像说左图像到中心,当我到达中心时,左图像到达中心。如何防止这种情况???
  • 很抱歉,我不太确定您在问什么。你能说得更具体点吗?
  • 哈,很好,但是我们必须寻找这样的解决方法而不是一个简单的属性,这真是太可怜了……
【解决方案2】:

这是对我有用的代码。

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

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

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

享受吧!

【讨论】:

    【解决方案3】:

    简单的方法如下:

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

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

    【讨论】:

    • 这个对我来说效果最好,因为它使画廊一次只能滚动一个,无论你滑动多快。我已经尝试过 KeyEvent 解决方案,如果你刷得太快,它会跳过两三个。
    【解决方案4】:

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

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

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

    【讨论】:

      【解决方案5】:

      我发现 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方法)

      【讨论】:

        【解决方案6】:

        不需要做任何事情,只需返回 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;  
        }
        

        对我有用

        【讨论】:

          【解决方案7】:

          感谢上帝! 天哪,我真的把它插入了一些代码,它工作了!呃,连续 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);
          }
          }
          

          【讨论】:

          • 很高兴这对您有所帮助。实际上,Google 已经发布了 ViewPager 类,它实现了相同的目标,但实际上在您滑动手指时会执行中间滚动动画,而不是在滑动动作完成后滚动。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多