【问题标题】:How to detect the end of the custom scroll view? [duplicate]如何检测自定义滚动视图的结束? [复制]
【发布时间】:2015-03-02 05:06:53
【问题描述】:

我已经完成了垂直滚动视图。我的要求是,我只需要检测滚动视图的结束并使按钮可见。谁能告诉我如何检测滚动视图已到达底部?

public class LockableVerScroll extends ScrollView {

private boolean isScrollable;
private ScrollListener scrollListener=null;
public LockableVerScroll(Context context)
{
    super(context);
    isScrollable=true;
}

public LockableVerScroll(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
    isScrollable=true;
}
public LockableVerScroll(Context context, AttributeSet attrs,int defStyle)
{
    super(context, attrs, defStyle);
    isScrollable=true;
}
public boolean isScrollable()
{
    return isScrollable;
}
public boolean setScrollable(boolean mScrollable)
{
    return mScrollable=isScrollable;
}
public void setScrollViewListener(ScrollListener mscrollListener)
{
    this.scrollListener=mscrollListener;

}

需要帮助..提前谢谢.!!

【问题讨论】:

    标签: android scrollview


    【解决方案1】:
    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
            View view = (View) getChildAt(getChildCount()-1);
            int diff = (view.getBottom()-(getHeight()+getScrollY()+view.getTop()));// Calculate the scrolldiff
            if( diff == 0 ){  // if diff is zero, then the bottom has been reached
                Log.d(ScrollTest.LOG_TAG, "MyScrollView: Bottom has been reached" );
            }
            super.onScrollChanged(l, t, oldl, oldt);
    }
    

    【讨论】:

      【解决方案2】:

      ScrollView 的扩展中覆盖onScrollChanged() 方法

      @Override
      protected void onScrollChanged(int l1, int t1, int l2, int t2) {
      
          int count = getChildCount();
          View view = (View) getChildAt(count - 1);
          int delta = (view.getBottom() - (getHeight() + getScrollY() + view.getTop()));
          if( delta == 0 )
          {
              // scrollview has reached bottom, do whatever you want here.
          }
          super.onScrollChanged(l1, t1, l2, t1);
      }
      

      试试这个。这将起作用。

      编辑:

      if( delta == 0 )
      {
          // define interface in Activity / Fragment and call its method here
          mScrollListener.onScrollViewHitsBottom();
      }
      

      【讨论】:

      • 我需要把这个方法放在上面的类中还是?
      • 是的,将其添加到LockableVerScroll 类中
      • 你看懂代码了吗?
      • 不..我只是一个初学者..
      • ok 没问题..我想对你说的是,当ScrollView 达到最低点时,无论你想做什么,你都需要在if 块中进行。 ..我希望你明白这一点:)
      猜你喜欢
      • 2016-11-25
      • 2012-10-30
      • 1970-01-01
      • 2012-11-23
      • 2016-05-25
      • 2011-04-27
      • 2011-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多