如何在StaggeredGridView中使用PullToRefresh?

时间:2013-05-10 01:28:10

标签: android swiperefreshlayout staggered-gridview

我在我的项目中使用Staggeredgridview。

我从这里下载了StaggeredGridView库和演示:https://github.com/maurycyw/StaggeredGridViewDemo https://github.com/maurycyw/StaggeredGridView

我需要PullToRefresh用于整个gridview

全部谢谢

2 个答案:

答案 0 :(得分:3)

刚刚在Chris Banes中添加了对StaggeredGridView的支持 Android-PullToRefresh。 同时支持ActionBarSherlock的{​​{3}}。

答案 1 :(得分:0)

将gridview包含在SwipeRefreshLyout中。

     <android.support.v4.widget.SwipeRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/swipeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

     </android.support.v4.widget.SwipeRefreshLayout>

在onViewCreated中为gridview添加onScroll侦听器。设置启用刷新时的条件。

    swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipeLayout);
    swipeRefreshLayout.setColorSchemeResources(R.color.theme_color);
    swipeRefreshLayout.setOnRefreshListener(this);
    gridView.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView absListView, int i) {
        }

        @Override
        public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            int dist = gridView.getDistanceToTop();

            if (dist == 0)
                swipeRefreshLayout.setEnabled(true);
            else {
                swipeRefreshLayout.setEnabled(false);
            }

        }
    });

然后添加onRefresh函数。

     @Override
    public void onRefresh() {
      if (!swipeRefreshLayout.isRefreshing()) {
          swipeRefreshLayout.setRefreshing(true);
      }
      //add functionality then set refresh to false
      swipeRefreshLayout.setRefreshing(false);
    }