Android标准控件实现下拉刷新,数据获取方式:Bmob云存储。
01 效果图
02 Layout
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
03 关键代码
public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
private Context mContext;
SwipeRefreshLayout mSwipeRefreshLayout;
RecyclerView mRecyclerView;
TweetAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
initWidget();
initData();
}
private void initData() {
mAdapter = new TweetAdapter(this, null);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setLayoutManager(getLayoutManager());
mSwipeRefreshLayout.setRefreshing(true);
getDataFromBmob(10);
}
private void initWidget() {
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
mSwipeRefreshLayout.setProgressBackgroundColorSchemeResource(R.color.swipe_background_color);
mSwipeRefreshLayout.setColorSchemeResources(R.color.swipe_color_1,
R.color.swipe_color_2,
R.color.swipe_color_3,
R.color.swipe_color_4);
mSwipeRefreshLayout.setProgressViewEndTarget(true, 200);
//mSwipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE);
//mSwipeRefreshLayout.setPadding(20, 20, 20, 20);
//mSwipeRefreshLayout.setProgressViewOffset(true, 100, 200);
//mSwipeRefreshLayout.setDistanceToTriggerSync(50);
mSwipeRefreshLayout.setOnRefreshListener(this);
}
private RecyclerView.LayoutManager getLayoutManager() {
return new LinearLayoutManager(mContext);
}
@Override
public void onRefresh() {
getDataFromBmob(0);
}
// 获取Bmob云存储数据
private void getDataFromBmob(int newSkip){
BmobQuery<Tweet> query = new BmobQuery<>();
query.include("author");
query.order("-pubDate");
query.setSkip(newSkip);
query.setLimit(10);
query.findObjects(new FindListener<Tweet>() {
@Override
public void done(final List<Tweet> list, BmobException e) {
if(e == null && list != null){
mAdapter.clear();
mAdapter.addAll(list);
mSwipeRefreshLayout.setRefreshing(false);
}
}
});
}
}
04 源码
05 数据来源
真实数据来自开源中国。