使用谷歌提供的SwipeRefreshLayout下拉控件进行下拉刷新的实现数据的刷新
package com.loaderman.swiperefreshdemo; import android.os.Bundle; import android.os.Handler; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import java.util.ArrayList; import java.util.List; /** * design库中包含V7包 V7包中包含V4包 */ public class MainActivity extends AppCompatActivity { private SwipeRefreshLayout swip_refresh; private RecyclerView recyclerView; private List<String> titleList; private MyAdapter adapter; private int page = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initEvent(); initData(); } private void initData() { swip_refresh.post(new Runnable() { @Override public void run() { swip_refresh.setRefreshing(true); } }); getData(1, "Load"); } private void initEvent() { swip_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { Log.e("main", "refresh"); //titleList.clear(); getData(2, "Refresh"); } }); } private void initView() { swip_refresh = (SwipeRefreshLayout) findViewById(R.id.swip_refresh); recyclerView = (RecyclerView) findViewById(R.id.recyclerView); recyclerView.setLayoutManager(new LinearLayoutManager(this)); titleList = new ArrayList<>(); adapter = new MyAdapter(titleList); recyclerView.setAdapter(adapter); //设置刷新时动画的颜色,可以设置4个 swip_refresh.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light); } private void getData(final int page, final String content) { new Handler().postDelayed(new Runnable() { @Override public void run() { int startPosition = 10 * (page - 1); int endPosition = 10 * page; for (int i = startPosition; i < endPosition; i++) { titleList.add(0,content + i); } adapter.notifyDataSetChanged(); swip_refresh.setRefreshing(false); } }, 3000); } }
package com.loaderman.swiperefreshdemo; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.List; public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> { private List<String> list; public MyAdapter(List<String> list) { this.list = list; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.content_main, parent, false); return new MyViewHolder(view); } @Override public void onBindViewHolder(MyViewHolder holder, int position) { holder.txt_item.setText(list.get(position)); } @Override public int getItemCount() { return list == null ? 0 : list.size(); } public static class MyViewHolder extends RecyclerView.ViewHolder { TextView txt_item; public MyViewHolder(View itemView) { super(itemView); txt_item = (TextView) itemView.findViewById(R.id.txt_item); } } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/swip_refresh" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v7.widget.RecyclerView> </android.support.v4.widget.SwipeRefreshLayout>
content_main.xml
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/txt_item" android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center"/>
效果图:
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!