【问题标题】:How to disable HorizontalScrollView scrolling on button click and enable again on another button click?如何在单击按钮时禁用 Horizo​​ntalScrollView 滚动并在单击另一个按钮时再次启用?
【发布时间】:2017-11-05 05:02:04
【问题描述】:

我在 xml 中添加了一个 Horizo​​ntalScrollView,我想在单击按钮时禁用滚动并在单击另一个按钮时再次启用。

通过单击按钮禁用滚动有效,但我不知道如何再次启用滚动。

下面的代码是禁用滚动的工作原理。

class OnTouch implements View.OnTouchListener
{
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true;
    }
}

我添加了上面的类,然后,

final HorizontalScrollView scrollView = (HorizontalScrollView)findViewById    (R.id.horizontalScrollView);
    Button stop = (Button)findViewById(R.id.stop);
    stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            scrollView.setOnTouchListener(new OnTouch());
        }
    });

我在 onCreate 方法中添加了上面的代码。我想添加另一个按钮(可能是“滚动”),并且我希望该按钮再次启用滚动。

【问题讨论】:

    标签: android horizontalscrollview


    【解决方案1】:

    这个怎么样(我没测试过,可能有错别字);

    class OnTouch implements View.OnTouchListener { 
        public boolean intercept = false;
        @Override public boolean onTouch(View v, MotionEvent event) { 
            return intercept; 
    } }
    
    final OnTouch listener = new OnTouch()); 
    final HorizontalScrollView scrollView = (HorizontalScrollView)findViewById (R.id.horizontalScrollView); 
    scrollView.setOnTouchListener(listener);
    Button stop = (Button)findViewById(R.id.stop);
    stop.setOnClickListener(new View.OnClickListener() { listener.intercept=true});
    Button start = (Button)findViewById(R.id.start);
    start.setOnClickListener(new View.OnClickListener() { listener.intercept=false});
    

    【讨论】:

      【解决方案2】:

      1 - 在与您的 Button 关联的 onClickListener 函数中:

      a) 删除scrollView.setOnTouchListener(new OnTouch())

      b) 切换boolean(例如scrollEnabled)以指示是否启用滚动。

      2 - 在您的 ScrollView 类中,覆盖 onTouchEvent 函数并将其放入其中:

          if(scrollEnabled){
              return(false);
          else {
              return(true);
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-07-15
        • 2021-04-02
        • 1970-01-01
        • 2023-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多