一.SwipeRefreshLayout实现下拉刷新
1.方法API:
setOnRefreshListener(OnRefreshListener):添加下拉刷新监听器
setRefreshing(boolean):显示或者隐藏刷新进度条
isRefreshing():检查是否处于刷新状态
setColorSchemeResources():设置进度条的颜色主题,最多设置四种,以前的setColorScheme()方法已经弃用了。
2.简单使用:
<?xmlversionxmlversion="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:androidLinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"android:layout_width="match_parent"
android:layout_height="match_parent">
<includelayoutincludelayout="@layout/common_top_bar_layout"/>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/demo_swiperefreshlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/demo_recycler"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
></android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
Java:
原理:是一个刷新布局,来自兼容包v4可以运行在低版本,控件如果想要支持下拉刷新,只要使用当前布局包裹
setColorSchemeColors:修改旋转颜色,可以添加多种颜色
setRefreshing: 是否显示loading状态
setOnRefreshListener:下拉刷新监听
public class MainActivity extends AppCompatActivity {
private SwipeRefreshLayout swipeRefreshLayout;
protected void onCreate(Bundle savedInstanceState) {
·····
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);
//设置旋转的颜色效果
swipeRefreshLayout.setColorSchemeColors(Color.GREEN, Color.YELLOW, Color.RED);
//设置下拉刷新的监听器
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
public void onRefresh() {
requestData();
}
});
}
private void requestData() {
new Handler().postDelayed(new Runnable() {//延时加载
public void run() {
String json = "{name:小米}";
TextView text = (TextView) findViewById(R.id.text);
text.setText(json);
swipeRefreshLayout.setRefreshing(false);//关闭显示
}
}, );
}
二.加载更多——上滑加载数据
思路:
1.滚动到底部 getItemCount()-2 ==bottomPosition
2.不处理滚动中
OnScrollListener监听滚动加载的监听器
int dy 滚动距离,向上滚动为正
layoutManager.findLastVisibleItemPosition获取处于底部数据的下标
dy>0与isLoading 都是用来控件灵敏度
public class HomeFragment extends Fragment {
private SwipeRefreshLayout swipeRefreshLayout;
private RecyclerView recyclerView;
private AppAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//有一个显示网络状态
//StateLayout stateLayout = new StateLayout(container.getContext());
swipeRefreshLayout = new SwipeRefreshLayout(container.getContext());
//设置颜色
swipeRefreshLayout.setColorSchemeColors(Color.GREEN);
//设置下拉监听
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
HttpConnect.get(ApiUrls.HOME+"?index=0", callback);
}
});
recyclerView = new RecyclerView(container.getContext());
//设置排列规则
recyclerView.setLayoutManager(new LinearLayoutManager(container.getContext()));
//预先设置了Noemal视图,但是normal视图没有内容 stateLayout.addNormalView(recyclerView);
swipeRefreshLayout.addView(recyclerView);
//发送请求给服务器
HttpConnect.get(ApiUrls.HOME+"?index=0",callback);
return swipeRefreshLayout;
}
DefaultCallBack callback = new DefaultCallBack() {
@Override
public void onStart(int what) {
swipeRefreshLayout.setRefreshing(true);
}
@Override
public void onFinish(int what) {
swipeRefreshLayout.setRefreshing(false);
}
//拿到数据
protected void createView(String json) {
HomeWebInfo info = new Gson().fromJson(json, HomeWebInfo.class);
//创建控件,设置适配器
adapter=new AppAdapter(info.list);
recyclerView.setAdapter(adapter);
//上滑加载数据
addLoadMoreList();
}
};
private void addLoadMoreList(){
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener(){
private boolean isLoading = false;
//int dy 上下滑动的距离 +数代表往上滑动
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
//判断 是否是排列布局
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
//判断是否是线性排列,并且是否滑动距离
if(layoutManager instanceof LinearLayoutManager && dy> ){
//当前是列表
int total = adapter.getItemCount();
int lastPosition = total-;
//获取rv 的bottom条目
int currLastPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
if(currLastPosition==lastPosition&&!isLoading){ }
isLoading=true;
Toast.makeText(MyApp.getContext(),"加载更多...",Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(MyApp.getContext(), "加载成功", Toast.LENGTH_SHORT).show();
isLoading=false;
}
},);
}
}
});
}
}