我是
靠谱客的博主
平淡往事,最近开发中收集的这篇文章主要介绍
android中gridview的上拉加载更多,觉得挺不错的,现在分享给大家,希望可以做个参考。
先看效果图
上面效果图可以看到,gridview滑动到底部时,如果再次拉动可以加载下一页内容
下面是使用步骤
一,首先定义监听器,来监听gridview是否滑动到底部
- package com.stickynavlistview.listenter;
-
- import android.view.View;
- import android.widget.AbsListView;
- import android.widget.AbsListView.OnScrollListener;
- import android.widget.Toast;
-
-
-
-
- public class AutoLoadListener implements OnScrollListener {
-
- public interface AutoLoadCallBack {
- void execute();
- }
-
- private int getLastVisiblePosition = 0, lastVisiblePositionY = 0;
- private AutoLoadCallBack mCallback;
-
- public AutoLoadListener(AutoLoadCallBack callback) {
- this.mCallback = callback;
- }
-
- public void onScrollStateChanged(AbsListView view, int scrollState) {
-
- if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
-
- if (view.getLastVisiblePosition() == (view.getCount() - 1)) {
- View v = (View) view.getChildAt(view.getChildCount() - 1);
- int[] location = new int[2];
- v.getLocationOnScreen(location);
- int y = location[1];
-
- if (view.getLastVisiblePosition() != getLastVisiblePosition && lastVisiblePositionY != y)
- {
- Toast.makeText(view.getContext(), "已经拖动至底部,再次拖动即可翻页", Toast.LENGTH_SHORT).show();
- getLastVisiblePosition = view.getLastVisiblePosition();
- lastVisiblePositionY = y;
- return;
- } else if (view.getLastVisiblePosition() == getLastVisiblePosition && lastVisiblePositionY == y)
- {
- mCallback.execute();
- }
- }
-
-
- getLastVisiblePosition = 0;
- lastVisiblePositionY = 0;
- }
- }
-
- public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {
-
- }
- }
2,使用时只需要为gridview设置
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.fragment_tab, container, false);
- mListView = (GridView) view
- .findViewById(R.id.id_stickynavlayout_innerscrollview);
-
-
- AutoLoadListener autoLoadListener = new AutoLoadListener(callBack);
- mListView.setOnScrollListener(autoLoadListener);
- initPage();
- return view;
-
- }
-
- AutoLoadListener.AutoLoadCallBack callBack = new AutoLoadListener.AutoLoadCallBack() {
-
- public void execute() {
-
- loadSpareItems(currentPage + 1);
- }
-
- };
-
-
github下载地址:
https://github.com/qiushi123/StickyNavListview-pulltoGridView
发表评论 取消回复