如果您希望能够启用/禁用图库的滚动,您可以使用以下类:
public class ExtendedGallery extends Gallery {
private boolean stuck = false;
public ExtendedGallery(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public ExtendedGallery(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ExtendedGallery(Context context) {
super(context);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return stuck || super.onTouchEvent(event);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_LEFT:
case KeyEvent.KEYCODE_DPAD_RIGHT:
return stuck || super.onKeyDown(keyCode, event);
}
return super.onKeyDown(keyCode, event);
}
public void setScrollingEnabled(boolean enabled) {
stuck = !enabled;
}
}
Run Code Online (Sandbox Code Playgroud)
根据Gallery源代码,有两种事件类型可以开始滚动:屏幕触摸和按键在D-pad上按下.因此,如果要禁用滚动,可以拦截这些事件.然后在你的布局中使用这样的东西:
<your.package.name.ExtendedGallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
Run Code Online (Sandbox Code Playgroud)
然后您可以随时启用/禁用该库的滚动:
ExtendedGallery mGallery = (ExtendedGallery) findViewById(R.id.gallery);
mGallery.setScrollingEnabled(false); // disable scrolling
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3704 次 |
| 最近记录: |