Android 如何停止在图库小部件中滚动?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2373617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to stop scrolling in a Gallery Widget?
提问by Nadewad
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
和 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>
回答by Nadewad
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.
我想我找到了一种方法来实现在图库中一次只滚动 1 个视图,并且能够以最小的滑动长度来触发动画。
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:
覆盖图库小部件的 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;
}
回答by peceps
This is the code that worked for me.
这是对我有用的代码。
Nadewad's solution + some animation speed adjustments:
Nadewad 的解决方案 + 一些动画速度调整:
Create class that extends Gallery, and override this metods:
创建扩展 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;
}
Enjoy!
享受!
回答by James
The easy way is following:
简单的方法如下:
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
return super.onFling(e1, e2, 0, velocityY);
}
You also can check here: Android Infinite Loop Gallery
您也可以在这里查看: Android 无限循环图库
回答by anshul
no need to do any thing simply return false
无需做任何事情,只需返回 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;
}
it worked for me
它对我有用
回答by Tim H
The easiest way I have found to accomplish this is by overriding the Gallery onFling method, and providing my own velocityX value:
我发现实现此目的的最简单方法是覆盖 Gallery onFling 方法,并提供我自己的 velocityX 值:
@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.
它并不完美,但它可以完成工作。理想情况下,您可能会为 onFling 编写一些自定义内容,使其完全按照您的意愿工作。
回答by dijipiji
I found the following override on onFling worked nicely enough for small swipes and single page pagination:
我发现 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);
}
The velX +-500 value seems to provide a good enough result, but could be adjusted to suit your preferences.
velX +-500 值似乎提供了足够好的结果,但可以根据您的喜好进行调整。
(Note: this is using the isScrollingLeft
method presented in @Nadewad's answer)
(注意:这是使用@Nadewad 的回答中isScrollingLeft
提供的方法)
回答by Newbie
THANK GOD!!! OMG, I literally plugged this into some code and it WORKED! ugh, 48-hrs straight and to think I only needed to do the perfect google search for my answer. Literally, plug and play. Thanks so much.
感谢上帝!!!天哪,我真的把它插入了一些代码,它奏效了!呃,连续 48 小时,我认为我只需要完美的谷歌搜索就可以找到我的答案。顾名思义,即插即用。非常感谢。
new code
新代码
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;
}
}
old code: just for comparison purposes
旧代码:仅供比较
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);
}
}