Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

android - how to stop scrolling gallery?

I've got a gallery (images) in a RelativeLayout and if the users click on it, three Buttons and a TextView appears. I made it with the visible-property, that means the three Buttons and the TextView are declared as invisible in the xml-file and later the onClick() of the Gallery makes it visible with setVisibility(0).That works fine, but I want the Gallery to stop scrolling during the Buttons and the TextView are in front.

Is there any way to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you want to be able to enable/disable scrolling of the Gallery, you could use class like this:

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

}

According to the Gallery source code, there are two event types that start the scrolling: screen touch and the key, pressed on D-pad. So you could intercept these events if you want to disable scrolling. Then use something like this in your layout:

<your.package.name.ExtendedGallery
    android:id="@+id/gallery"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />

Then you can enable/disable scrolling of that gallery at any time:

ExtendedGallery mGallery = (ExtendedGallery) findViewById(R.id.gallery);
mGallery.setScrollingEnabled(false); // disable scrolling

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...