用最简单简洁的方式做出目标效果
如果有什么不好的地方希望大佬在评论区指出!
本次练习项目是用一个开源的API做了一个图片APP,滑动到底部会触发加载更多
布局方面
创建一个item布局来作为列表,这个布局根据自己的需求来添加,我这里用卡片布局放了一个图片控件
java方面
使用到的依赖
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.android.volley:volley:1.1.1'
创建一个bean类
这个是API那边提供的,然后用插件导入生成的
package com.sakura.testvolley.bean;
import java.util.List;
public class PhotoBean {
private int total;
private int totalHits;
private List<HitsBean> hits;
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getTotalHits() {
return totalHits;
}
public void setTotalHits(int totalHits) {
this.totalHits = totalHits;
}
public List<HitsBean> getHits() {
return hits;
}
public void setHits(List<HitsBean> hits) {
this.hits = hits;
}
public static class HitsBean {
private int id;
private String pageURL;
private String type;
private String tags;
private String previewURL;
private int previewWidth;
private int previewHeight;
private String webformatURL;
private int webformatWidth;
private int webformatHeight;
private String largeImageURL;
private int imageWidth;
private int imageHeight;
private int imageSize;
private int views;
private int downloads;
private int favorites;
private int likes;
private int comments;
private int user_id;
private String user;
private String userImageURL;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPageURL() {
return pageURL;
}
public void setPageURL(String pageURL) {
this.pageURL = pageURL;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getTags() {
return tags;
}
public void setTags(String tags) {
this.tags = tags;
}
public String getPreviewURL() {
return previewURL;
}
public void setPreviewURL(String previewURL) {
this.previewURL = previewURL;
}
public int getPreviewWidth() {
return previewWidth;
}
public void setPreviewWidth(int previewWidth) {
this.previewWidth = previewWidth;
}
public int getPreviewHeight() {
return previewHeight;
}
public void setPreviewHeight(int previewHeight) {
this.previewHeight = previewHeight;
}
public String getWebformatURL() {
return webformatURL;
}
public void setWebformatURL(String webformatURL) {
this.webformatURL = webformatURL;
}
public int getWebformatWidth() {
return webformatWidth;
}
public void setWebformatWidth(int webformatWidth) {
this.webformatWidth = webformatWidth;
}
public int getWebformatHeight() {
return webformatHeight;
}
public void setWebformatHeight(int webformatHeight) {
this.webformatHeight = webformatHeight;
}
public String getLargeImageURL() {
return largeImageURL;
}
public void setLargeImageURL(String largeImageURL) {
this.largeImageURL = largeImageURL;
}
public int getImageWidth() {
return imageWidth;
}
public void setImageWidth(int imageWidth) {
this.imageWidth = imageWidth;
}
public int getImageHeight() {
return imageHeight;
}
public void setImageHeight(int imageHeight) {
this.imageHeight = imageHeight;
}
public int getImageSize() {
return imageSize;
}
public void setImageSize(int imageSize) {
this.imageSize = imageSize;
}
public int getViews() {
return views;
}
public void setViews(int views) {
this.views = views;
}
public int getDownloads() {
return downloads;
}
public void setDownloads(int downloads) {
this.downloads = downloads;
}
public int getFavorites() {
return favorites;
}
public void setFavorites(int favorites) {
this.favorites = favorites;
}
public int getLikes() {
return likes;
}
public void setLikes(int likes) {
this.likes = likes;
}
public int getComments() {
return comments;
}
public void setComments(int comments) {
this.comments = comments;
}
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getUserImageURL() {
return userImageURL;
}
public void setUserImageURL(String userImageURL) {
this.userImageURL = userImageURL;
}
}
}
创建一个adapter
package com.sakura.testvolley.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.sakura.testvolley.R;
import com.sakura.testvolley.bean.PhotoBean;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
//继承自RecyclerView.Adapter
List<PhotoBean.HitsBean> mLis; //列表
Context context;
public MyAdapter(List<PhotoBean.HitsBean> mLis, Context context) {
//构造方法,用于从main那边传参数过来
this.mLis = mLis;
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//匹配布局
return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_layout, parent, false));
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
//事件写在这里
//glide加载图片
Glide.with(context)
.load(mLis.get(position).getWebformatURL())
.into(holder.imageView);
}
@Override
public int getItemCount() {
//获取列表里面的数据个数,如果为0的话就return0,否则return全部
if (mLis.size() == 0) {
return 0;
}
return mLis.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
//定义控件
private ImageView imageView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
//绑定控件
imageView = itemView.findViewById(R.id.imageView_item);
}
}
}
main方法
package com.sakura.testvolley;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import com.sakura.testvolley.adapter.MyAdapter;
import com.sakura.testvolley.bean.PhotoBean;
import com.sakura.testvolley.volley.MyVolley;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
MyAdapter adapter;//适配器
MyVolley volley;//解析类
List<PhotoBean.HitsBean> mList = new ArrayList<>();//list列表
PhotoBean bean; //bean类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
into();//方法
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//设置列表显示类型,有单列,多列,瀑布流
adapter = new MyAdapter(mList, this);
//设置适配器 传入list和当前context,数据传入到adapter里面,adapter那边有构造方法
volley = new MyVolley(this, mList, adapter);
//设置解析 原理和adapter一样 ,不过传递的多了一个adapter
recyclerView.setAdapter(adapter);
//设置适配器
volley.getVolley();
//调用解析方法
loading();//加载更多的方法
}
private void loading() {
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
//获取管理器 上面用的什么类型的布局管理器,这里就写什么布局管理器
int lastItem = manager.findLastVisibleItemPosition();
//获取最后一个数据的位置
if (newState == RecyclerView.SCROLL_STATE_IDLE && adapter.getItemCount() >= lastItem) {
//如果上面的条件成立,那么就调用解析类方法刷新一次,获取更多的数据,我这里不是最优的判断,但是经测试能用且没出什么问题
volley.getVolley();
}
}
@Override
//这个不用管
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
}
});
}
private void into() { //这里是into方法
recyclerView = findViewById(R.id.recyclerView);
}
}
创建一个volley解析类
package com.sakura.testvolley.volley;
import android.content.Context;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.sakura.testvolley.adapter.MyAdapter;
import com.sakura.testvolley.bean.PhotoBean;
import java.util.List;
public class MyVolley {
Context context;
List<PhotoBean.HitsBean> mList;
MyAdapter myAdapter;
PhotoBean bean;
Gson gson;
int page = 1;//这个用于加页数
public MyVolley(Context context, List<PhotoBean.HitsBean> mList, MyAdapter myAdapter) {
//构造方法用于main那边传递参数
this.context = context;
this.mList = mList;
this.myAdapter = myAdapter;
}
public void getVolley() {
++page;//每次调用的时候自增1
String url = "https://pixabay.com/api/?key=14409832-7145bfafbcc12bb115e09ff42&q=&keyWord&image_type=photo&pretty=true&orientation=vertical&page=" + page + "&per_page=10";
RequestQueue requestQueue = Volley.newRequestQueue(context);
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
gson = new Gson();
bean = gson.fromJson(response, PhotoBean.class);
//gson解析到bean里面
mList.addAll(bean.getHits());
//把bean里面的数据添加到mlist里面
myAdapter.notifyItemInserted(myAdapter.getItemCount());
//每次添加数据后都刷新一下适配器
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(stringRequest);
}
}