113

ScrollViewを有効にし、ボタンクリックで無効にしたいのですが。
無効とは、ScrollViewが存在しない場合などを意味し、有効にするとScrollViewが返されます。

テキスト画像のあるギャラリーがあり、ボタンをクリックすると画面の向きが変わるので、横向きでテキストが大きくなります。そして、画像が伸びず、テキストが読めなくなるようにScrollViewが必要です。

scrollview.Enabled=false / setVisibility(false)何もしません。

xml:

<ScrollView 
android:id="@+id/QuranGalleryScrollView" 
android:layout_height="fill_parent" 
android:layout_width="fill_parent">
<Gallery android:id="@+id/Gallery" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:scrollbars="horizontal"></Gallery>
</ScrollView>

Visibility(gone)を使用することはできません。これは、ギャラリーも非表示にするためです。必要なのは、ScrollViewの効果を非表示にすることです。ScrollViewがある場合、ギャラリーの画像はスクロール可能になり、画面に収まらないため、画像全体を表示するにはスクロールする必要があります。ボタンクリックでそれを無効/有効にしたくありません。

私はこれを試しました:

((ScrollView)findViewById(R.id.QuranGalleryScrollView)).setOnTouchListener(null);
                        ((ScrollView)findViewById(R.id.QuranGalleryScrollView)).setHorizontalScrollBarEnabled(false);
                        ((ScrollView)findViewById(R.id.QuranGalleryScrollView)).setVerticalScrollBarEnabled(false);
                        ((ScrollView)findViewById(R.id.QuranGalleryScrollView)).setEnabled(false);

ただし、ギャラリー内の画像はスクロール可能であり、画面に収まりません。これに対する解決策は何ですか?

4

17 に答える 17

197

最初にいくつかのポイント:

  1. ScrollView のスクロールを無効にすることはできません。ScrollView に拡張し、メソッドをオーバーライドして、条件が一致したときonTouchEventに戻る必要があります。false
  2. ギャラリー コンポーネントは、ScrollView 内にあるかどうかに関係なく、水平にスクロールします。ScrollView は垂直スクロールのみを提供します (水平スクロールには Horizo​​ntalScrollView が必要です)。
  3. 画像自体の伸縮に問題があると言っているようです-これはScrollViewとは関係ありません.ImageViewがandroid:scaleTypeプロパティ(XML)またはsetScaleTypeメソッドでスケーリングする方法を変更できます-たとえばScaleType.CENTER、画像は伸縮せず、元のサイズで中央に配置します

次のように変更ScrollViewして、スクロールを無効にすることができます

class LockableScrollView extends ScrollView {

    ...

    // true if we can scroll (not locked)
    // false if we cannot scroll (locked)
    private boolean mScrollable = true;

    public void setScrollingEnabled(boolean enabled) {
        mScrollable = enabled;
    }

    public boolean isScrollable() {
        return mScrollable;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // if we can scroll pass the event to the superclass
                return mScrollable && super.onTouchEvent(ev);
            default:
                return super.onTouchEvent(ev);
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        // Don't do anything with intercepted touch events if
        // we are not scrollable
        return mScrollable && super.onInterceptTouchEvent(ev);
    }

}

次に使用します

<com.mypackagename.LockableScrollView 
    android:id="@+id/QuranGalleryScrollView" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent">

    <Gallery android:id="@+id/Gallery" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:scrollbars="horizontal">
    </Gallery>

</com.mypackagename.LockableScrollView>

XML ファイルで (ScrollViewを特別なに変更しただけですLockableScrollView)。

それから電話する

((LockableScrollView)findViewById(R.id.QuranGalleryScrollView)).setScrollingEnabled(false);

ビューのスクロールを無効にします。

目的の結果を得るために、スクロールを無効にするだけの問題ではないと思います (たとえば、ギャラリーは上記のコードでスクロール可能のままです)。3 つのコンポーネント (ギャラリー、 ScrollView、ImageView) を使用して、それぞれにどのようなプロパティがあり、どのように動作するかを確認します。

于 2011-04-23T11:09:25.010 に答える
78

私はこのように行きました:

        scrollView.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            return isBlockedScrollView;
        }
    });
于 2013-03-15T07:22:49.303 に答える
14

設定したばかりのこの簡単なソリューションを見つけました

ScrollView.requestDisallowInterceptTouchEvent(true);
于 2014-01-20T10:48:06.703 に答える
5

まず、最初のコメントに投稿されたコードを使用しましたが、次のように変更しました。

    public class LockableScrollView extends ScrollView {

    public LockableScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }
    public LockableScrollView(Context context, AttributeSet attrs) 
    {
        super(context, attrs);
    }

    public LockableScrollView(Context context) 
    {
        super(context);
    }

    // true if we can scroll (not locked)
    // false if we cannot scroll (locked)
    private boolean mScrollable = true;

    public void setScrollingEnabled(boolean enabled) {
        mScrollable = enabled;
    }

    public boolean isScrollable() {
        return mScrollable;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // if we can scroll pass the event to the superclass
                if (mScrollable) return super.onTouchEvent(ev);
                // only continue to handle the touch event if scrolling enabled
                return mScrollable; // mScrollable is always false at this point
            default:
                return super.onTouchEvent(ev);
        }
    }



@Override  
public boolean onInterceptTouchEvent(MotionEvent ev) {
    switch (ev.getAction()) {     
    case MotionEvent.ACTION_DOWN:         
        // if we can scroll pass the event to the superclass      
        if (mScrollable) return super.onInterceptTouchEvent(ev);      
        // only continue to handle the touch event if scrolling enabled    
        return mScrollable; // mScrollable is always false at this point     
        default:          
            return super.onInterceptTouchEvent(ev);      
            }
    }

}

それから私はこの方法でそれを呼び出しました

((LockableScrollView)findViewById(R.id.scrollV)).setScrollingEnabled(false);

私が試したので

((LockableScrollView)findViewById(R.id.scrollV)).setIsScrollable(false);

しかし、 setIsScrollableは未定義であると言われました

これがあなたに役立つことを願っています

于 2014-02-27T11:17:12.660 に答える
4

答えについてコメントするのに十分なポイントがありませんが、次のように !isScrollable を返すように変更する必要があったことを除いて、mikec の答えはうまくいったと言いたいです。

mScroller.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
      return !isScrollable;
    }
});
于 2012-09-21T13:17:09.730 に答える
3

これがより簡単な解決策です。onTouch()タッチによるスクロールをバイパスする場合は、 for をオーバーライドしてScrollView OnTouchListenerfalse を返します。プログラムによるスクロールは引き続き機能し、ScrollViewクラスを拡張する必要はありません。

mScroller.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
  return isScrollable;
}
});
于 2012-06-26T15:43:29.490 に答える
1

@JosephEarl +1彼はここで素晴らしい解決策を持っており、プログラムでそれを行うためのいくつかの小さな変更を加えて、私にとって完璧に機能しました。


これが私が行ったマイナーな変更です:

LockableScrollViewクラス:

public boolean setScrollingEnabled(boolean enabled) {
    mScrollable = enabled;
    return mScrollable;
}

主な活動:

LockableScrollView sv;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    sv = new LockableScrollView(this);
    sv.setScrollingEnabled(false);
}
于 2012-11-07T21:04:01.987 に答える
1
@Override  
public boolean onInterceptTouchEvent(MotionEvent ev) {
    switch (ev.getAction()) {     
    case MotionEvent.ACTION_DOWN:         
        // if we can scroll pass the event to the superclass      
        if (mScrollable) return super.onInterceptTouchEvent(ev);      
        // only continue to handle the touch event if scrolling enabled    
        return mScrollable; // mScrollable is always false at this point     
        default:          
            return super.onInterceptTouchEvent(ev);      
            }
    }
于 2011-07-13T21:54:14.740 に答える
1

他のビューとの干渉を無効にできる CustomScrollView を作成できます。ただし、これはエンド ユーザーにとって苛立たしい場合があります。注意して使用してください。

これはコードです:

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewParent;

public class CustomScrollView extends android.widget.ScrollView {

    public CustomScrollView(Context context) {
        super(context);
    }

    public CustomScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev)
    {
        /* Prevent parent controls from stealing our events once we've gotten a touch down */
        if (ev.getActionMasked() == MotionEvent.ACTION_DOWN)
        {
            ViewParent p = getParent();
            if (p != null)
                p.requestDisallowInterceptTouchEvent(true);
        }

        return false;
    }
}

CustomScrollView の使用方法

別の Scrollview を子ビューとして追加したい画面に CustomScrollView を親として追加すると、画面全体がスクロール可能になります。これを RelativeLayout に使用しました。

<?xml version="1.0" encoding="utf-8"?>
<**package.**CustomScrollView 
xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/some_id"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:tools="http://schemas.android.com/tools">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    #**Inside this I had a TableLayout and TableRow. Inside the TableRow,**
    #**I had a TextView which I made scrollable from Java Code.**
    #**textView.setMovementMethod(new ScrollingMovementMethod());**
    </RelativeLayout>
</ankit.inventory.ankitarora.inventorymanagementsimple.CustomScrollView>

私の場合はうまくいきました。

于 2017-03-02T18:58:02.117 に答える
1
public class LockableScrollView extends ScrollView {

    private boolean mScrollable = true;

    public LockableScrollView(Context context) {
        super(context);
    }

    public LockableScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public LockableScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public LockableScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public void setScrollable(boolean enabled) {
        mScrollable = enabled;
    }

    public boolean isScrollable() {
        return mScrollable;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return mScrollable && super.onTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return mScrollable && super.onInterceptTouchEvent(ev);
    }
}
于 2018-03-15T15:32:58.770 に答える
0

これは役に立ちますか?

((ScrollView)findViewById(R.id.QuranGalleryScrollView)).setOnTouchListener(null);
于 2011-04-23T09:09:16.533 に答える
0

ギャラリーを拡張し、フラグを使用して、必要に応じてスクロールを無効にすることができます。

public class MyGallery extends Gallery {

public boolean canScroll;

public MyGallery(Context context, AttributeSet attrs) {
    canScroll = true;
    super(context, attrs);
}

public void setCanScroll(boolean flag)
{
    canScroll = flag;
}

@Override
public boolean onScroll(android.view.MotionEvent e1, android.view.MotionEvent e2, float distanceX, float distanceY) {
    if (canScroll)
        return super.onScroll(e1,e2,distancex,distancey);
    else
        return false;
}

@Override
public boolean onSingleTapUp(MotionEvent e)
{
    if (canScroll)
        return super.onSingleTapUp(ey);
    else
        return false;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
    if (canScroll)
        return super.onFling(e1,e2,velocityX,velocityY);
    else
        return false;
}
}
于 2012-10-28T00:36:44.227 に答える
-6

ドキュメントでわかるように、可視性を false に設定することはできません。あなたの場合、おそらく使用する必要があります:

scrollview.setVisibility(Visibility.GONE);
于 2011-04-23T09:08:58.780 に答える
-6

ボタンクリックリスナーで行うだけ

ScrollView sView = (ScrollView)findViewById(R.id.ScrollView01);

sView.setVerticalScrollBarEnabled(false);
sView.setHorizontalScrollBarEnabled(false);

ボタンのクリック時にスクロールバーが有効にならないようにする

于 2011-04-23T09:09:58.000 に答える