Issue
I have 24 rows in database from 1 to 24 but the problem with EndlessRecyclerViewScrollListener class when I try show Items on recyclerview become shuffle items.
I want show Items from #1 to #24
PostsActivity
public 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) {
}
});
}
}
PostsModels
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;
}
}
PostsAdapter
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);
}
}
}
EndlessRecyclerViewScrollListener
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);
I hope you help me to found solution....
Solution
EndlessRecyclerViewScrollListener checks the last visible position with the LayoutManager. When it detects the final one (in this case), call onLoadMore() method. Something weird I notice is that you are managing page in the activity but then ignoring the currentPage value that is using the listener.
Maybe the problem is in that difference. Put a breakpoint inside the EndlessRecyclerViewScrollListener and check how many times calls ´onLoadMore()` method and with which parameters.
Hope it helps.
Answered By - Juanje
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.