加载更多的数据时,滚动型滚动到下 [英] Load more data when ScrollView is scrolled to bottom

查看:124
本文介绍了加载更多的数据时,滚动型滚动到下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个滚动视图与dinamically加载内容。有时可以有很多内容,所以我想装载更多,当用户滚动到下。

I've got a scrollView with dinamically loading content. Sometimes there can be a lot of content, so I would like to load more when user scrolls to bottom.

我searced合适metods发现两个:

I searced for suitable metods and found two:

onScrollChanged() 

getScrollY()

但我不知道如何使用它为我的目的。 请给我一些建议。

But I dont know how to use it for my purposes. Please give me some advice.

推荐答案

滚动视图不提供任何方法来检查,如果你已经达到视图的底部,所以最好的方法是扩大与滚动视图自己的自定义视图。这是我在我的应用程序如何实现的。

Scroll view does not provide any method to check if you've reached bottom of the view so the best technique is to extend your own custom view with scroll view . This is how i implemented in my app.

第1步:创建滚动视图的自定义类

public class ObservableScrollView extends ScrollView {

private ScrollViewListener scrollViewListener = null;

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

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

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

public void setScrollViewListener(ScrollViewListener scrollViewListener) {
    this.scrollViewListener = scrollViewListener;
}

@Override
protected void onScrollChanged(int x, int y, int oldx, int oldy) {

    View view = (View) getChildAt(getChildCount() - 1);
    int diff = (view.getBottom() - (getHeight() + getScrollY()));
    if (diff == 0) { // if diff is zero, then the bottom has been reached
        if (scrollViewListener != null) {
            scrollViewListener.onScrollEnded(this, x, y, oldx, oldy);
        }
    }
    super.onScrollChanged(x, y, oldx, oldy);
}

}

第2步:创建一个滚动视图监听器接口

public interface ScrollViewListener {

void onScrollEnded(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);



 }

第三步:添加视图在布局

   <com.platinumapps.facedroid.ObservableScrollView
        android:id="@+id/scrollView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </com.platinumapps.facedroid.ObservableScrollView>

第四步:实现在你的类接口

  public class MyFragment extends Fragment implements ScrollViewListener 

@Override
public void onScrollEnded(ObservableScrollView scrollView, int x, int y, int oldx,   int oldy) {

           //Perform your action
}

和你做

这篇关于加载更多的数据时,滚动型滚动到下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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