LoaderManager 不是从子类触发的
LoaderManager doesn't triggered from subclass
我有我的 MainActivity,它简单地实现 LoaderManager
:
public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor>
在覆盖的方法中,我为 RecycleView
创建了一个 new
适配器,例如:
@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);
}
现在在我的 NYTAdapter
我 再次 实施 LoaderManager
:
public class NYTAdapter extends RecyclerView.Adapter<NYTAdapter.ViewHolder> implements LoaderManager.LoaderCallbacks<Cursor>
要创建另一个适配器:
@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);
}
但是问题是,NYTAdapter
里面重写的方法没有触发,我不知道为什么?
您所要做的就是在您的 activity 或片段上调用 getLoaderManager.initLoader
。
我有我的 MainActivity,它简单地实现 LoaderManager
:
public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor>
在覆盖的方法中,我为 RecycleView
创建了一个 new
适配器,例如:
@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);
}
现在在我的 NYTAdapter
我 再次 实施 LoaderManager
:
public class NYTAdapter extends RecyclerView.Adapter<NYTAdapter.ViewHolder> implements LoaderManager.LoaderCallbacks<Cursor>
要创建另一个适配器:
@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);
}
但是问题是,NYTAdapter
里面重写的方法没有触发,我不知道为什么?
您所要做的就是在您的 activity 或片段上调用 getLoaderManager.initLoader
。