在滚动到底部后运行app时没有发生任何事情,下面没有加载数据的加载标志是endlessScroll代码扩展RecyclerView.OnScrollListener
public abstract class EndlessScroll extends RecyclerView.OnScrollListener {
public static String TAG = EndlessScroll.class.getSimpleName();
private int previousTotal = 0; // The total number of items in the dataset after the last load
private boolean loading = true; // True if we are still waiting for the last set of data to load.
private int visibleThreshold = 4; // The minimum amount of items to have below your current scroll position before loading more.
int firstVisibleItem, visibleItemCount, totalItemCount;
private int current_page = 1;
private LinearLayoutManager mLinearLayoutManager;
public EndlessScroll(LinearLayoutManager linearLayoutManager) {
this.mLinearLayoutManager = linearLayoutManager;
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
visibleItemCount = recyclerView.getChildCount();
totalItemCount = mLinearLayoutManager.getItemCount();
firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();
if (loading) {
if (totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
}
}
if (!loading && (totalItemCount - visibleItemCount)
<= (firstVisibleItem + visibleThreshold)) {
// End has been reached
// Do something
current_page++;
onLoadMore(current_page);
loading = true;
}
}
public abstract void onLoadMore(int current_page);
}
现在下面是我的异步任务代码
public class RequestItems extends AsyncTask<Void, Void, Void> {
Context context;
ProgressDialog progressDialog;
List<ParseObject> parseObjects;
List<FeedItem> feedItems=new ArrayList<>();
EventsRecyclerView eventsRecyclerView;
RecyclerView recyclerView;
RequestItems(Context context,RecyclerView recyclerView) {
this.context = context;
this.recyclerView=recyclerView;
}
@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(context);
progressDialog.setMessage("Loading");
progressDialog.show();
progressDialog.setCancelable(false);
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
Loadmore();
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progressDialog.dismiss();
final EventsRecyclerView eventsRecyclerView=new EventsRecyclerView(context,feedItems);
int spacingInPixels = context.getResources().getDimensionPixelSize(R.dimen.spacing);
recyclerView.addItemDecoration(new SpacesItemDecoration(spacingInPixels));
LinearLayoutManager manager=new LinearLayoutManager(context);
recyclerView.setLayoutManager(manager);
recyclerView.setOnScrollListener(new EndlessScroll(manager) {
@Override
public void onLoadMore(int current_page) {
Loadmore();
}
});
recyclerView.setAdapter(eventsRecyclerView);
}
private void Loadmore() {
ParseQuery<ParseObject> query = ParseQuery.getQuery("Events");
query.setLimit(5);
query.orderByDescending("createdAt");
try {
parseObjects = query.find();
} catch (ParseException e) {
e.printStackTrace();
}
if (!parseObjects.isEmpty()) {
for (int i = 0; i < parseObjects.size(); i++) {
FeedItem item = new FeedItem();
item.setTitle(parseObjects.get(i).getString("Title"));
item.setDate(parseObjects.get(i).getString("Date"));
item.setTime(parseObjects.get(i).getString("Time"));
item.setVenue(parseObjects.get(i).getString("Venue"));
item.setLike(parseObjects.get(i).getBoolean("Like"));
ParseFile file = parseObjects.get(i).getParseFile("EventImage");
try {
byte bytes[] = file.getData();
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
Bitmap bitmap1 = Bitmap.createScaledBitmap(bitmap,150,200,true);
item.setEventImage(bitmap1);
feedItems.add(item);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
}
} 我无法在此代码中找到错误是否有遗漏或错误
答案 0 :(得分:-1)
请尝试以下方法
GroomViewAdapter groomViewPagerAdapter = new GroomViewAdapter(getBaseContext(),
listGroom,R.layout.item_groom_viewpager);
viewpagerGroom.setAdapter(groomViewPagerAdapter);
viewpagerGroom.clearOnScrollListeners();
viewpagerGroom.addOnScrollListener(new RecyclerView.OnScrollListener(){
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if(dy < 100){
super.onScrolled(recyclerView, dx, 0);
}else {
super.onScrolled(recyclerView, dx, dy);
}
}
});