我的应用程序在将更多项目加载到recyclerview时冻结,我的RequestItem类代码被打击
public class RequestItems extends AsyncTask<Void, Void, Void> implements EventsRecyclerView.ClickListenner {
Context context;
ProgressDialog progressDialog;
List<ParseObject> parseObjects;
List<FeedItem> feedItems = new ArrayList<>();
EventsRecyclerView eventsRecyclerView;
RecyclerView recyclerView;
int skip = 0;
public int remove = 0;
int isScrolled=0;
Handler handler = new Handler();
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) {
eventsRecyclerView = null;
Loadmore(eventsRecyclerView);
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.spacing2);
recyclerView.addItemDecoration(new SpacesItemDecoration(spacingInPixels));
LinearLayoutManager manager = new LinearLayoutManager(context);
recyclerView.setLayoutManager(manager);
recyclerView.hasFixedSize();
recyclerView.setAdapter(eventsRecyclerView);
recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (isScrolled==0){
if (newState == recyclerView.getChildCount()) {
skip = skip + 5;
remove = 1;
feedItems.add(null);
eventsRecyclerView.notifyItemInserted(feedItems.size());
handler.postDelayed(new Runnable() {
@Override
public void run() {
Loadmore(eventsRecyclerView);
}
}, 500);
}
}
}
});
eventsRecyclerView.setClickListener(this);
}
private void Loadmore(final EventsRecyclerView eventsRecyclerView) {
if (eventsRecyclerView!=null){
isScrolled=1;
}
ParseQuery<ParseObject> query = ParseQuery.getQuery("Events");
query.setSkip(skip);
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++) {
final 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, 100, 150, true);
item.setEventImage(bitmap1);
if (eventsRecyclerView != null) {
if (remove == 1) {
feedItems.remove(null);
eventsRecyclerView.notifyItemRemoved(feedItems.size());
remove = 0;
}
feedItems.add(item);
eventsRecyclerView.notifyItemInserted(feedItems.size());
isScrolled=0;
} else {
feedItems.add(item);
}
} catch (ParseException e) {
e.printStackTrace();
}
}
} else {
feedItems.remove(null);
eventsRecyclerView.notifyItemRemoved(feedItems.size());
}
}
@Override
public void itemClicked(View view, int position) {
}
}
我无法理解发生了什么,此代码中的问题是什么,因为我的应用程序在将更多项目加载到recyclerview时冻结