码迷,mamicode.com
首页 > 其他好文 > 详细

RecyclerView 上拉加载下拉刷新

时间:2017-07-28 16:21:03      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:nbsp   extend   on()   xtend   ogre   void   fresh   list   resource   

 RecyclerView 上拉加载下拉刷新

 

 <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/teach_swipe"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.v4.widget.SwipeRefreshLayout>

 

public class RecyclerOnScrollListener extends RecyclerView.OnScrollListener {
    private LinearLayoutManager layoutManager;
    private LoadMoreData loadMoreData;

    public RecyclerOnScrollListener(LinearLayoutManager layoutManager, LoadMoreData loadMoreData) {
        this.layoutManager = layoutManager;
        this.loadMoreData = loadMoreData;
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        int lastVisibleItem = layoutManager.findLastVisibleItemPosition();
        int totalItemCount = layoutManager.getItemCount();
        //dy>0表示向上滑动
        //lastVisibleItem >= totalItemCount - 2表示剩下两个
        if (lastVisibleItem >= totalItemCount - 2 && dy > 0) {
            loadMoreData.loadMore();
        }
    }

    public interface LoadMoreData {
        void loadMore();
    }
}

 

 核心代码片段:------------------

 //下拉刷新
        mSwipeRefresh = (SwipeRefreshLayout) findViewById(R.id.teach_swipe);
        mSwipeRefresh.setSize(1);
        mSwipeRefresh.setColorSchemeColors(Color.RED, Color.GREEN);
        mSwipeRefresh.setProgressBackgroundColorSchemeResource(R.color.colorAccent);
        mSwipeRefresh.setOnRefreshListener(this);

 @Override
    public void onRefresh() {
        indexPage = 1;
        getData();
        mHandler.sendEmptyMessageDelayed(UPDATE, DELAY_TIME);
    }

//上拉加载
        mRecyclerView.addOnScrollListener(new RecyclerOnScrollListener(layoutManager, new RecyclerOnScrollListener.LoadMoreData() {
            @Override
            public void loadMore() {
                indexPage++;
                getData();
            }
        }));

 

RecyclerView 上拉加载下拉刷新

标签:nbsp   extend   on()   xtend   ogre   void   fresh   list   resource   

原文地址:http://www.cnblogs.com/shen-hua/p/5828422.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!