Android EndlessRecycleViewScrollListener不显示RecycleView上的所有项
我在数据库中有24行,从1到24,但当我尝试在RecycleView上显示项时,EndlesRecycleWebScrollListener类的问题变成了随机项 截图 绿色圆圈=#20 红色圆圈=#11 为什么是11而不是21 我要显示从#1到#24的项目 后活性 后萨达普特Android EndlessRecycleViewScrollListener不显示RecycleView上的所有项,android,android-recyclerview,Android,Android Recyclerview,我在数据库中有24行,从1到24,但当我尝试在RecycleView上显示项时,EndlesRecycleWebScrollListener类的问题变成了随机项 截图 绿色圆圈=#20 红色圆圈=#11 为什么是11而不是21 我要显示从#1到#24的项目 后活性 后萨达普特 public类PostsAdapter扩展了RecyclerView.Adapter{ 语境; 列出后模型; int lastCheckedPosition=-1; 公共PostsAdapter(上下文、列表postsm
public类PostsAdapter扩展了RecyclerView.Adapter{
语境;
列出后模型;
int lastCheckedPosition=-1;
公共PostsAdapter(上下文、列表postsmodel){
this.context=上下文;
this.postsModels=postsModels;
}
@非空
@凌驾
public posts onCreateViewHolder(@NonNull ViewGroup ViewGroup,int i){
View View=LayoutInflater.from(viewGroup.getContext()).flate(R.layout.row_posts,viewGroup,false);
posts viewHolder=新帖子(视图);
返回视图持有者;
}
@凌驾
public void onBindViewHolder(@NonNull posts posts,int i){
PostsModels列表=PostsModels.get(i);
//定影
Picasso.get().load(“http://graph.facebook.com/“+list.user_id+”/picture?type=large“).into(posts.img_profile);
//固定词
posts.txt_word.setText(list.word);
//设置id
posts.txt_id.setText(“#”+list.id);
//集合描述
posts.txt_description.setText(list.description);
//如果检查
如果(lastCheckedPosition==i){
posts.linear_layout.setBackgroundColor(context.getResources().getColor(R.color.posts_item_selected_1));
posts.frame_pink.setVisibility(View.VISIBLE);
}否则{
posts.linear_layout.setBackgroundColor(0x000000000);
posts.frame_pink.setVisibility(视图.不可见);
}
}
@凌驾
public int getItemCount(){
返回postsModels.size();
}
类文章扩展了RecyclerView.ViewHolder{
@BindView(R.id.linear_布局)
线性布局线性布局;
@BindView(R.id.frame_粉色)
框架布局框架为粉红色;
@BindView(R.id.img_剖面)
CircleImageView img_剖面图;
@BindView(R.id.txt\u单词)
text查看字体txt_单词;
@BindView(R.id.txt\u id)
TextViewFont txt_id;
@BindView(R.id.txt_说明)
TextViewFont txt_描述;
@OnClick(R.id.linear\u布局)
公共void onClick(){
lastCheckedPosition=getAdapterPosition();
notifyDataSetChanged();
}
公共职位(视图){
超级(视图);
ButterKnife.bind(这个,视图);
}
}
}
EndlessRecycleViewScrollListener
公共抽象类endlessRecycleWebScrollListener扩展了RecycleView.OnScrollListener{
//低于当前滚动位置的最小项目数
////在加载更多之前。
私有int visibleThreshold=5;
//已加载数据的当前偏移索引
private int currentPage=0;
//上次加载后数据集中的项目总数
private int previousTotalItemCount=0;
//如果仍在等待加载最后一组数据,则为True。
私有布尔加载=真;
//设置起始页索引
私有int startingPageIndex=0;
RecyclerView.LayoutManager mllayoutmanager;
public endlessRecycleViewScrollListener(LinearLayoutManager布局管理器){
this.mLayoutManager=layoutManager;
}
public endlessRecycleViewScrollListener(GridLayoutManager布局管理器){
this.mLayoutManager=layoutManager;
VisibleThrreshold=VisibleThrreshold*layoutManager.getSpanCount();
}
public EndlessRecycleViewScrollListener(交错边缘布局管理器布局管理器){
this.mLayoutManager=layoutManager;
VisibleThrreshold=VisibleThrreshold*layoutManager.getSpanCount();
}
public int getLastVisibleItem(int[]lastVisibleItemPositions){
int maxSize=0;
对于(int i=0;imaxSize){
maxSize=lastVisibleItemPositions[i];
}
}
返回最大大小;
}
//在滚动过程中,这种情况在一秒钟内会发生很多次,因此请小心放置在此处的代码。
//我们得到了一些有用的参数,以帮助我们确定是否需要加载更多数据,
//但首先我们检查是否在等待前一次加载完成。
@凌驾
已克隆的公共无效(RecyclerView视图、int dx、int dy){
int lastVisibleItemPosition=0;
int totalItemCount=mLayoutManager.getItemCount();
if(交错边缘布局管理器的mLayoutManager实例){
int[]lastVisibleItemPositions=((StaggedGridLayoutManager)mLayoutManager)。findLastVisibleItemPositions(null);
//获取列表中的最大元素
lastVisibleItemPosition=getLastVisibleItem(lastVisibleItemPositions);
}else if(网格布局管理器的mLayoutManager实例){
lastVisibleItemPosition=((GridLayoutManager)mLayoutManager).findLastVisibleItemPosition();
}else if(线性布局管理器的mLayoutManager实例){
lastVisibleItemPosition=((LinearLayoutManager)mLayoutManager)。findLastVisibleItemPosition();
}
//如果项目总数为零,而前一个不是,则假定
//列表无效,应重置回初始状态
if(totalItemCountpublic class PostsActivity extends AppCompatActivity {
@BindView(R.id.rv_posts)
RecyclerView rv_posts;
SaveSystem saveSystem;
List<PostsModels> postsModels = new ArrayList<>();
PostsAdapter postsAdapter;
EndlessRecyclerViewScrollListener scrollListener;
int page = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_posts);
ButterKnife.bind(this);
saveSystem = new SaveSystem(this);
//check first time
if (saveSystem.LoadBooleanData("FirstTime")) {
saveSystem.SaveStringData("language", "en");
saveSystem.SaveBooleanData("FirstTime", false);
}
//setData
setData();
}
void setData() {
postsAdapter = new PostsAdapter(PostsActivity.this, postsModels);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(PostsActivity.this, LinearLayoutManager.VERTICAL, false);
rv_posts.setLayoutManager(linearLayoutManager);
rv_posts.setAdapter(postsAdapter);
scrollListener = new EndlessRecyclerViewScrollListener(linearLayoutManager) {
@Override
public void onLoadMore(int i, int totalItemsCount, RecyclerView view) {
page = page + 10;
Toast.makeText(PostsActivity.this, "" + page, Toast.LENGTH_SHORT).show();
retrofit(page);
}
};
rv_posts.addOnScrollListener(scrollListener);
retrofit(page);
}
void retrofit(int number) {
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://***").addConverterFactory(GsonConverterFactory.create()).build();
HTTP http = retrofit.create(HTTP.class);
Call<List<PostsModels>> call = http.get_posts(saveSystem.LoadStringData("language"), number);
call.enqueue(new Callback<List<PostsModels>>() {
@Override
public void onResponse(Call<List<PostsModels>> call, Response<List<PostsModels>> response) {
List<PostsModels> list = response.body();
for (PostsModels i : list) {
postsModels.add(new PostsModels(i.id, i.user_id, i.user_name, i.word, i.description, i.sound, i.level, i.shortcut));
}
postsAdapter.notifyItemRangeInserted(postsAdapter.getItemCount(), list.size() - 1);
}
@Override
public void onFailure(Call<List<PostsModels>> call, Throwable t) {
}
});
}
}
public class PostsModels {
@SerializedName("id")
String id;
@SerializedName("user_id")
String user_id;
@SerializedName("user_name")
String user_name;
@SerializedName("word")
String word;
@SerializedName("description")
String description;
@SerializedName("sound")
String sound;
@SerializedName("level")
String level;
@SerializedName("shortcut")
String shortcut;
public PostsModels(String id, String user_id, String user_name, String word, String description, String sound, String level, String shortcut) {
this.id = id;
this.user_id = user_id;
this.user_name = user_name;
this.word = word;
this.description = description;
this.sound = sound;
this.level = level;
this.shortcut = shortcut;
}
}
public class PostsAdapter extends RecyclerView.Adapter<PostsAdapter.posts> {
Context context;
List<PostsModels> postsModels;
int lastCheckedPosition = -1;
public PostsAdapter(Context context, List<PostsModels> postsModels) {
this.context = context;
this.postsModels = postsModels;
}
@NonNull
@Override
public posts onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_posts, viewGroup, false);
posts viewHolder = new posts(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull posts posts, int i) {
PostsModels list = postsModels.get(i);
//set photo
Picasso.get().load("http://graph.facebook.com/" + list.user_id + "/picture?type=large").into(posts.img_profile);
//set word
posts.txt_word.setText(list.word);
//set id
posts.txt_id.setText("#" + list.id);
//set description
posts.txt_description.setText(list.description);
//if checked
if (lastCheckedPosition == i) {
posts.linear_layout.setBackgroundColor(context.getResources().getColor(R.color.posts_item_selected_1));
posts.frame_pink.setVisibility(View.VISIBLE);
} else {
posts.linear_layout.setBackgroundColor(0x000000000);
posts.frame_pink.setVisibility(View.INVISIBLE);
}
}
@Override
public int getItemCount() {
return postsModels.size();
}
class posts extends RecyclerView.ViewHolder {
@BindView(R.id.linear_layout)
LinearLayout linear_layout;
@BindView(R.id.frame_pink)
FrameLayout frame_pink;
@BindView(R.id.img_profile)
CircleImageView img_profile;
@BindView(R.id.txt_word)
TextViewFont txt_word;
@BindView(R.id.txt_id)
TextViewFont txt_id;
@BindView(R.id.txt_description)
TextViewFont txt_description;
@OnClick(R.id.linear_layout)
public void onClick() {
lastCheckedPosition = getAdapterPosition();
notifyDataSetChanged();
}
public posts(View view) {
super(view);
ButterKnife.bind(this, view);
}
}
}
public abstract class EndlessRecyclerViewScrollListener extends RecyclerView.OnScrollListener {
// The minimum amount of items to have below your current scroll position
// // before loading more.
private int visibleThreshold = 5;
// The current offset index of data you have loaded
private int currentPage = 0;
// The total number of items in the dataset after the last load
private int previousTotalItemCount = 0;
// True if we are still waiting for the last set of data to load.
private boolean loading = true;
// Sets the starting page index
private int startingPageIndex = 0;
RecyclerView.LayoutManager mLayoutManager;
public EndlessRecyclerViewScrollListener(LinearLayoutManager layoutManager) {
this.mLayoutManager = layoutManager;
}
public EndlessRecyclerViewScrollListener(GridLayoutManager layoutManager) {
this.mLayoutManager = layoutManager;
visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
}
public EndlessRecyclerViewScrollListener(StaggeredGridLayoutManager layoutManager) {
this.mLayoutManager = layoutManager;
visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
}
public int getLastVisibleItem(int[] lastVisibleItemPositions) {
int maxSize = 0;
for (int i = 0; i < lastVisibleItemPositions.length; i++) {
if (i == 0) {
maxSize = lastVisibleItemPositions[i];
}
else if (lastVisibleItemPositions[i] > maxSize) {
maxSize = lastVisibleItemPositions[i];
}
}
return maxSize;
}
// This happens many times a second during a scroll, so be wary of the code you place here.
// We are given a few useful parameters to help us work out if we need to load some more data,
// but first we check if we are waiting for the previous load to finish.
@Override
public void onScrolled(RecyclerView view, int dx, int dy) {
int lastVisibleItemPosition = 0;
int totalItemCount = mLayoutManager.getItemCount();
if (mLayoutManager instanceof StaggeredGridLayoutManager) {
int[] lastVisibleItemPositions = ((StaggeredGridLayoutManager) mLayoutManager).findLastVisibleItemPositions(null);
// get maximum element within the list
lastVisibleItemPosition = getLastVisibleItem(lastVisibleItemPositions);
} else if (mLayoutManager instanceof GridLayoutManager) {
lastVisibleItemPosition = ((GridLayoutManager) mLayoutManager).findLastVisibleItemPosition();
} else if (mLayoutManager instanceof LinearLayoutManager) {
lastVisibleItemPosition = ((LinearLayoutManager) mLayoutManager).findLastVisibleItemPosition();
}
// If the total item count is zero and the previous isn't, assume the
// list is invalidated and should be reset back to initial state
if (totalItemCount < previousTotalItemCount) {
this.currentPage = this.startingPageIndex;
this.previousTotalItemCount = totalItemCount;
if (totalItemCount == 0) {
this.loading = true;
}
}
// If it’s still loading, we check to see if the dataset count has
// changed, if so we conclude it has finished loading and update the current page
// number and total item count.
if (loading && (totalItemCount > previousTotalItemCount)) {
loading = false;
previousTotalItemCount = totalItemCount;
}
// If it isn’t currently loading, we check to see if we have breached
// the visibleThreshold and need to reload more data.
// If we do need to reload some more data, we execute onLoadMore to fetch the data.
// threshold should reflect how many total columns there are too
if (!loading && (lastVisibleItemPosition + visibleThreshold) > totalItemCount) {
currentPage++;
onLoadMore(currentPage, totalItemCount, view);
loading = true;
}
}
// Call this method whenever performing new searches
public void resetState() {
this.currentPage = this.startingPageIndex;
this.previousTotalItemCount = 0;
this.loading = true;
}
// Defines the process for actually loading more data based on page
public abstract void onLoadMore(int page, int totalItemsCount, RecyclerView view);