@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recycle_swipetolayout); swipeToLoadLayout = ((SwipeToLoadLayout) findViewById(R.id.swipeToLoadLayout)); mRecycleView = ((RecyclerView) findViewById(R.id.swipe_target)); adapter = new HomeAdapter(); // 设置垂直的线性布局管理器,Orientation --> VERTICAL:垂直 HORIZONTAL:水平 LinearLayoutManager layoutManager = new LinearLayoutManager(this); layoutManager.setOrientation(LinearLayoutManager.VERTICAL); // StaggeredGridLayoutManager layoutManager = new // StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL); // 添加分割线 mRecycleView.addItemDecoration( new DividerItemDecoration(getApplicationContext(), DividerItemDecoration.VERTICAL_LIST)); mRecycleView.setLayoutManager(layoutManager); mRecycleView.setItemAnimator(new DefaultItemAnimator()); mRecycleView.setAdapter(adapter); adapter.refresh(); /** 设置下拉刷新和上拉加载监听 */ swipeToLoadLayout.setOnRefreshListener(this); swipeToLoadLayout.setOnLoadMoreListener(this); }
@Override public void onRefresh() { swipeToLoadLayout.postDelayed( new Runnable() { @Override public void run() { adapter.refresh(); swipeToLoadLayout.setRefreshing(false); } }, 2000); }
@Override public void onLoadMore() { swipeToLoadLayout.postDelayed( new Runnable() { @Override public void run() { adapter.add(); swipeToLoadLayout.setLoadingMore(false); } }, 2000); }