RecyclerView 没有为我们提供刷新加载功能,这时我们只能自己封装或者去网上找
一些依赖来帮助我们去实现效果,这里为大家提供了用依赖来实现的发。
首先需要添加依赖:
compile 'com.wuxiaolong.pullloadmorerecyclerview:library:1.0.4'
添加完依赖之后可以看一下控件
<com.wuxiaolong.pullloadmorerecyclerview.PullLoadMoreRecyclerView
android:id="@+id/pullloadmorerecyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/topBar"
>
com.wuxiaolong.pullloadmorerecyclerview.PullLoadMoreRecyclerView>
这个控件是对RecyclerView的一个封装其中包含着上拉加载下拉刷新功能
可以看一下API的一些调用,在这里我是在Fragment中做的
public class Fragment_A extends Fragment {
private int pager=1;
private View view;
private ViewGroup parent;
private MRecyclerViewAdapter adapter;
private Handler handler=new Handler(){
private ArrayList dataBeen;
private DataBeans dataBeans;
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what==0){
String json= (String) msg.obj;
Gson gson=new Gson();
dataBeans = gson.fromJson(json, DataBeans.class);
if(pager==1){
dataBeen = new ArrayList<>();
}
/**
* 通过jsonBean中的集合,循环放入新的集合
*/
for (int i=0;itrue);
recyclerview.setPushRefreshEnable(true);
recyclerview.setOnPullLoadMoreListener(new PullLoadMoreRecyclerView.PullLoadMoreListener() {
private ArrayList list;
@Override
public void onRefresh() {
//刷新
handler.postDelayed(new Runnable() {
@Override
public void run() {
recyclerview.setPullLoadMoreCompleted();
Toast.makeText(getActivity(), "刷新成功", Toast.LENGTH_SHORT).show();
}
},2000);
}
@Override
public void onLoadMore() {
//加载
pager++;
getData();
handler.postDelayed(new Runnable() {
@Override
public void run() {
recyclerview.setPullLoadMoreCompleted();
Toast.makeText(getActivity(), "加载成功", Toast.LENGTH_SHORT).show();
}
},2000);
}
});
}
}
};
private PullLoadMoreRecyclerView recyclerview;
private boolean net;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if(view==null){
view = View.inflate(getActivity(), R.layout.fragment_a,null);
}
parent = (ViewGroup) view.getParent();
if(parent!=null){
parent.removeView(view);
}
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getid();
net = NetInfo.netinfo(getActivity());
if(net==false){
Toast.makeText(getActivity(), "请检查网络是否连接", Toast.LENGTH_SHORT).show();
return;
}else{
getData();
}
//随便保存一些数据放入sd卡
try {
String fileName = "来呀! ";
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/我刚刚创建/";
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
FileOutputStream fos = new FileOutputStream(path + fileName);
fos.write("lao shi luan ma".getBytes());
fos.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 将handler置空防止内存泄漏,OOM
*/
@Override
public void onDestroy() {
super.onDestroy();
handler.removeCallbacksAndMessages(null);
}
public void getid() {
recyclerview = (PullLoadMoreRecyclerView) view.findViewById(R.id.pullloadmorerecyclerview);
}
public void getData(){
String url="http://www.yulin520.com/a2a/forum/allTypeList?sign=376C5BFC22179A1B8FF3A86D4588B29F&pageSize=10&ts=1877785007&forumType=0&page="+pager;
OkHttp3Utils.doGet(url, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String json= response.body().string();
Log.e("json",json.toString());
Message message=new Message();
message.what=0;
message.obj=json;
handler.sendMessage(message);
}
});
}
public void getInfo(List list){
if(adapter==null){
adapter = new MRecyclerViewAdapter(getActivity(),list);
recyclerview.setAdapter(adapter);
}else{
adapter.notifyDataSetChanged();
}
}
}
适配器
public class MRecyclerViewAdapter extends RecyclerView.Adapter<MRecyclerViewAdapter.ViewHolder>{
private Context context;
private List list;
public MRecyclerViewAdapter(Context context, List list) {
this.context = context;
this.list = list;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(context).inflate(R.layout.recycleritem,null);
ObjectAnimator anim = ObjectAnimator.ofFloat(view,"rotationX", 0f, 360f);
// 动画的持续时间,执行多久?
anim.setDuration(1000);
anim.start();
ViewHolder viewHolder=new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Picasso.with(context).load(list.get(position).getHeadImg()).resize(200,230).into(holder.ImageView);
holder.title.setText(list.get(position).getTitle());
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView title;
private ImageView ImageView;
//找控件
public ViewHolder(View itemView) {
super(itemView);
ImageView = (ImageView) itemView.findViewById(R.id.item_image);
title = (TextView) itemView.findViewById(R.id.item_title);
}
}
}
判断网络是否连接
public class NetInfo {
private static NetworkInfo networkInfo;
public static boolean netinfo(Context context){
ConnectivityManager connect= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
networkInfo = connect.getActiveNetworkInfo();
if(networkInfo!=null){
return true;
}
return false;
};
}
这里我没有为RecyclerView添加分割线如果大家想要添加一些分割线的话可以看一下我的博客