在Android的开发过程中数据的展示是非常普遍的,从一开始的LIstView到RecyclerView一直都是我们最长用到的控件,而这些列表的数据的不是固定死的,而且为了不让App加载数据的速度太慢用户等太久,所以一般都会使用分页加载,当用户想查看跟多的时候,可以自行上拉加载更多,这样可以减少加载数据的时间同时可以让用户不需要浪费流量去加载他不需要的数据。所以光知道展示数据还不行,还要分类加载数据。
在LIstView和RecyclerView的上拉加载使用会比较麻烦,而XRecyclerView非常方便的决解了这些问题,都已经封装好了,我们只需要调用方法就OK了。下面是XRecyclerView的使用。
Okutils的使用
public class Okutils {
public static Okutils okutils;
public static OkHttpClient client;
public static Handler handler;
public static OkHttpClient getClient(){
return client;
}
public static Handler getHandler(){
return handler;
}
public static Okutils getOkutils(){
if (okutils == null){
synchronized (Okutils.class){
if (okutils == null){
okutils = new Okutils();
}
}
}
return okutils;
}
public Okutils(){
//TODO 网络拦截器
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
handler = new Handler();
}
public void doget(String url, Callback callback){
Request request = new Request.Builder().url(url).build();
Call call = client.newCall(request);
call.enqueue(callback);
}
### OkCallback 的使用 ##
public abstract class OkCallback implements Callback{
public abstract void success(String json);
public abstract void failed(String json);
@Override
public void onFailure(Call call, final IOException e) {
Okutils.getHandler().post(new Runnable() {
@Override
public void run() {
failed(e.getMessage());
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String json = response.body().string();
Okutils.getHandler().post(new Runnable() {
@Override
public void run() {
success(json);
}
});
}
}
### M层 I...Task接口 ##
public interface IPhoneTask {
void getListM(String url, OkCallback callback);
}
### M 层 PhoneTask ##
public class PhoneTask implements IPhoneTask{
@Override
public void getListM(String url, OkCallback callback) {
Okutils.getOkutils().doget(url,callback);
}
}
### P 层 I...Presenter 的接口 ##
public interface IPhonePresenter extends BasePresenter{
void getListP(String key,int page);
}
### P层 Presenter类 ##
public class PhonePresenter implements IPhonePresenter{
//注入M层
IPhoneTask phoneTask;
IPhoneView phoneView;
public PhonePresenter(ErActivity activity) {
phoneTask = new PhoneTask();
phoneView = activity;
}
@Override
public void getListP(String key,int page) {
//key的中文编码
String k = URLEncoder.encode(key);
String url="http://www.zhaoapi.cn/product/searchProducts?keywords="+k+"&page="+page+"&sort=0";
phoneTask.getListM(url, new OkCallback() {
@Override
public void success(String json) {
//Gson解析
Gson gson = new Gson();
RootData data = gson.fromJson(json, RootData.class);
phoneView.seccess(data.data);
}
@Override
public void failed(String json) {
phoneView.failed(json);
}
});
}
@Override
public void onDestroy() {
phoneView = null;
}
}
### V层 IPhoneView 接口 ##
public interface IPhoneView extends BasePhoneView<PhonePresenter>{
void seccess(List<Phones> list);
void failed(String string);
}
## Holder类 ##
public class PhoneHolder extends RecyclerView.ViewHolder{
public ImageView phone_img;
public TextView phone_tilet,phone_price;
public PhoneHolder(View itemView) {
super(itemView);
phone_img = itemView.findViewById(R.id.phone_img);
phone_tilet = itemView.findViewById(R.id.phone_tilet);
phone_price = itemView.findViewById(R.id.phone_price);
}
}
## V层的 Adapter适配器 ##
public class PhoneAdapter extends RecyclerView.Adapter<PhoneHolder>{
private List<Phones> list;
private Context context;
//TODO 声明自定义接口listener
ItemListener listener;
//实例化List
public PhoneAdapter() {
this.list = new ArrayList<>();
}
public void setListener(ItemListener listener) {
this.listener = listener;
}
//TODO 自定义set方法
public void addData(List<Phones> data){
list.addAll(data);
//刷新适配器
notifyDataSetChanged();
}
//TODO 清空
public void clearData(){
this.list.clear();
notifyDataSetChanged();
}
@NonNull
@Override
public PhoneHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new PhoneHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.phone_item,parent,false));
}
@Override
public void onBindViewHolder(@NonNull PhoneHolder holder, int position) {
final Phones p = list.get(position);
holder.phone_tilet.setText(list.get(position).title);
holder.phone_price.setText(list.get(position).price);
//图片路径切割
String url = p.images.split("\\|")[0];
//加载图片
ImageLoader.getInstance().displayImage(url,holder.phone_img, MyAppliaction.getOptions());
//TODO 自定义设置监听
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.ItemClick(p);
}
});
}
@Override
public int getItemCount() {
return list == null ? 0 : list.size();
}
//TODO 自定义条目点击事件的接口
public interface ItemListener{
void ItemClick(Phones phones);
}
}
### ErActivity 主页面类 ##
public class ErActivity extends AppCompatActivity implements IPhoneView{
//注入P层的类
PhonePresenter presenter;
XRecyclerView xRecyclerView;
int page = 0;
String key;
PhoneAdapter adapter;
//给输入框赋值
EditText er_edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_er);
//获取输入框的ID
er_edit = findViewById(R.id.er_edit);
// 1. 接受从mainactivity传来的key
Intent it = getIntent();
key = it.getStringExtra("key");
//给输入框赋值
er_edit.setText(key);
//初始化
presenter = new PhonePresenter(this);
presenter.getListP(key,page);
initView();
}
private void initView() {
//获取资源ID
xRecyclerView = findViewById(R.id.xrecycler_view);
//设置允许上拉刷新
xRecyclerView.setPullRefreshEnabled(true);
//设置允许下拉加载
xRecyclerView.setLoadingMoreEnabled(true);
//设置上拉下拉监听
xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() { //上拉刷新
adapter.clearData();
presenter.getListP(key,page);
Log.i("Tag",page+"");
}
@Override
public void onLoadMore() { //下拉加载
page++;
presenter.getListP(key,page);
}
});
//设置布局
xRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//添加自条目之间的分割线
xRecyclerView.addItemDecoration(new DividerItemDecoration(this, OrientationHelper.VERTICAL));
//创建适配器
adapter = new PhoneAdapter();
xRecyclerView.setAdapter(adapter);
//TODO 自定义子条目的监听事件
adapter.setListener(new PhoneAdapter.ItemListener() {
@Override
public void ItemClick(Phones phones) {
Intent intent = new Intent(ErActivity.this,SanActivity.class);
//传商品的id
intent.putExtra("pid",phones.pid);
//传商品的images图片
intent.putExtra("images",phones.images);
startActivity(intent);
}
});
}
//成功
@Override
public void seccess(List<Phones> list) {
//添加数据
adapter.addData(list);
//关闭上拉刷新
xRecyclerView.refreshComplete();
//关闭下拉加载
xRecyclerView.loadMoreComplete();
}
//失败
@Override
public void failed(String string) {
xRecyclerView.refreshComplete();
xRecyclerView.loadMoreComplete();
}
@Override
public void setP(PhonePresenter P) {
this.presenter = P;
}
}
(在java文件中所有要注意的事项都已经注释好了,不管是下拉的方法,上拉的方法,加载的逻辑,还有它的适配Adapter都已经写好了,如果有什么问题都可以留言,基本每天都会查看的)
谢谢您的查看!!!