SwipeRefreshLayout 结合Recyclervie
2016-07-24 本文已影响1918人
翻身不做咸鱼
先看一下效果图:
GIF.gif
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.administrator.swiperefreshlayout.MainActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
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="wrap_content">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
activity_main布局文件,就是一个SwipeRefreshLayout嵌套一个RecyClerview。
主页面:
public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
@Bind(R.id.recyclerview)
RecyclerView recyclerview;
@Bind(R.id.swipeRefreshLayout)
SwipeRefreshLayout swipeRefreshLayout;
private List<String> mData;
private ItemsAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary);
swipeRefreshLayout.setOnRefreshListener(this);
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this){
protected int getExtraLayoutSpace(RecyclerView.State state) {
return 6000;}
};
//如果可以确定每个item的高度是固定的,设置这个选项可以提高性能
recyclerview.setHasFixedSize(true);
recyclerview.setLayoutManager(linearLayoutManager);
mData = new ArrayList<>();
swipeRefreshLayout.post(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(true);
}
});
loadData(false);
}
private void loadData(final boolean isMore) {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(isMore){
mAdapter.setLoaded();
mData.remove(mData.size() - 1);
mAdapter.notifyItemRemoved(mData.size());
// 去除空白的一条
}
int size=mData.size();
for(int i=size;i<size+5;i++)
{
mData.add("第"+i+"个数据");
}
runOnUiThread(new Runnable() {
@Override
public void run() {
setAdapter();
swipeRefreshLayout.setRefreshing(false);
}
});
}
}).start();
}
private void setAdapter() {
if(mAdapter==null)
{
mAdapter=new ItemsAdapter(this,mData,recyclerview);
recyclerview.setAdapter(mAdapter);
mAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore() {
mData.add(null);
mAdapter.notifyItemInserted(mData.size() - 1);
// 增加空白的一条
loadData(true);
mAdapter.setLoading();
}
});
}
else {
mAdapter.notifyDataSetChanged();
}
}
@Override
public void onRefresh() {
// loadData(false);
refresh();
}
private void refresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
final List<String> newData=new ArrayList<String>();
int index=1;
for(int i=0;i<5;i++)
{
index=i+1;
newData.add("新增第"+index+"个数据");
}
runOnUiThread(new Runnable() {
@Override
public void run() {
mAdapter.addItem(newData);
swipeRefreshLayout.setRefreshing(false);
}
});
}
},1000);
}
}
swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary);
设置动画进度颜色;
额外补充:
1、setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener):设置手势滑动监听器。
2、setProgressBackgroundColor(int colorRes):设置进度圈的背景色。
3、setColorSchemeResources(int… colorResIds):设置进度动画的颜色。
4、setRefreshing(Boolean refreshing):设置组件的刷洗状态。
5、setSize(int size):设置进度圈的大小,只有两个值:DEFAULT、LARGE
swipeRefreshLayout.post(new Runnable() { @Override public void run() { swipeRefreshLayout.setRefreshing(true); } });
第一次进入,设置swipeRefreshLayout的刷新状态显示,要异步实现。
SwipeRefreshLayout.OnRefreshListener接口下有个onRefresh()方法用于实现下拉刷新;
public void onRefresh() {
// loadData(false);
refresh();
}
private void refresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
final List<String> newData=new ArrayList<String>();
int index=1;
for(int i=0;i<5;i++)
{
index=i+1;
newData.add("新增第"+index+"个数据");
}
runOnUiThread(new Runnable() {
@Override
public void run() {
mAdapter.addItem(newData);
swipeRefreshLayout.setRefreshing(false);
}
});
}
},1000);
}
每下拉一次生成5条新的数据。
Adapter:
public class ItemsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private List<String> mData;
private Context context;
private OnLoadMoreListener onLoadMoreListener;
private int lastVisibleItem, totalItemCount;
private boolean loading;
private int visibleThreshold = 1;
private static final int EMPTY_VIEW = 1;
private static final int VIEW_PROG = 2;
private LayoutInflater mLayoutInflater;
public ItemsAdapter(Context context, List<String> data, RecyclerView recyclerview) {
this.context=context;
this.mData=data;
if (recyclerview.getLayoutManager() instanceof LinearLayoutManager) {
final LinearLayoutManager linearLayoutManager= (LinearLayoutManager) recyclerview.getLayoutManager();
recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
totalItemCount=linearLayoutManager.getItemCount();
lastVisibleItem=linearLayoutManager.findLastVisibleItemPosition();
if(!loading && totalItemCount<=(lastVisibleItem+visibleThreshold))
{
if(onLoadMoreListener!=null)
onLoadMoreListener.onLoadMore();
}
}
});
}
}
@Override
public int getItemViewType(int position) {
if(mData.size()==0)
{
return EMPTY_VIEW;
}
return mData.get(position)!=null ? super.getItemViewType(position):VIEW_PROG;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
mLayoutInflater=LayoutInflater.from(context);
if(viewType==EMPTY_VIEW)
{
return new EmptyViewHolder(mLayoutInflater.inflate(R.layout.empty_view,parent,false));
}
else if(viewType==VIEW_PROG)
{
return new ProgressViewHolder(mLayoutInflater.inflate(R.layout.progressbar_item,parent,false));
}
else {
return new ItemHolder(mLayoutInflater.inflate(R.layout.item,parent,false));
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if(holder instanceof ItemHolder)
{
((ItemHolder) holder).mTextView.setText(mData.get(position));
}else if(holder instanceof ProgressViewHolder)
{
((ProgressViewHolder) holder).progressBar.setIndeterminate(true);
}
}
@Override
public int getItemCount() {
return mData.size();
}
public void setLoading() {
loading = true;
}
public void setLoaded() {
loading = false;
}
public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
this.onLoadMoreListener = onLoadMoreListener;
}
//添加数据
public void addItem(List<String> newDatas) {
//mTitles.add(position, data);
//notifyItemInserted(position);
newDatas.addAll(mData);
mData.removeAll(mData);
mData.addAll(newDatas);
notifyDataSetChanged();
}
}
加载更多,用接口的方式定义
public interface OnLoadMoreListener {
void onLoadMore();
}
recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
totalItemCount=linearLayoutManager.getItemCount();
lastVisibleItem=linearLayoutManager.findLastVisibleItemPosition();
if(!loading && totalItemCount<=(lastVisibleItem+visibleThreshold))
{
if(onLoadMoreListener!=null)
onLoadMoreListener.onLoadMore();
}
}
});
public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) { this.onLoadMoreListener = onLoadMoreListener;}
暴露接口给外边调用,
外边调用的加载更多:
mAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore() {
mData.add(null);
mAdapter.notifyItemInserted(mData.size() - 1);
// 增加空白的一条
loadData(true);
mAdapter.setLoading();
}
});