android - 如何停止在图库小部件中滚动?

标签 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>

最佳答案

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

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

关于android - 如何停止在图库小部件中滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2373617/

相关文章:

Android向小部件添加标题

android - Android 2.3.3 上的 ListView 页脚背景

javascript - 将 HTML5 应用程序转换为独立的 Android 应用程序

android - 从首选项中删除图标

scroll - Polymer 1.0 的无限滚动(无附加)

javascript - 移动 Android Chrome 在 "window.scrollTo"和 "behavior: smooth"之后起伏不定的滞后滚动

Android:当我启动一个新 Activity 并按返回返回时,Listview 会 self 复制

java - Android WebView 的阅读器模式

jquery - 滚动具有绝对位置的内容

android - 如何获取屏幕宽度和高度