我有一個recyclerview,我試圖用這個popular android Endless scroll Library實現無盡的滾動。它幾乎運行良好,除了當我到達當前列表的按鈕時,它會重新加載並從頂部開始,而不是從最後一個可見項繼續。Recyclerview無盡的滾動刷新頁面
這是我的Scroll Listerner。
scrollListener = new EndlessRecyclerViewScrollListener(manager) {
@Override
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
// Triggered only when new data needs to be appended to the list
// Add whatever code is needed to append new items to the bottom of the list
loadMore(page);
++page;
}
};
mRecyclerView.addOnScrollListener(scrollListener);
這是我loadMore()
public void loadMore(int page) {
String url = myurl + page;
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//Log.i("Responses", response);
ArrayList<Deal> deals = new JsonConverter<Deal>().toArrayList(response, Deal.class);
dealArrayList.addAll(deals);
adapter = new DealAdapter(getApplicationContext(), dealArrayList);
mRecyclerView.setAdapter(adapter);
newList = deals;
adapter.notifyItemRangeInserted(newList.size(), dealArrayList.size());
stopAnim();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, error.toString());
Toast.makeText(getApplicationContext(), "Bad Network Connection. Please Try Again", Toast.LENGTH_LONG).show();
stopAnim();
}
}
);
int socketTimeout = 30000; // 30 seconds. You can change it
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy(policy);
MySingleton.getInstance(getApplicationContext()).addToRequestQueue(stringRequest);
mSwipeRefresh.setRefreshing(false);
}
我無法弄清楚什麼是錯。 幫助。
編輯.. 當我第一次午餐時,我的應用程序調用這個函數,然後當我滾動時調用loadMore函數。
public void firstRun(int page) {
String url = myurl + page;
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("Responses", response);
dealArrayList = new JsonConverter<Deal>().toArrayList(response, Deal.class);
adapter = new DealAdapter(getApplicationContext(), dealArrayList);
mRecyclerView.setAdapter(adapter);
newList = dealArrayList;
adapter.notifyDataSetChanged();
stopAnim();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, error.toString());
Toast.makeText(getApplicationContext(), "Bad Network Connection. Please Try Again", Toast.LENGTH_LONG).show();
stopAnim();
}
}
);
int socketTimeout = 30000; // 30 seconds. You can change it
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy(policy);
MySingleton.getInstance(getApplicationContext()).addToRequestQueue(stringRequest);
mSwipeRefresh.setRefreshing(false);
}
這是我的適配器
public class DealAdapter extends RecyclerView.Adapter<DealAdapter.ViewHolder> {
private Context context;
private ArrayList<Deal> deals;
public DealAdapter(Context context, ArrayList<Deal> deals) {
this.context = context;
this.deals = deals;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.card_view, parent, false);
final ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
Deal deal = deals.get(position);
holder.mCardTitle.setText(deal.title);
holder.mCardLocation.setText(deal.location);
holder.mCardDiscount.setText(String.valueOf(deal.discount));
holder.mCardPrice.setText(deal.price);
holder.mCardContact.setText(deal.contact);
holder.mCardDescription.setText(deal.description);
String url = myurl + deal.image;
Picasso.with(context)
.load(url).resize(0,300)
.placeholder(R.drawable.load)
.error(android.R.drawable.stat_notify_error)
.into(holder.mCardImage);
holder.mCardDescription.setOnClickListener(new View.OnClickListener() {
boolean isClicked = false;
@Override
public void onClick(View v) {
if(isClicked){
//This will shrink textview to 2 lines if it is expanded.
holder.mCardDescription.setMaxLines(2);
holder.more.setVisibility(View.VISIBLE);
holder.mCardInvisible.setVisibility(View.GONE);
isClicked = false;
} else {
//This will expand the textview if it is of 2 lines
holder.mCardDescription.setMaxLines(Integer.MAX_VALUE);
holder.more.setVisibility(View.GONE);
holder.mCardInvisible.setVisibility(View.VISIBLE);
isClicked = true;
}
}
});
}
@Override
public int getItemCount() {
if (deals != null) {
return deals.size();
}else{
return 0;
}
}
static class ViewHolder extends RecyclerView.ViewHolder {
private TextView mCardTitle;
private ImageView mCardImage;
private TextView mCardLocation;
private TextView mCardDescription;
private TextView mCardContact;
private TextView mCardPrice;
private TextView mCardDiscount, more;
private LinearLayout mCardInvisible;
private ViewHolder(View itemView) {
super(itemView);
mCardTitle = (TextView) itemView.findViewById(R.id.mCardTitle);
mCardImage = (ImageView) itemView.findViewById(R.id.mCardImage);
mCardLocation = (TextView) itemView.findViewById(R.id.mCardLocation);
mCardDescription = (TextView) itemView.findViewById(R.id.mCardDescription);
mCardContact = (TextView) itemView.findViewById(R.id.mCardContact);
mCardPrice = (TextView) itemView.findViewById(R.id.mCardPrice);
mCardDiscount = (TextView) itemView.findViewById(R.id.mCardDiscount);
more = (TextView) itemView.findViewById(R.id.mCardMore);
mCardInvisible = (LinearLayout) itemView.findViewById(R.id.mCardInvisible);
}
}
}
編輯::這是我OnRefresh監聽
mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
firstRun(1);
}
}, 3000);
}
});
要設置'適配器=新DealAdapter(getApplicationContext(),dealArrayList);'每次你到達底部,你必須初始化一次您的適配器然後只需添加額外的項目。 – Akis
感謝您的回覆..我將該行代碼移動到onCreate函數,但它仍然是相同的 –
'mRecyclerView.setAdapter(adapter);'必須設置一次 – Akis