I have My MainActivity which simply implement LoaderManager
:
public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor>
And inside the overridden methods I create a new
adapter for RecycleView
e.g:
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
return NYTLoader.newNYTInstance(this);
}
@Override
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
NYTAdapter adapter = new NYTAdapter(cursor, this);
adapter.setHasStableIds(true);
mRecyclerView.setAdapter(adapter);
int columnCount = getResources().getInteger(R.integer.list_column_count);
StaggeredGridLayoutManager sglm =
new StaggeredGridLayoutManager(columnCount, StaggeredGridLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(sglm);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mRecyclerView.setAdapter(null);
}
Now inside My NYTAdapter
I again implemented LoaderManager
:
public class NYTAdapter extends RecyclerView.Adapter<NYTAdapter.ViewHolder> implements LoaderManager.LoaderCallbacks<Cursor>
To create another another adapter:
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
Log.d(TAG, "onCreateLoader: ");
return WeatherLoader.newAllWeatherInstance(context);
}
@Override
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
this.cursor = cursor;
Log.d(TAG, "onLoadFinished: ");
WeatherAdapter adapter = new WeatherAdapter(cursor, context);
adapter.setHasStableIds(true);
mRecyclerView.setAdapter(adapter);
int columnCount = context.getResources().getInteger(R.integer.list_column_count);
StaggeredGridLayoutManager sglm =
new StaggeredGridLayoutManager(columnCount, StaggeredGridLayoutManager.HORIZONTAL);
mRecyclerView.setLayoutManager(sglm);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mRecyclerView.setAdapter(null);
}
But the problem that, The overridden methods inside NYTAdapter
didn't triggered, I don't know why?
All you have to do is call getLoaderManager.initLoader
on your activity or your fragment.