您的位置:首页 > 其它

SwipeRefreshLayout的下拉刷新和上拉加载

2017-06-30 11:18 148 查看

SwipeRefreshLayout的下拉刷新和上拉加载

随着Recycleview的普遍使用,上拉刷新和上拉加载就成为开发者不可绕过的一个坎,而在所有的刷新控件中,swipeRefreshLayout独占鳌头,不过swipeRefreshLayout只给出了下拉刷新,确实没有给出上拉加载的方法,需要开发者根据自己的实际要求去自己定义,话不多说,下面就是我在开发中实现的上拉加载与下拉刷新


布局的使用

<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/sr_user"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/utc"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>


下拉刷新

mSrUser.setProgressBackgroundColorSchemeResource(android.R.color.white);
mSrUser.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);
mSrUser.setProgressViewOffset(false, 0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources().getDisplayMetrics()));
mGridLayout = new GridLayoutManager(getContext(), 2);
mGridLayout.setOrientation(OrientationHelper.VERTICAL);
mUtc.setLayoutManager(mGridLayout);
//添加分隔线
//mUtc.addItemDecoration(new AdvanceDecoration(getContext(), OrientationHelper.VERTICAL));
GridltemDecoration decoration = new GridltemDecoration(0, getContext());
mUtc.addItemDecoration(decoration);
mAdapter = new CardAdapter();
mUtc.setAdapter(mAdapter);
mSrUser.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
position = 1;
getData(1);
mSrUser.setRefreshing(false);//刷新完毕!
isloading = false;
currentPage = 1;
}
}, 500);
}
});


上拉加载

## mUtc自然就是Recycleview##
mUtc.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int lastVisibleItemPosition = mGridLayout.findLastVisibleItemPosition();
Log.v("lastVisibleItemPosition", lastVisibleItemPosition + "");
Log.v("mAdapter.getItemCount()", mAdapter.getItemCount() + "");
if (lastVisibleItemPosition + 1 == mAdapter.getItemCount()) {
boolean isRefreshing = mSrUser.isRefreshing();
if (isRefreshing) {
mAdapter.notifyItemRemoved(mAdapter.getItemCount());
return;
}
Log.v("isloading", isloading + "");
if (isloading && dy > 0) {
//Toast.makeText(getContext(), "没有更多数据了", Toast.LENGTH_SHORT).show();
}
//搜索之后,关键字为空时,上拉加载
System.out.println("isloadiong====" + isloading);
if (!isloading) {
Log.v("isloading2", isloading + "");
Log.v("sumpageid", sumpageid.get(0) + "");
isloading = true;
currentPage++;
if (currentPage > sumpageid.get(0)) {
currentPage = sumpageid.get(0);
return;
}
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
getData(currentPage);
Log.v("currentpag++", currentPage + "");
isloading = false;
position=2;
}
}, 1000);
}
}
}
});


重点,加载数据时的用法`

@Override
public void onResponse(String response, int id) {
mTopicSearchWeiboEntity = JSON.parseObject(response, TopicSearchWeiboEntity.class);
记录总页数
sumpageid.clear();
sumpageid.add(mTopicSearchWeiboEntity.getData().getPageTotal());
if (mTopicSearchWeiboEntity.getState() == 0) {
stateLayout.showContent();
mRlNetStates.setVisibility(View.GONE);
list.clear();
newlist.clear();
list.addAll(mTopicSearchWeiboEntity.getData().getItems());
newlist.addAll(list);
if (position == 1) {
mAdapter.addItem(newlist);
} else {
mAdapter.addMoreItem(newlist);
}
} else if (mTopicSearchWeiboEntity.getState() == 3) {
stateLayout.showEmpty();
} else {
stateLayout.showError();
}
}


当获取数据后加载数据,主要的两行代码
list.clear();
newlist.clear();
list.addAll(mTopicSearchWeiboEntity.getData().getItems());
newlist.addAll(list);
if (position == 1) {
mAdapter.addItem(newlist);
} else {
mAdapter.addMoreItem(newlist);
}


addItem方法和addMoreItem方法在Adapter中的实现

//添加数据
public void addItem(List<TopicSearchWeiboEntity.DataBean.ItemsBean> newDatas) {
list.clear();
newDatas.addAll(list);
list.removeAll(list);
list.addAll(newDatas);
notifyDataSetChanged();
}

public void addMoreItem(List<TopicSearchWeiboEntity.DataBean.ItemsBean> newDatas) {
list.addAll(newDatas);
notifyDataSetChanged();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐