I am not able to load more pages using Endless RecyclerView ScrollListener
I am not able to load more pages using Endless RecyclerView ScrollListener
I am using Recyclerview to achieve endless recyclerview scroll listener. Now it loading only page = 1 and page = 2 while scrolling but it not loading another pages, and I added EndlessRecyclerViewScrollListener class from github.
Activity
public class AccountPagination extends AppCompatActivity implements RestCallback {
String UserId, rollname, username, name, fname, lname, emailid, contact_no, gender1, date_of_birth, country_id, postal_code, profession_response, Street_Address, City;
NonScrollListView listItem;
public static AccountStatementAdapter adapter;
ArrayList<AccountStatementModel> AccountStatementList;
AccountsSortingAdapter adapterSort;
AccountsTenRecordsAdapter adapterTenRecords;
ArrayList<AccountStatementModel> AccountDetailsList;
ArrayList<AccountStatementModel> AccountSortingList;
ArrayList<AccountStatementModel> AccountTenRecordsList;
ArrayList<AccountStatementModel> androidVersions;
List<AccountStatementModel> AccountList;
RecyclerView recyclerView;
int userPage = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_account_pagination);
initViews();
recyclerView = (RecyclerView) findViewById(R.id.card_recycler_view);
callAccountStatementAPI(userPage);
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(linearLayoutManager);
// recyclerView.setAdapter(adapter);
recyclerView.addOnScrollListener(new EndlessRecyclerViewScrollListener(linearLayoutManager) {
@Override
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
userPage++;
callAccountStatementAPI(userPage);
}
});
//RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
//RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 1);
}
public void initViews() {
Intent i = getIntent();
UserId = i.getStringExtra("id");
name = SharedPref.read(SharedPref.FIRST_NAME, "") + "t" + SharedPref.read(SharedPref.LAST_NAME, "");
String Hello = "Hello " + name;
getSupportActionBar().setSubtitle(Hello);
}
private void callAccountStatementAPI(final int page) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("user_id", SharedPref.read(SharedPref.USER_ID, ""));
map.put("page", String.valueOf(page));
RestService.getInstance(AccountPagination.this).getAccount1(map, new MyCallback<ArrayList<AccountStatementModel>>(AccountPagination.this,
AccountPagination.this, true, "Loading ...", GlobalVariables.SERVICE_MODE.ACCOUNT_STATEMENT));
}
@Override
public void onFailure(Call call, Throwable t, GlobalVariables.SERVICE_MODE mode) {
}
@Override
public void onSuccess(Response response, GlobalVariables.SERVICE_MODE mode) {
switch (mode) {
case ACCOUNT_STATEMENT:
androidVersions = (ArrayList<AccountStatementModel>) response.body();
AccountPaginationAdapter adapter = new AccountPaginationAdapter(getApplicationContext(), androidVersions);
androidVersions.addAll((Collection<? extends AccountStatementModel>) response.body());
adapter.notifyItemRangeInserted(adapter.getItemCount(), androidVersions.size()-2);
recyclerView.setAdapter(adapter);
break;
}
}
}
Adapter Class
public class AccountPaginationAdapter extends RecyclerView.Adapter<AccountPaginationAdapter.ViewHolder> {
private ArrayList<AccountStatementModel> android_versions;
private Context context;
private String TAG = "On Click";
String main_url = "http://www.consumer1st.in/ccb/";
public AccountPaginationAdapter(Context context, ArrayList<AccountStatementModel> android_versions) {
this.context = context;
this.android_versions = android_versions;
}
@Override
public AccountPaginationAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.accountpagination_item_list, viewGroup, false);
return new AccountPaginationAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(AccountPaginationAdapter.ViewHolder viewHolder, final int position) {
viewHolder.lable_name.setText(android_versions.get(position).getRemarks());
viewHolder.icon_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String icoon_index_id = android_versions.get(position).getId();
String iconn_id = android_versions.get(position).getUserId();
}
});
}
@Override
public int getItemCount() {
return android_versions.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView lable_name;
ImageView icon_image;
public ViewHolder(View view) {
super(view);
icon_image = (ImageView) itemView.findViewById(R.id.icon_image);
lable_name = (TextView) itemView.findViewById(R.id.lable_name);
}
}
}
2 Answers
2
Below the code will work But this code totally wrong way actually u are calling every api hit to creating new adapter and adding list. See more pagination examples and change it.. See reference here.
Write a method in Adapter Class:
public void addMoreItems(ArrayList<AccountStatementModel> newItems){
androidVersions.clear();
androidVersions.addAll(newItems);
}
Write Activity:
case ACCOUNT_STATEMENT:
androidVersions = (ArrayList<AccountStatementModel>) response.body();
AccountPaginationAdapter adapter = new AccountPaginationAdapter(getApplicationContext(), androidVersions);
androidVersions.addAll((Collection<? extends AccountStatementModel>) response.body());
adapter.addMoreItems(androidVersions);
adapter.notifyItemRangeInserted(adapter.getItemCount(), androidVersions.size()-2);
recyclerView.setAdapter(adapter);
break;
}
still its not working !!,
– Lokesh GP Loki
Jun 29 at 11:24
He dude this is totally wrong. Refer that link and change your code ... Don't do like this...
– Gobu CSG
Jun 29 at 11:26
Try this you will get perfect result.
int pastVisiblesItems, visibleItemCount, totalItemCount;
recyclerView.addOnScrollListener(new
RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (dy > 0) {
visibleItemCount = layoutManager.getChildCount();
totalItemCount = layoutManager.getItemCount();
pastVisiblesItems = layoutManager.findFirstVisibleItemPosition();
if (loading && totalItemCount >= (pastVisiblesItems + 50)) {
userPage++;
callAccountStatementAPI(userPage);
Log.v("...", "Last Item Wow !");
//Do pagination.. i.e. fetch new data
}
}
}
});
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
That's not PHP.
– kerbholz
Jun 29 at 10:40