0
I’m trying to paginate with Recycleview and Staggeredgridlayoutmanager but I’m not getting the right result. Every time I start the app, right at the beginning of the list, it displays the Oast that I put in the "onload" function that would be called at the end of the list, and is endlessly calling this function.
I’m using this library: https://github.com/MarkoMilos/Paginate
Here is Fragment’s code:
public class DemoObjectFragment extends Fragment {
public static final String ARG_OBJECT = "object";
public ArrayList<String> opcoes;
ArrayAdapter<String> adaptador;
private StaggeredGridLayoutManager gaggeredGridLayoutManager;
public RecyclerView recyclerView;
public NestedScrollView nestedScrollView;
public ProgressBar progressBar;
private AdView mAdView;
public SolventRecyclerViewAdapter adapter = null;
public List<ImagemObjeto> imgs;
public int pagina = 1;
public Paginate.Callbacks callbacks;
public boolean isLoading = false;
private static final int GRID_SPAN = 2;
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.content_main, container, false);
Bundle args = getArguments();
progressBar = rootView.findViewById(R.id.progresbar);
AdView adView = new AdView(this.getContext());
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
mAdView = rootView.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
recyclerView = rootView.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
nestedScrollView = rootView.findViewById(R.id.nested_primeiro);
gaggeredGridLayoutManager = new StaggeredGridLayoutManager(2, 1);
recyclerView.setLayoutManager(gaggeredGridLayoutManager);
//Executo a chamada pela primeira vez
AsyncTaskConsumirApi async = new AsyncTaskConsumirApi();
async.execute();
callbacks = new Paginate.Callbacks() {
@Override
public void onLoadMore() {
Toast.makeText(getContext(), "final da lista pagina "+pagina, Toast.LENGTH_SHORT).show();
isLoading = true;
AsyncTaskConsumirApi async = new AsyncTaskConsumirApi();
async.execute();
}
@Override
public boolean isLoading() {
// Indicate whether new page loading is in progress or not
return isLoading;
}
@Override
public boolean hasLoadedAllItems() {
// Indicate whether all data (pages) are loaded or not
return false;
}
};
return rootView;
}
public static DemoObjectFragment newInstance(int position) {
DemoObjectFragment f = new DemoObjectFragment();
Bundle b = new Bundle();
b.putInt(ARG_OBJECT, position);
f.setArguments(b);
return f;
}
public class AsyncTaskConsumirApi extends AsyncTask<String, Integer, List<ImagemObjeto>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressBar.setVisibility(View.VISIBLE); //To show ProgressBar
}
@Override
protected List<ImagemObjeto> doInBackground(String[] strings) {
ControllerApi controllerApi = new ControllerApi(getContext(), "");
String json = controllerApi.pegarDados("https://jsonplaceholder.typicode.com/photos");
List<ImagemObjeto> novasImgs = controllerApi.baixarImagens(json, pagina);
return novasImgs;
}
@Override
protected void onPostExecute(List<ImagemObjeto> o) {
super.onPostExecute(o);
if (pagina==1) {
imgs = o;
adapter = new SolventRecyclerViewAdapter(getContext(), imgs);
recyclerView.setAdapter(adapter);
Paginate.with(recyclerView, callbacks)
.setLoadingTriggerThreshold(2)
.addLoadingListItem(true)
.setLoadingListItemSpanSizeLookup(new LoadingListItemSpanLookup() {
@Override
public int getSpanSize() {
return GRID_SPAN;
}
})
.build();
} else {
for(int i =0;i<o.size();i++){
imgs.add(o.get(i));
}
adapter.notifyDataSetChanged();
}
pagina++;
isLoading=false;
}}}
Here’s my Fragment layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
android:fillViewport="true"
android:orientation="vertical">
<ProgressBar
android:id="@+id/progresbar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:visibility="gone"/>
<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="bottom"
android:foregroundGravity="bottom"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111"></com.google.android.gms.ads.AdView>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/nested_primeiro"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
android:fillViewport="true">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"></android.support.v7.widget.RecyclerView>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>