|
上图
 
没有什么注释 没有demo 但是 思路清晰 好好理解看一下
xml
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.v4.widget.SwipeRefreshLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/swipe_refresh_widget"
- android:layout_width="match_parent"
-
- android:layout_height="match_parent"
- tools:context="com.jsu.whr.recyclerview.Main3Activity">
- <android.support.v7.widget.RecyclerView
- android:id="@+id/recycle_view"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
-
- </android.support.v7.widget.RecyclerView>
- </android.support.v4.widget.SwipeRefreshLayout>
主activity
- package com.jsu.whr.recyclerview;
-
- import android.os.Handler;
- import android.support.v4.widget.SwipeRefreshLayout;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.support.v7.widget.LinearLayoutManager;
- import android.support.v7.widget.RecyclerView;
-
- import com.jsu.whr.myapplication.R;
-
- import java.util.ArrayList;
- import java.util.List;
-
- public class Main3Activity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
- SwipeRefreshLayout mSwipeRefreshLayout;
- RecyclerView mRecyclerView;
- MyAdapter adapter;
- List<String> list = new ArrayList<String>();
- LinearLayoutManager mLayoutManager;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main3);
- mSwipeRefreshLayout = (SwipeRefreshLayout) this.findViewById(R.id.swipe_refresh_widget);
- mSwipeRefreshLayout.setColorSchemeResources(
- R.color.black,
- R.color.read,
- R.color.green,
- R.color.blue);
- mSwipeRefreshLayout.setOnRefreshListener(this);
- mRecyclerView = (RecyclerView) this.findViewById(R.id.recycle_view);
- mLayoutManager = new LinearLayoutManager(this);
- mRecyclerView.setLayoutManager(mLayoutManager);
- mRecyclerView.setOnScrollListener(mOnScrollListener);
- adapter = new MyAdapter(this);
- mRecyclerView.setAdapter(adapter);
- }
- private RecyclerView.OnScrollListener mOnScrollListener = new RecyclerView.OnScrollListener() {
- private int lastVisibleItem;
- @Override
- public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
- super.onScrollStateChanged(recyclerView, newState);
- if (newState == RecyclerView.SCROLL_STATE_IDLE
- && lastVisibleItem + 1 == adapter.getItemCount()
- && adapter.isShowFooter()) {
-
- handler.postDelayed(new Runnable() {
- @Override
- public void run() {
- list.addAll(loadNewData());
- adapter.notifyDataSetChanged();
- }
- },1000);
-
-
- }
- }
- @Override
- public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
- super.onScrolled(recyclerView, dx, dy);
- lastVisibleItem = mLayoutManager.findLastVisibleItemPosition();
- }
- };
- @Override
- public void onRefresh() {
- handler.postDelayed(new Runnable() {
- @Override
- public void run() {
- if(list!=null){
- list.clear();
- }
- list.addAll(loadData());
- adapter.setDatalist(list);
- mSwipeRefreshLayout.setRefreshing(false);
- }
- },4000);
- }
- public List<String> loadData(){
- List<String> listdata = new ArrayList<String>();
- for(int i=0;i<20;i++){
- list.add("---->"+i);
- }
- return listdata;
- }
- public List<String> loadNewData(){
- List<String> listdata = new ArrayList<String>();
- for(int i=0;i<20;i++){
- list.add("---xin->"+i);
- }
- return listdata;
- }
- Handler handler = new Handler(){
-
- };
- }
adapter
底部layout
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/load_layout"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:orientation="horizontal"
- android:paddingBottom="12dip"
- android:paddingTop="12dip" >
-
- <ProgressBar
- style="?android:attr/progressBarStyleSmall"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
-
- <TextView
- android:id="@+id/more_data_msg"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="16sp"
- android:layout_marginLeft="10dp"
- android:text="正在加载..." />
- </LinearLayout>
|