1.先写布局文件

 <android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/refresh"
android:layout_width="match_parent"
android:layout_height="wrap_content"> <android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/> </android.support.v4.widget.SwipeRefreshLayout>

2.mainactivity

public class MainActivity extends AppCompatActivity {
private OkHttpHelper httpHelper =OkHttpHelper.getInstance();
private SwipeRefreshLayout swipeRefreshLayout;
@ViewInject(R.id.tv)
private TextView tv; private RecyclerView mRecyclerView;
private ArrayList<String> mDatas=new ArrayList<>();
private MyAdapter myAdapter; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = findViewById(R.id.my_recycler_view);
initDatas();
myAdapter =new MyAdapter(mDatas);
mRecyclerView.setAdapter(myAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//mRecyclerView.setLayoutManager(new GridLayoutManager(this,4));
// mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.HORIZONTAL));
//配置分割线
mRecyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL_LIST));
//事件监听 int size =mDatas.size();
//添加数据到头部
myAdapter.addData(size,"adddddddddd"); //删除数据
myAdapter.removeData(); swipeRefreshLayout = findViewById(R.id.refresh);
initRefreshLayout();
} private void initRefreshLayout(){
//转的圈圈的颜色
swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary); swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
Log.d(TAG, "onRefresh: "+swipeRefreshLayout.isRefreshing());
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
for(int i=;i<=;i++){
myAdapter.addData(i,"new"+i);
}
myAdapter.notifyItemRangeChanged(,); mRecyclerView.scrollToPosition();
//刷新完成,必须调用,否则会一直是圈圈转转的情况
swipeRefreshLayout.setRefreshing(false);
}
},);
}
});
} private static final String TAG = "MainActivity";
private void initDatas(){
mDatas.add("aa");
mDatas.add("cv");
mDatas.add("cv1");
mDatas.add("cv112");
mDatas.add("cv112");
mDatas.add("cv112");
mDatas.add("cv112");
mDatas.add("cv112");
mDatas.add("cv112");
mDatas.add("cv112");
mDatas.add("cv112");
mDatas.add("cv112");
mDatas.add("cv112");
mDatas.add("cv112");
} }
05-11 17:57