禁止以编程方式滚动型? [英] Disable ScrollView Programmatically?

查看:147
本文介绍了禁止以编程方式滚动型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现滚动型和一个按钮单击禁用它。
禁用的手段一样,如果在滚动型wasnt有..并使其返回滚动型..
我想这是因为我有文字图像画廊,以及按钮上点击屏幕的方向变化,因此在园林中的文字变大。而我想要的滚动视图,因此图像犯规舒展它的自我和文本变得不可读..
scrollview.Enabled = FALSE / setVisibility(假)犯规做什么.. XML:

 <滚动型
机器人:ID =@ + ID / QuranGalleryScrollView
机器人:layout_height =FILL_PARENT
机器人:layout_width =FILL_PARENT>
<画廊机器人:ID =@ + ID /画廊
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:滚动条=横向>< /画廊>
< /滚动型>
 

感谢

EDIT1:我不能使用可视性(走了),因为这也将隐藏画廊,我要的是隐藏滚动型的效果。当有滚动型多媒体资料中的图像会scrollabale,不适合在屏幕所以ü必须滚动看到整个形象,我不想做的禁用/启用按钮上点击。我试过这样的:

 ((滚动型)findViewById(R.id.QuranGalleryScrollView))setOnTouchListener(空)。
                        ((滚动型)findViewById(R.id.QuranGalleryScrollView))setHorizo​​ntalScrollBarEnabled(假)。
                        ((滚动型)findViewById(R.id.QuranGalleryScrollView))setVerticalScrollBarEnabled(假)。
                        ((滚动型)findViewById(R.id.QuranGalleryScrollView))的setEnabled(假)。
 

但还是在画廊的图像滚动,而不是适应屏幕..什么解决这个?

解决方案

几点开始说起:

  1. 您不能禁用滚动型的滚动。您需要扩展到滚动型和覆盖的onTouchEvent 方法返回某些条件匹配时。
  2. 画廊组件水平滚动,不管它是在一个滚动型与否 - 一个滚动型只提供垂直滚动(你需要一个Horizo​​ntalScrollView水平滚动)
  3. 您好像说你有图象扩张本身出现了问题 - 这无关的滚动型,您可以更改的ImageView的尺度与安卓scaleType 属性(XML)或者 setScaleType 方法 - 例如 ScaleType.CENTER 不会伸展你的形象,将集中它在它的原始大小

您可以修改滚动型如下禁用滚动

 类LockableScrollView扩展了滚动型{

    ...

    //如此,如果我们可以滚动(未锁定)
    //错误的,如果我们不能滚动(锁定)
    私人布尔mScrollable = TRUE;

    公共无效setScrollingEnabled(布尔启用){
        mScrollable =启用;
    }

    公共布尔isScrollable(){
        返回mScrollable;
    }

    @覆盖
    公共布尔的onTouchEvent(MotionEvent EV){
        开关(ev.getAction()){
            案例MotionEvent.ACTION_DOWN:
                //如果我们可以滚动的事件传递给超
                如果(mScrollable)返回super.onTouchEvent(EV);
                //只有不断地处理触摸事件,如果滚动启用
                返回mScrollable; // mScrollable永远是假的,在这一点上
            默认:
                返回super.onTouchEvent(EV);
        }
    }

    @覆盖
    公共布尔onInterceptTouchEvent(MotionEvent EV){
        //不要拦截触摸事件,如果做任何事情
        //我们不滚动
        如果(mScrollable!)返回false;
        否则返回super.onInterceptTouchEvent(EV);
    }

}
 

您会然后用

 < com.mypackagename.LockableScrollView
    机器人:ID =@ + ID / QuranGalleryScrollView
    机器人:layout_height =FILL_PARENT
    机器人:layout_width =FILL_PARENT>

    <画廊机器人:ID =@ + ID /画廊
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:滚动条=横向>
    < /画廊>

< /com.mypackagename.LockableScrollView>
 

在XML文件(只是改变了滚动型您的特殊 LockableScrollView )。

然后调用

<$p$p><$c$c>((LockableScrollView)findViewById(R.id.QuranGalleryScrollView)).setScrollingEnabled(false);

禁用视图的滚动。

我觉得你有一个以上禁用滚动虽然能达到您想要的结果只是问题(画廊将继续滚动上述code为例) - 我建议你做一些更多的研究,在每个三个组成部分(图库,滚动型,ImageView的),看看什么样的属性每一个都有它的行为方式。

I would like to enable ScrollView and disable it by a Button Click.
Disable means like if the ScrollView wasnt there.. and enable it returns the ScrollView..
I want that because I have a gallery with text images, and on a button click the screen orientation changes, so in Landscape the text becomes bigger.. And I want the scrollview so the image doesnt stretch it self and the text becomes unreadable..
scrollview.Enabled=false / setVisibility(false) doesnt make anything.. 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>

Thanks

Edit1: I cant use Visibility(gone) since that would also hide the Gallery, what I want is to hide the effect of the scrollview.. When there is ScrollView the images in Gallery become scrollabale and do not fit in the screen so u have to scroll to see the whole image, I dont want do disable/enable that on a button click.. I tried this:

((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);

But still the images in the Gallery are scrollable and not fit the screen.. whats the solution to this?

解决方案

Several points to begin with:

  1. You cannot disable the scrolling of a ScrollView. You would need to extend to ScrollView and override the onTouchEvent method to return false when some condition is matched.
  2. The Gallery component scrolls horizontally regardless of whether it is in a ScrollView or not - a ScrollView provides only vertical scrolling (you need a HorizontalScrollView for horizontal scrolling)
  3. You seem to say you have a problem with the image stretching itself -- this has nothing to do with the ScrollView, you can change how an ImageView scales with the android:scaleType property (XML) or the setScaleType method - for instance ScaleType.CENTER will not stretch your image and will center it at it's original size

You could modify ScrollView as follows to disable scrolling

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

}

You would then use

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

in your XML file (just changed the ScrollView to your special LockableScrollView).

Then call

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

to disable scrolling of the view.

I think that you have more than just the issue of disabling scrolling though to achieve your desired result (the gallery will remain scrollable with the above code for instance) - I'd recommend doing some more research on each of the three components (Gallery, ScrollView, ImageView) to see what properties each one has and how it behaves.

这篇关于禁止以编程方式滚动型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆