以编程方式禁用ScrollView?

Oma*_*mar 104 android android-scrollview

我想启用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>
Run Code Online (Sandbox Code Playgroud)

谢谢

Edit1:我不能使用Visibility(消失),因为那也会隐藏Gallery,我想要的是隐藏ScrollView的效果.当有ScrollView时,Gallery中的图像变为scrollabale并且不适合屏幕,所以你必须滚动才能看到整个图像,我不想在按钮点击时禁用/启用它.

我试过这个:

((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);
Run Code Online (Sandbox Code Playgroud)

但是图库中的图像仍然是可滚动的,不适合屏幕..这是什么解决方案?

Jos*_*arl 179

以下几点:

  1. 您无法禁用滚动浏览ScrollView.您需要扩展到ScrollView并覆盖onTouchEvent方法以false在匹配某些条件时返回.
  2. 无论是否在ScrollView中,Gallery组件都会水平滚动 - 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);
    }

}
Run Code Online (Sandbox Code Playgroud)

然后你会用

<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>
Run Code Online (Sandbox Code Playgroud)

在您的XML文件中(只是更改ScrollView为您的特殊LockableScrollView).

然后打电话

((LockableScrollView)findViewById(R.id.QuranGalleryScrollView)).setScrollingEnabled(false);
Run Code Online (Sandbox Code Playgroud)

禁用滚动视图.

我认为你不仅仅是为了实现你想要的结果而禁用滚动的问题(例如,图库将依旧使用上面的代码保持滚动) - 我建议对这三个组件中的每一个进行更多的研究(Gallery, ScrollView,ImageView)查看每个属性具有哪些属性以及它的行为方式.


lil*_*ily 69

我走了这条路:

        scrollView.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            return isBlockedScrollView;
        }
    });
Run Code Online (Sandbox Code Playgroud)

  • @PeteH,我不知道为什么,但我的`ListView`的工作方式相反:当禁用滚动时为true,启用滚动时为false. (4认同)
  • 简单而且效果很好.关键是使用变量(例如,isBlockedScrollView)来控制何时启用滚动.当允许滚动时将其设置为true,否则设置为false. (3认同)

JWq*_*ist 9

找到这个简单的解决方案

ScrollView.requestDisallowInterceptTouchEvent(true);
Run Code Online (Sandbox Code Playgroud)

  • `requestDisallowInterceptTouchEvent`仅适用于要锁定的View的**子**.所以它更像是'mLinearLayout.requestDisallowInterceptTouchEvent(true);` (5认同)

Dav*_*ave 5

我没有足够的观点来评论答案,但我想说 mikec 的答案对我有用,只是我必须将其更改为 return !isScrollable ,如下所示:

mScroller.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
      return !isScrollable;
    }
});
Run Code Online (Sandbox Code Playgroud)


小智 5

禁用ScrollView

ScrollView sw = (ScrollView) findViewById(R.id.scrollView1);
sw.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true;
    }
});
Run Code Online (Sandbox Code Playgroud)


小智 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);      
            }
    }

}
Run Code Online (Sandbox Code Playgroud)

然后我就这样叫了

((LockableScrollView)findViewById(R.id.scrollV)).setScrollingEnabled(false);
Run Code Online (Sandbox Code Playgroud)

因为我尝试过

((LockableScrollView)findViewById(R.id.scrollV)).setIsScrollable(false);
Run Code Online (Sandbox Code Playgroud)

但是它说setIsScrollable是不确定的

我希望这能帮到您