关闭

SwipeRefreshLayout下拉刷新

标签: androidJava
469人阅读 评论(0) 收藏 举报
分类:


<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_refresh_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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



public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        swipeRefreshLayout = ((SwipeRefreshLayout) inflater.inflate(R.layout.fragment_swipe_refresh_recycler, null));
        swipeRefreshLayout.setColorSchemeResources(
                android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light
        );
        layoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
        recyclerView = (RecyclerView) swipeRefreshLayout.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(layoutManager);
        adapter = initAdapter(getActivity(), list);
        recyclerView.setAdapter(adapter);
        recyclerView.addOnScrollListener(
                new RecyclerView.OnScrollListener() {
                    @Override
                    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                        super.onScrolled(recyclerView, dx, dy);
                    }
                    /**
                     * 向下滚动的过程,自动加载
                     */
                    @Override
                    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                        super.onScrollStateChanged(recyclerView, newState);
                        int itemCount = adapter.getItemCount();
                        layoutManager.findFirstCompletelyVisibleItemPosition();
                        swipeRefreshLayout.setEnabled(layoutManager.findFirstCompletelyVisibleItemPosition() == 0);
                     if (itemCount > PAGE_SIZE && layoutManager.findLastVisibleItemPosition() > itemCount - 5) {
                             request(currentPage + 1);
                        }
}
}
);
swipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
reset();
}
}
);
return swipeRefreshLayout;
}

/**
 * 切换fragment时,如果正在刷新会干扰事件的传递
 */
public void stopRefreshing() {
    if (swipeRefreshLayout != null && swipeRefreshLayout.isRefreshing()) {
        swipeRefreshLayout.setRefreshing(false);
    }

}

    public void reset() {
        stopRefreshing();
        request(FIRST_PAGE);
    }

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_refresh_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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



public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        swipeRefreshLayout = ((SwipeRefreshLayout) inflater.inflate(R.layout.fragment_swipe_refresh_recycler, null));
        swipeRefreshLayout.setColorSchemeResources(
                android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light
        );
        layoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
        recyclerView = (RecyclerView) swipeRefreshLayout.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(layoutManager);
        adapter = initAdapter(getActivity(), list);
        recyclerView.setAdapter(adapter);
        recyclerView.addOnScrollListener(
                new RecyclerView.OnScrollListener() {
                    @Override
                    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                        super.onScrolled(recyclerView, dx, dy);
                    }
                    /**
                     * 向下滚动的过程,自动加载
                     */
                    @Override
                    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                        super.onScrollStateChanged(recyclerView, newState);
                        int itemCount = adapter.getItemCount();
                        layoutManager.findFirstCompletelyVisibleItemPosition();
                        swipeRefreshLayout.setEnabled(layoutManager.findFirstCompletelyVisibleItemPosition() == 0);
                     if (itemCount > PAGE_SIZE && layoutManager.findLastVisibleItemPosition() > itemCount - 5) {
                             request(currentPage + 1);
                        }
}
}
);
swipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
reset();
}
}
);
return swipeRefreshLayout;
}

/**
 * 切换fragment时,如果正在刷新会干扰事件的传递
 */
public void stopRefreshing() {
    if (swipeRefreshLayout != null && swipeRefreshLayout.isRefreshing()) {
        swipeRefreshLayout.setRefreshing(false);
    }

}

    public void reset() {
        stopRefreshing();
        request(FIRST_PAGE);
    }

0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

SuperSwipeRefreshLayout 一个功能强大的自定义下拉刷新组件

SuperSwipeRefreshLayout一个功能强大的自定义下拉刷新组件。Why?下拉刷新这种控件,想必大家用的太多了,比如使用很多的XListView等。最近,项目中很多列表都是使用Reycl...
  • NUPTboyZHB
  • NUPTboyZHB
  • 2015-08-25 17:53
  • 6561

下拉刷新与上拉更多(用SwipeRefreshLayout和ListView实现)

SwipeRefreshLayout是Google在support v4 19.1版本的library更新的一个下拉刷新组件,实现刷新效果更方便。 1:在布局中添加SwipeRefreshLayo...
  • pengkv
  • pengkv
  • 2015-04-03 09:17
  • 5976

android:ListView下拉刷新上拉加载(SwipeRefreshLayout+滑动监听加载更多)

这次使用的是系统的SwipeRefreshLayout实现下拉刷新,和设置ListView的滑动监听判断是否滑动到最底部然后加载更多; 这个要比PullToRefreshListView简单很多,想P...
  • Zheng_Jiao
  • Zheng_Jiao
  • 2016-05-20 22:39
  • 3669

android原装下拉刷新控件--SwipeRefreshLayout

  • 2014-04-25 14:05
  • 1.47MB
  • 下载

Google官网下拉刷新SwipeRefreshLayout

  • 2014-12-25 11:16
  • 1.13MB
  • 下载

谷歌下拉刷新SwipeRefreshLayout修改版

  • 2014-11-26 00:02
  • 1.28MB
  • 下载

扩展的google官方下拉刷新控件SwipeRefreshLayout,支持多个子控件

  • 2015-04-30 22:50
  • 184KB
  • 下载

swipeRefreshLayout+listview下拉刷新

  • 2017-09-29 20:28
  • 907KB
  • 下载

toolbar+recyclerView+SwipeRefreshLayout+上拉刷新,下拉加载更多

  • 2016-01-09 22:50
  • 6.53MB
  • 下载

探索SwipeRefreshLayout配合自定义ListView完成下拉刷新、滑到底部自动加载更多

  • 2015-12-13 00:24
  • 5.84MB
  • 下载
    个人资料
    • 访问:13130次
    • 积分:264
    • 等级:
    • 排名:千里之外
    • 原创:12篇
    • 转载:0篇
    • 译文:2篇
    • 评论:1条
    文章分类
    文章存档