如何在scrollView到达Android底部时触发事件?

时间:2009-08-27 11:38:10

标签: android events

我正在寻找一个可以在用户滚动到底部时触发的scrollView上使用的事件。

例如,我的项目列表应该自动扩展更多项目。

任何想法如何做到这一点?

我很感激任何提示。

5 个答案:

答案 0 :(得分:8)

请阅读这篇文章:Android: Understanding When Scrollview Has Reached The Bottom

源代码:

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) 
{
    // Grab the last child placed in the ScrollView, we need it to determinate the bottom position.
    View view = (View) getChildAt(getChildCount()-1);

    // Calculate the scrolldiff
    int diff = (view.getBottom()-(getHeight()+getScrollY()));

    // if diff is zero, then the bottom has been reached
    if( diff == 0 )
    {
        // notify that we have reached the bottom
        Log.d(ScrollTest.LOG_TAG, "MyScrollView: Bottom has been reached" );
    }

    super.onScrollChanged(l, t, oldl, oldt);
}

如果您愿意,可以添加制作自定义小部件。

示例:

package se.marteinn.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ScrollView;


/**
 * Triggers a event when scrolling reaches bottom.
 *
 * Created by martinsandstrom on 2010-05-12.
 * Updated by martinsandstrom on 2014-07-22.
 *
 * Usage:
 *
 *  scrollView.setOnBottomReachedListener(
 *      new InteractiveScrollView.OnBottomReachedListener() {
 *          @Override
 *          public void onBottomReached() {
 *              // do something
 *          }
 *      }
 *  );
 * 
 *
 * Include in layout:
 *  
 *  <se.marteinn.ui.InteractiveScrollView
 *      android:layout_width="match_parent"
 *      android:layout_height="match_parent" />
 *  
 */
public class InteractiveScrollView extends ScrollView {
    OnBottomReachedListener mListener;

    public InteractiveScrollView(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
    }

    public InteractiveScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public InteractiveScrollView(Context context) {
        super(context);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        View view = (View) getChildAt(getChildCount()-1);
        int diff = (view.getBottom()-(getHeight()+getScrollY()));

        if (diff == 0 && mListener != null) {
            mListener.onBottomReached();
        }

        super.onScrollChanged(l, t, oldl, oldt);
    }


    // Getters & Setters

    public OnBottomReachedListener getOnBottomReachedListener() {
        return mListener;
    }

    public void setOnBottomReachedListener(
            OnBottomReachedListener onBottomReachedListener) {
        mListener = onBottomReachedListener;
    }


    /**
     * Event listener.
     */
    public interface OnBottomReachedListener{
        public void onBottomReached();
    }

}

答案 1 :(得分:2)

我遇到了同样的问题,我使用 ListView 解决了这个问题(如果需要,会自动添加ScrollView)并设置 OnScrollListener

以下是代码示例:

        tableListView.setOnScrollListener(new OnScrollListener() {

        public void onScrollStateChanged(AbsListView view, int scrollState) {

        }

        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
            if (visibleItemCount == totalItemCount)
            // too little items (ScrollView not needed)
            {
                java.lang.System.out.println("too little items to use a ScrollView!");
            }
            else {
                if ((firstVisibleItem + visibleItemCount) == totalItemCount) {
                    // put your stuff here
                    java.lang.System.out.println("end of the line reached!");
                }
            }

        }
    });

答案 2 :(得分:0)

如果您使用的是低于21(android M)的android API,请不要尝试SetOnScrollChangeListener,可以尝试以下操作:

yourScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {
                int rootHeight = yourScrollView.getHeight();
                int childHeight = yourScrollView.getChildAt(0).getHeight();
                int scrollY = yourScrollView.getScrollY();
                if(childHeight - rootHeight == scrollY)
                Log.d("Hei","You Have Reach The End!");
            }
        });

答案 3 :(得分:0)

这个答案来晚了,但是如果有人偶然发现,我们可以使用setOnScrollChangeListener

library(shiny)
library(ggplot2)

table <- data.frame(
  A = c(1,2,3),
  B = c(3,2,1)
)

ui <- fluidPage(
  mainPanel(
    plotOutput("Plot", click="plot_click"), 
    tableOutput("HitSpots")
  )
)

server <- function(input, output){

  output$Plot <- renderPlot({ 
    ggplot(table, aes(x=A, y=B)) + geom_point() 
  })

  hit <- reactive({ nearPoints(table, input$plot_click) })

  output$HitSpots <- renderTable({
    hit()
  })

}

shinyApp(ui = ui, server = server)

答案 4 :(得分:-2)

这是一个更简单的解决方案,无需子类滚动视图。 假设您有一个RecyclerView recyclerView变量,

recyclerView.computeVerticalScrollRange()为您提供完整的滚动范围,换句话说,就是内容高度。

(recyclelerView.computeVerticalScrollOffset()在滚动时给出顶部的偏移量。此值将持续到内容高度 - 滚动视图高度。

所以,

    if( (recyclerView.computeVerticalScrollOffset() == 
(recyclerView.computeVerticalScrollRange() - recyclerView.getHeight()) ) {
    // You have hit rock bottom.
    }

操纵滚动视图的子视图看起来真是太乱了。