我有一个recyclerview,我正在尝试用这个popular android Endless scroll Library实现Endless Scroll。它几乎运行良好,除了当我到达当前列表的底部时,它重新加载并从顶部开始而不是从最后一个可见项继续。
这是我的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);
}
});
答案 0 :(得分:1)
在适配器中添加一个新功能,将您拥有的数据与新数据合并:
public void addAll(List data){
deals.addAll(data);
notifyDataSetChanged();
}
loadMore函数中的onResponse应如下所示:
public void onResponse(String response) {
//Log.i("Responses", response);
ArrayList<Deal> deals = new JsonConverter<Deal>().toArrayList(response, Deal.class);
adapter.addAll(deals);
stopAnim();
}