Android - 用于交错网格视图的OnScrollListener

时间:2013-05-31 07:25:02

标签: android gridview scroll

请原谅我的英语,我是法国人......

所以,我对我的Android应用程序有疑问。 我要将网格视图集成为Pinterest风格。 我找到了这个lib:交错网格视图(https://github.com/maurycyw/StaggeredGridView

它工作正常但是......没有OnScrollListener! 我必须知道用户何时看到最后一项,以便加载更多。 但是没有OnScrollListener就不可能!

你对我的问题有所了解吗?

非常感谢。

4 个答案:

答案 0 :(得分:1)

<强>更新
经过测试,
notifyDataSetChanged()案例调用StaggeredGridView Adapter将滚动位置重置为0(视图开始) 而是使用PinterestLikeAdapterView


我已将loadMoreListener添加到StaggeredGridView
步骤:
1-添加一个loadmore接口和booelan isLoading(在类的顶部)

public interface OnLoadMoreListener {
    public boolean onLoadMore();
}

private boolean mIsLoading;
private OnLoadMoreListener mLoadMoreListener;

2-查找trackMotionScroll函数,在deltaY > 0的其他内容中 并在up = false;添加

之后
if (overhang <= 80) { // 80 is how nearest to the end. ZERO mean the end of list
    // scrolling down and almost reach the end of list
    if (false == mIsLoading && null != mLoadMoreListener) {
        if (false == mLoadMoreListener.onLoadMore()) {
            mIsLoading = true;
            Log.d(TAG, "loadMore");
        }
    }
}

3-添加setListener()loadComplated()函数

public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
    this.mLoadMoreListener = onLoadMoreListener;
}

public void loadMoreCompleated() {
    mIsLoading = false;
}

如何使用
1-在loadMoreListener

中定义Activity/Fragment
private StaggeredGridView.OnLoadMoreListener loadMoreListener = new StaggeredGridView.OnLoadMoreListener() {

    @Override
    public boolean onLoadMore() {
        loading.setVisibility(View.VISIBLE);
        // load more data from internet (not in the UI thread)

        return true; // true if you have more data to load, false you dont have more data to load 
    }
};

2-完成数据加载并将其添加到适配器调用

private void doneLoading() {
    gridView.loadMoreCompleated();
    loading.setVisibility(View.GONE);
}

答案 1 :(得分:1)

如果没有任何作用,请尝试此操作,然后将回收站视图包装在滚动视图中,然后检查滚动视图是否已到达底部:

在你的xml文件中:

<ScrollView
    android:id="@+id/scroll_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scrollbars="vertical"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    </RelativeLayout>

</ScrollView>

在您的班级文件中:

private ScrollView mScrollView;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    initScrollView();
    ...
}

private void initScrollView() {
    mScrollView = (ScrollView) mView.findViewById(R.id.scroll_view);
    mScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
        @Override
        public void onScrollChanged() {
            if (mScrollView != null) {
                if (mScrollView.getChildAt(0).getBottom() <= (mScrollView.getHeight() + mScrollView.getScrollY())) {
                    // Scroll view is at bottom
                    // If not loading then start load more
                } else {
                    // Scroll view is not at bottom
                }
            }
        }
    });
}

请记住将recycler视图的嵌套滚动设置为false,以便在类文件中平滑滚动:

mRecyclerView.setNestedScrollingEnabled(false);

如果你想在它到达bottomm之前开始加载下一页,那么只需从mScrollView.getChildAt(0)中减去一个整数.getBottom()

例如,这里减去1000:

if (mScrollView.getChildAt(0).getBottom() - 1000 <= (mScrollView.getHeight() + mScrollView.getScrollY())) {
    // Scroll view is at bottom
    // If not loading then start load more
} else {
    // Scroll view is not at bottom
}

在滚动到达底部之前,使用更大的整数开始加载。

答案 2 :(得分:0)

  

必须知道用户何时看到最后一项,以便加载更多。   但是没有OnScrollListener就不可能!

为什么要使用onScrollListener呢?而是编写一个类(MyStaggeredGridAdapter,也许是?),它扩展了BaseAdapter并将其应用于StaggeredGridView。然后你可以使用适配器的getView()方法,只要需要渲染一个新视图就会调用它。

答案 3 :(得分:0)

要知道屏幕上显示最后一项的时间,我使用适配器的getView()方法。只需将您的上一个索引与&#34;位置&#34;进行比较参数。像这样的东西

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ...

        if (lastLoaded == position){
            loadMore();
        }

        ...
    }

然后您可能希望看到:How to add data into bottom of Android StaggeredGridView and not go back top?以避免从开头加载列表并从显示的最后一项继续。

我希望这有帮助,不知道它是否是最佳解决方案,但是对我有用;)