-
Android 中的 XRecyclerview的使用案例
这篇文章主要介绍了Android 中的 XRecyclerview的使用案例,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
前言:
我们在上一篇文章中学到了Recyclerview但是在现实中往往需求不是那么的简单,可能需要多种需求合并起来,例如常见的上下拉刷新,删除 item 还有多种item,有着头部和尾部等等之类的。这次我们久来学习一个RecyclerView的库,XRecyclerView的他集合了众多的功能,下面就让我来讲解一下如何使用。
步骤:
引用 库
创建 控件
下拉刷新
设置上拉下拉刷新的样式
创建头部
设置上下拉的文字提示语
设置刷新的回调
更新完毕
实战过程
彩蛋
我的案例
引用库
compile 'com.jcodecraeer:xrecyclerview:1.5.8'
创建控件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.jcodecraeer.xrecyclerview.XRecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="wrap_content"></com.jcodecraeer.xrecyclerview.XRecyclerView>
</LinearLayout>
设置是否可以上下拉刷新(默认是可以的)
recyclerView.setPullRefreshEnabled(true);
recyclerView.setLoadingMoreEnabled(false);
设置上拉下拉刷新的样式
recyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
recyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
recyclerView.setArrowImageView(R.drawable.iconfont_downgrey);
设置头部
recyclerView
.getDefaultRefreshHeaderView()
.setRefreshTimeVisible(true);
View header = LayoutInflater.from(this).inflate(R.layout.recyclerview_header,(ViewGroup)findViewById(android.R.id.content),false);
recyclerView.addHeaderView(header);
设置上下拉的文字提示语
// recyclerView.getDefaultFootView().setLoadingHint("自定义加载中提示");
recyclerView.getDefaultFootView().setNoMoreHint("自定义加载完毕提示");
设置刷新的回调(onRefrsh为刷新回调,onLoadMore为下拉更新)
recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable(){
public void run() {
InternextData(2);
}
}, 2000);
}
@Override
public void onLoadMore() {
}
});
更新完毕
lists.clear();
lists.addAll( ReadAllLock.arrayDemoFromData(body));
adapter.notifyDataSetChanged();
recyclerView.refreshComplete();
实战过程
默认初级进入页面的时候先加载好数据使我们的网络请求函数,多加上一个参数区分 初始化网络 下拉刷新 下拉添加数据的操作
彩蛋
顺便讲一下一点更新数据的问题 ,使用下面语句来更新的时候我们必须更新我一初始化的数据内存,不能让引用指向另一内存
adapter.notifyDataSetChanged();
我的案例
package com.LY.project.View;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.LY.basemodule.Essential.BaseTemplate.BaseActivity;
import com.LY.project.Adapter.DemoAdapter;
import com.LY.project.Adapter.MyLockAdapter;
import com.LY.project.Controller.LockController;
import com.LY.project.Manager.InterfaceManger;
import com.LY.project.Module.ReadAllLock;
import com.LY.project.R;
import com.LY.project.Utils.RetrofitUtils;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
/**
* Created by jie on 2018/8/17.
*/
public class Demo extends BaseActivity {
List<ReadAllLock> lists = new ArrayList<>();
private XRecyclerView recyclerView;
private DemoAdapter adapter;
@Override
public int getLayoutId() {
return R.layout.demo;
}
@Override
public void initViews() {
recyclerView = findView(R.id.rv);
initR();
}
/**
* 初始化控件
*/
private void initR() {
/*
* 默认为垂直向下的列表格式
*/
LinearLayoutManager layoutmanager = new LinearLayoutManager(getApplication());
/**
* 设置为水平布局格式
*/
// layoutmanager.setOrientation(LinearLayoutManager.HORIZONTAL);
/*
* 瀑布格式
* 第一个参数表示布局的列数
* 第二个参数表示布局的方向,这里我们传入StaggeredGridLayoutManager.VERTICAL,表示布局纵向排列
*/
// StaggeredGridLayoutManager layoutmanager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
//设置RecyclerView 布局
recyclerView.setLayoutManager(layoutmanager);
recyclerView.setPullRefreshEnabled(true);
recyclerView.setLoadingMoreEnabled(false);
recyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
recyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
recyclerView.setArrowImageView(R.drawable.iconfont_downgrey);
recyclerView
.getDefaultRefreshHeaderView()
.setRefreshTimeVisible(true);
View header = LayoutInflater.from(this).inflate(R.layout.recyclerview_header, (ViewGroup)findViewById(android.R.id.content),false);
recyclerView.addHeaderView(header);
// recyclerView.getDefaultFootView().setLoadingHint("自定义加载中提示");
recyclerView.getDefaultFootView().setNoMoreHint("自定义加载完毕提示");
}
/**
* 初始化点击事件
*/
@Override
public void initListener() {
recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable(){
public void run() {
InternextData(2);
}
}, 2000);
}
@Override
public void onLoadMore() {
}
});
}
/**
* 初始化数据
*/
@Override
public void initData() {
InternextData(1);
}
/**
* 获取数据
* @param type tyoe==1 初次获取数据 type == 2 刷新获取数据 type == 3 下拉更新数据
*/
private void InternextData(final int type) {
LockController lockController = new LockController(Demo.this);
List<String> photos = new ArrayList<>();
List<MultipartBody.Part> parts = null;
Map<String, RequestBody> params = new HashMap<>();
params.put("phone", RetrofitUtils.convertToRequestBody("17875305749"));
params.put("password", RetrofitUtils.convertToRequestBody("1234567"));
lockController.ReadAllLock(params, parts, new InterfaceManger.OnRequestListener() {
@Override
public void onSuccess(Object success) {
String body = success.toString();
Log.e("onSuccess:", body);
switch (type){
case 1:
lists = ReadAllLock.arrayDemoFromData(body);
//设置Adapter
adapter = new DemoAdapter(lists);
adapter.setOnItemClickListener(new DemoAdapter.OnItemClickListener() {
@Override
public void onLongClick(int position) {
Toast.makeText(Demo.this, "onLongClick事件 您点击了第:" + position + "个Item", Toast.LENGTH_SHORT).show();
}
@Override
public void onClick(int position) {
Toast.makeText(Demo.this, "onClick事件 您点击了第:" + position + "个Item", Toast.LENGTH_SHORT).show();
}
});
recyclerView.setAdapter(adapter);
recyclerView.refresh();
break;
case 2:
lists.clear();
lists.addAll( ReadAllLock.arrayDemoFromData(body));
adapter.notifyDataSetChanged();
recyclerView.refreshComplete();
break;
default:
break;
}
}
@Override
public void onError(String error) {
showToast(error);
}
@Override
public void onComplete() {
}
});
}
@Override
public void processClick(View v) {
}
}
复制AI辅助
package com.LY.project.View;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.LY.basemodule.Essential.BaseTemplate.BaseActivity;
import com.LY.project.Adapter.DemoAdapter;
import com.LY.project.Adapter.MyLockAdapter;
import com.LY.project.Controller.LockController;
import com.LY.project.Manager.InterfaceManger;
import com.LY.project.Module.ReadAllLock;
import com.LY.project.R;
import com.LY.project.Utils.RetrofitUtils;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
/**
* Created by jie on 2018/8/17.
*/
public class Demo extends BaseActivity {
List<ReadAllLock> lists = new ArrayList<>();
private XRecyclerView recyclerView;
private DemoAdapter adapter;
@Override
public int getLayoutId() {
return R.layout.demo;
}
@Override
public void initViews() {
recyclerView = findView(R.id.rv);
initR();
}
/**
* 初始化控件
*/
private void initR() {
/*
* 默认为垂直向下的列表格式
*/
LinearLayoutManager layoutmanager = new LinearLayoutManager(getApplication());
/**
* 设置为水平布局格式
*/
// layoutmanager.setOrientation(LinearLayoutManager.HORIZONTAL);
/*
* 瀑布格式
* 第一个参数表示布局的列数
* 第二个参数表示布局的方向,这里我们传入StaggeredGridLayoutManager.VERTICAL,表示布局纵向排列
*/
// StaggeredGridLayoutManager layoutmanager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
//设置RecyclerView 布局
recyclerView.setLayoutManager(layoutmanager);
recyclerView.setPullRefreshEnabled(true);
recyclerView.setLoadingMoreEnabled(false);
recyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
recyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
recyclerView.setArrowImageView(R.drawable.iconfont_downgrey);
recyclerView
.getDefaultRefreshHeaderView()
.setRefreshTimeVisible(true);
View header = LayoutInflater.from(this).inflate(R.layout.recyclerview_header, (ViewGroup)findViewById(android.R.id.content),false);
recyclerView.addHeaderView(header);
// recyclerView.getDefaultFootView().setLoadingHint("自定义加载中提示");
recyclerView.getDefaultFootView().setNoMoreHint("自定义加载完毕提示");
}
/**
* 初始化点击事件
*/
@Override
public void initListener() {
recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable(){
public void run() {
InternextData(2);
}
}, 2000);
}
@Override
public void onLoadMore() {
}
});
}
/**
* 初始化数据
*/
@Override
public void initData() {
InternextData(1);
}
/**
* 获取数据
* @param type tyoe==1 初次获取数据 type == 2 刷新获取数据 type == 3 下拉更新数据
*/
private void InternextData(final int type) {
LockController lockController = new LockController(Demo.this);
List<String> photos = new ArrayList<>();
List<MultipartBody.Part> parts = null;
Map<String, RequestBody> params = new HashMap<>();
params.put("phone", RetrofitUtils.convertToRequestBody("17875305749"));
params.put("password", RetrofitUtils.convertToRequestBody("1234567"));
lockController.ReadAllLock(params, parts, new InterfaceManger.OnRequestListener() {
@Override
public void onSuccess(Object success) {
String body = success.toString();
Log.e("onSuccess:", body);
switch (type){
case 1:
lists = ReadAllLock.arrayDemoFromData(body);
//设置Adapter
adapter = new DemoAdapter(lists);
adapter.setOnItemClickListener(new DemoAdapter.OnItemClickListener() {
@Override
public void onLongClick(int position) {
Toast.makeText(Demo.this, "onLongClick事件 您点击了第:" + position + "个Item", Toast.LENGTH_SHORT).show();
}
@Override
public void onClick(int position) {
Toast.makeText(Demo.this, "onClick事件 您点击了第:" + position + "个Item", Toast.LENGTH_SHORT).show();
}
});
recyclerView.setAdapter(adapter);
recyclerView.refresh();
break;
case 2:
lists.clear();
lists.addAll( ReadAllLock.arrayDemoFromData(body));
adapter.notifyDataSetChanged();
recyclerView.refreshComplete();
break;
default:
break;
}
}
@Override
public void onError(String error) {
showToast(error);
}
@Override
public void onComplete() {
}
});
}
@Override
public void processClick(View v) {
}
}
到此这篇关于Android 之 XRecyclerview的使用的文章就介绍到这了,更多相关Android XRecyclerview使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持
原文链接:https://blog.csdn.net/weixin_39460667/article/details/81814030