以编程方式禁用 ScrollView? ?

我想启用 ScrollView 并通过按钮单击禁用它。
禁用意味着如果 ScrollView 不存在,则启用它返回 ScrollView。

我希望这样,因为我有一个文字图像的画廊,在一个按钮上点击屏幕方向的变化,所以在横向的文字变得更大。我想要的滚动视图,以便图像不会拉伸本身和文字变得不可读。

scrollview.Enabled=false / setVisibility(false) doesn't 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>

I can't 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 scrollable and do not fit in the screen so you have to scroll to see the whole image. I don't want to disable/enable that on a button click.

我试过了:

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

但是画廊中的图像仍然是可滚动的,不适合屏幕。解决这个问题的办法是什么?

145580 次浏览

正如您在 文件中看到的,您不能将可见性设置为 false。在你的情况下,你可能应该使用:

scrollview.setVisibility(Visibility.GONE);

这有帮助吗?

((ScrollView)findViewById(R.id.QuranGalleryScrollView)).setOnTouchListener(null);

按钮上点击监听器就可以了

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


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

因此,滚动条不启用按钮点击

首先有几点:

  1. 不能禁用 ScrollView 的滚动。您需要扩展到 ScrollView 并重写 onTouchEvent方法,以便在某些条件匹配时返回 false
  2. 不管是否在 ScrollView 中,Gallery 组件都会水平滚动—— ScrollView 只提供垂直滚动(需要 HorizontalScrollView 进行水平滚动)
  3. 你似乎说你有一个问题与图像拉伸本身-这与 ScrollView 无关,你可以改变一个图像视图如何与 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);

禁用视图的滚动。

我认为你不仅仅需要禁用滚动来实现你想要的结果(例如,图库将保持滚动)-我建议对三个组件(图库,ScrollView,ImageView)中的每一个做更多的研究,看看每一个都有什么属性,以及它是如何工作的。

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

这里有一个更简单的解决方案。覆盖 ScrollView OnTouchListeneronTouch()并返回 false,如果你想通过触摸绕过滚动。编程滚动仍然可以工作,不需要扩展 ScrollView类。

mScroller.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return isScrollable;
}
});

我没有足够的点数来评论一个答案,但我想说的是,迈克的答案为我工作,除了我必须改变它返回!可以这样滚动:

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

You can extend the gallery and use some flag to disable scrolling when you want:

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

@JosephEarl +1 He has a great solution here that worked perfectly for me with some minor modifications for doing it programmatically.


以下是我做的一些小改动:

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

我走的是这条路:

        scrollView.setOnTouchListener(new View.OnTouchListener() {


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

禁用 ScrollView

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

找到了这个简单的解决方案

ScrollView.requestDisallowInterceptTouchEvent(true);

to start, i used the Code posted posted in the first Comment but i changed it like this:

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

because i tried

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

但是它说 SetIsScrollable是未定义的

我希望这能帮到你

我有一个在 NestedScrollView 上的布局,使用触摸事件并禁用滚动:

progressBarLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return true;
}
});

并使:

progressBarLayout.setOnTouchListener(null);

您可以创建 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?

您可以将 CustomScrollView 作为父视图添加到要在其中添加另一个 Scrollview 作为子视图的屏幕中,整个屏幕是可滚动的。我用这个做了一个 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>

对我的案子很有用。

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

科特林:

scrollView.setOnTouchListener { _, _ -> true }

Extend ScrollView and override onInterceptTouchEvent to return false, in Kotlin:

class SV(context: Context) : HorizontalScrollView(context) {
var allowUserScroll = false


override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
return if (allowUserScroll) super.onInterceptTouchEvent(ev) else false
}
}

够了!

(与使用 setOnTouchListener相比,这种方式的优点是禁用的 ScrollView不会以任何方式干扰子代的触摸事件处理)

Here is a Kotlin extension

   @SuppressLint("ClickableViewAccessibility")
fun NestedScrollView.disableScroll() {
this.setOnTouchListener { _, _ ->
true
}
}


@SuppressLint("ClickableViewAccessibility")
fun NestedScrollView.enableScroll() {
this.setOnTouchListener { _, _ ->
false
}
}


@SuppressLint("ClickableViewAccessibility")
fun ScrollView.disableScroll() {
this.setOnTouchListener { _, _ ->
true
}
}


@SuppressLint("ClickableViewAccessibility")
fun ScrollView.enableScroll() {
this.setOnTouchListener { _, _ ->
false
}
}