我正在使用Recyclerview实现无止境的recyclerview滚动侦听器。现在,它在滚动时仅加载page = 1和page = 2,但未加载其他页面,我从github添加了 EndlessRecyclerViewScrollListener 类。
活动
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;
}
}
}
适配器类
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);
}
}
}
答案 0 :(得分:0)
下面的代码将起作用,但是此代码完全错误,实际上您正在调用每个api命中来创建新的适配器并添加列表。查看更多分页示例并进行更改。.See reference here。
在适配器类中写一个方法:
public void addMoreItems(ArrayList<AccountStatementModel> newItems){
androidVersions.clear();
androidVersions.addAll(newItems);
}
写活动:
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;
}
答案 1 :(得分:0)
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
}
}
}
});