web-dev-qa-db-ja.com

Android ScrollView?のスクロールを無効および有効にする方法

私はAndroid developer.IでもScrollViewを使用したい。私を助けてください。また、次のようないくつかのコードを使用しようとします

fullparentscrolling.setHorizontalFadingEdgeEnabled(false);
fullparentscrolling.setVerticalFadingEdgeEnabled(false);

または

 fullparentscrolling.setEnabled(false);

しかし、それは機能しません。

22
Prosanto

この方法を試してください

このようにCustomScrollviewを作成します

import Android.content.Context;
import Android.util.AttributeSet;
import Android.view.MotionEvent;
import Android.widget.ScrollView;

public class CustomScrollView extends ScrollView {

    private boolean enableScrolling = true;

    public boolean isEnableScrolling() {
        return enableScrolling;
    }

    public void setEnableScrolling(boolean enableScrolling) {
        this.enableScrolling = enableScrolling;
    }

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

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

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

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        if (isEnableScrolling()) {
            return super.onInterceptTouchEvent(ev);
        } else {
            return false;
        }
    }
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
       if (isEnableScrolling()) {
            return super.onTouchEvent(ev);
       } else {
           return false;
       }
}
}

あなたのXMLで

//「com.example.demo」はパッケージ名に置き換えます

<com.example.demo.CustomScrollView
        Android:id="@+id/myScroll"
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content" >
    </com.example.demo.CustomScrollView>

あなたの活動で

CustomScrollView myScrollView = (CustomScrollView) findViewById(R.id.myScroll);
        myScrollView.setEnableScrolling(false); // disable scrolling
        myScrollView.setEnableScrolling(true); // enable scrolling
65
Biraj Zalavadia

スクロールビューのon touchリスナーを設定し、onTouchメソッドでtrueを再設定します。それは私のためのトリックをしました。

mScrollView.setOnTouchListener( new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) 
            {
                  return true;
            }
});

すべての人に最適

25
themobius

ScrollViewのスクロールを無効にすることはできません。 ScrollViewに拡張し、onTouchEventメソッドをオーバーライドして、何らかの条件が一致したときにfalseを返す必要があります。

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
                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) {
        // Don't do anything with intercepted touch events if 
        // we are not scrollable
        if (!mScrollable) return false;
        else return super.onInterceptTouchEvent(ev);
    }

}

ここでの参照は、スタックオーバーフローに関する完全なソリューションでもあります。 プログラムでScrollViewを無効にしますか?

これを他の人への回答としてマークしてください。

8
Hamad