请原谅我的英语,我是法国人...
所以,我有一个关于我的 Android 应用程序的问题。我必须将网格视图集成为 Pinterest 样式。我找到了这个库:交错网格视图(https://github.com/maurycyw/StaggeredGridView)
它工作正常但是......没有 OnScrollListener !我必须知道用户何时看到最后一个项目,以便加载更多。但是没有 OnScrollListener 是不可能的!
你对我的问题有想法吗?
非常感谢。
请原谅我的英语,我是法国人...
所以,我有一个关于我的 Android 应用程序的问题。我必须将网格视图集成为 Pinterest 样式。我找到了这个库:交错网格视图(https://github.com/maurycyw/StaggeredGridView)
它工作正常但是......没有 OnScrollListener !我必须知道用户何时看到最后一个项目,以便加载更多。但是没有 OnScrollListener 是不可能的!
你对我的问题有想法吗?
非常感谢。
如果对您没有任何效果,请尝试此操作,然后将回收器视图包装在滚动视图中,然后检查滚动视图是否已到达底部:
在您的 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
}
}
}
});
}
请记住将回收器视图的嵌套滚动设置为 false 以便在您的类文件中平滑滚动:
mRecyclerView.setNestedScrollingEnabled(false);
如果您想在下一页到达底部之前开始加载下一页,则只需从 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
}
在滚动到达底部之前使用更大的整数开始加载。
更新:
经过测试,
要求案例将滚动位置重置为 0(视图开始)notifyDataSetChanged()
。
而是使用PinterestLikeAdapterViewStaggeredGridView Adapter
我已经添加loadMoreListener
到StaggeredGridView
步骤:
1-添加一个loadmore接口和booelan isLoading(在类的顶部)
public interface OnLoadMoreListener {
public boolean onLoadMore();
}
private boolean mIsLoading;
private OnLoadMoreListener mLoadMoreListener;
2-查找trackMotionScroll
功能,在else的deltaY > 0
和up = false;
add之后
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);
}
要知道最后一个项目何时显示在屏幕上,我使用适配器的 getView() 方法。只需将您的最后一个索引与“位置”参数进行比较。像这样的东西
@Override
public View getView(int position, View convertView, ViewGroup parent) {
...
if (lastLoaded == position){
loadMore();
}
...
}
也许你想看到这个:如何将数据添加到 Android StaggeredGridView 的底部而不返回顶部?以避免从头开始加载列表并从显示的最后一个项目继续。
我希望这会有所帮助,不知道这是否是最好的解决方案,但对我有用;)
必须知道用户何时看到最后一个项目,以便加载更多。但是没有 OnScrollListener 是不可能的!
你为什么要使用 onScrollListener 呢?相反,编写一个扩展 BaseAdapter 的类(也许是 MyStaggeredGridAdapter?)并将其应用于 StaggeredGridView。然后可以使用适配器的 getView() 方法,每当需要渲染新视图时都会调用该方法。