下面列出了怎么用android.support.v4.app.LoaderManager的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Override and recreate calls from WeaponDetailFragment
// so we can call HornMelodyLoader after WeaponLoader is finished ----------------------
setRetainInstance(true);
// Check for a Weapon ID as an argument, and find the weapon
Bundle args = getArguments();
if (args != null) {
long weaponId = args.getLong(ARG_WEAPON_ID, -1);
if (weaponId != -1) {
LoaderManager lm = getLoaderManager();
lm.initLoader(R.id.weapon_detail_fragment, args, new WeaponLoaderCallbacks2());
}
}
//-------------------------------------------------------------------------------------
}
private void loadListViewItems(int listPosition, boolean markSeen, boolean showProgress) {
if (!isAdded()) {
return;
}
int loaderId = 0;
ConversationsLoaderCallbacks callbacks =
new ConversationsLoaderCallbacks(getContext(), listView,
listPosition, cursorAdapter, markSeen, showProgress);
LoaderManager manager = getLoaderManager();
Loader<?> loader = manager.getLoader(loaderId);
if (loader == null) {
// init and run the items loader
manager.initLoader(loaderId, null, callbacks);
} else {
// restart loader
manager.restartLoader(loaderId, null, callbacks);
}
}
private void loadListViewItems(String itemsFilter, boolean deleteItems, int listPosition) {
if (!isAdded()) {
return;
}
int loaderId = 0;
JournalItemsLoaderCallbacks callbacks =
new JournalItemsLoaderCallbacks(getContext(), cursorAdapter,
itemsFilter, deleteItems, listView, listPosition);
LoaderManager manager = getLoaderManager();
if (manager.getLoader(loaderId) == null) {
// init and run the items loader
manager.initLoader(loaderId, null, callbacks);
} else {
// restart loader
manager.restartLoader(loaderId, null, callbacks);
}
}
private void loadListViewItems(int threadId, int unreadCount, int listPosition) {
if (!isAdded()) {
return;
}
int loaderId = 0;
ConversationLoaderCallbacks callbacks =
new ConversationLoaderCallbacks(getContext(),
threadId, unreadCount, listView, listPosition, cursorAdapter);
LoaderManager manager = getLoaderManager();
if (manager.getLoader(loaderId) == null) {
// init and run the items loader
manager.initLoader(loaderId, null, callbacks);
} else {
// restart loader
manager.restartLoader(loaderId, null, callbacks);
}
}
private void loadListViewItems(String itemsFilter, boolean deleteItems, int listPosition) {
if (!isAdded()) {
return;
}
int loaderId = 0;
ContactsLoaderCallbacks callbacks =
new ContactsLoaderCallbacks(getContext(), contactType, cursorAdapter,
itemsFilter, deleteItems, listView, listPosition);
LoaderManager manager = getLoaderManager();
if (manager.getLoader(loaderId) == null) {
// init and run the items loader
manager.initLoader(loaderId, null, callbacks);
} else {
// restart loader
manager.restartLoader(loaderId, null, callbacks);
}
}
private void search(EditText editText) {
mEditText = editText;
// Initialize the Loader with id '1239' and callbacks.
// If the loader doesn't already exist, one is created. Otherwise,
// the already created Loader is reused. In either case, the
// LoaderManager will manage the Loader across the Activity/Fragment
// lifecycle, will receive any new loads once they have completed,
// and will report this new data back via callbacks.
LoaderManager lm = getSupportLoaderManager();
//close any loader that is already running
lm.destroyLoader(LOADER_ID);
//init new loader
lm.initLoader(LOADER_ID, null, this);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
// Check for a Item ID as an argument, and find the item
Bundle args = getArguments();
if (args != null) {
long itemId = args.getLong(ARG_ITEM_ID, -1);
if (itemId != -1) {
LoaderManager lm = getLoaderManager();
lm.initLoader(R.id.item_detail_fragment, args, new ItemLoaderCallbacks());
}
}
}
public LoaderManager getLoaderManager() {
if (mPlugins.isEmpty()) {
return getOriginal().super_getLoaderManager();
}
final ListIterator<FragmentPlugin> iterator = mPlugins.listIterator(mPlugins.size());
final CallFun0<LoaderManager> superCall = new CallFun0<LoaderManager>("getLoaderManager()") {
@Override
public LoaderManager call() {
if (iterator.hasPrevious()) {
return iterator.previous().getLoaderManager(this);
} else {
return getOriginal().super_getLoaderManager();
}
}
};
return superCall.call();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
setHasOptionsMenu(true);
// Check for a Item ID as an argument, and find the item
Bundle args = getArguments();
if (args != null) {
long armorId = args.getLong(ARG_ARMOR_ID, -1);
if (armorId != -1) {
LoaderManager lm = getLoaderManager();
lm.initLoader(R.id.armor_detail_fragment, args,
new ArmorLoaderCallbacks());
}
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
// Check for a Weapon ID as an argument, and find the weapon
Bundle args = getArguments();
if (args != null) {
long weaponId = args.getLong(ARG_WEAPON_ID, -1);
if (weaponId != -1) {
LoaderManager lm = getLoaderManager();
lm.initLoader(R.id.weapon_detail_fragment, args, new WeaponLoaderCallbacks());
}
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
// Check for a Item ID as an argument, and find the item
Bundle args = getArguments();
if (args != null) {
long locationId = args.getLong(ARG_LOCATION_ID, -1);
if (locationId != -1) {
LoaderManager lm = getLoaderManager();
lm.initLoader(R.id.location_detail_fragment, args, new LocationLoaderCallbacks());
}
}
}
CategoryController(final Activity activity, LoaderManager loaderManager, View itemView) {
super(itemView);
this.activity = activity;
this.loaderManager = loaderManager;
appCardsAdapter = new AppPreviewAdapter(activity);
viewAll = (Button) itemView.findViewById(R.id.view_all_button);
viewAll.setOnClickListener(onViewAll);
heading = (TextView) itemView.findViewById(R.id.name);
image = (FeatureImage) itemView.findViewById(R.id.category_image);
background = (FrameLayout) itemView.findViewById(R.id.category_background);
RecyclerView appCards = (RecyclerView) itemView.findViewById(R.id.app_cards);
appCards.setAdapter(appCardsAdapter);
appCards.addItemDecoration(new ItemDecorator(activity));
displayImageOptions = Utils.getDefaultDisplayImageOptionsBuilder()
.displayer(new FadeInBitmapDisplayer(100, true, true, false))
.build();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
// Check for a Item ID as an argument, and find the item
Bundle args = getArguments();
if (args != null) {
long itemInId = args.getLong(ARG_ITEM_IN_ID, -1);
if (itemInId != -1) {
LoaderManager lm = getLoaderManager();
lm.initLoader(R.id.wyporium_trade_detail_fragment, args,
new WyporiumTradeLoaderCallbacks());
}
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
Log.d(TAG, "onActivityCreated()");
super.onActivityCreated(savedInstanceState);
// signal the dispatcher to sync
mUri = getActivity().getIntent().getData();
if (mUri == null) {
mUri = Patients.CONTENT_URI;
}
Log.d(TAG, "onActivityCreated(): sync?");
mAdapter = new PatientCursorAdapter(getActivity(), null, 0);
setListAdapter(mAdapter);
mAdapter.setOnScrollCompleteListener(this);
// Do we sync with server
delta = getActivity().getResources().getInteger(R.integer.sync_delta_subjects);
//sync(getActivity(), Subjects.CONTENT_URI);
LoaderManager.enableDebugLogging(true);
getActivity().getSupportLoaderManager().initLoader(PATIENTS_LOADER, null, this);
}
/**
* @param activity 用于初始化LoaderManager,需要兼容到2.3
* @param path 指定扫描的文件夹目录,可以为 null,表示扫描所有图片
* @param loadedListener 图片加载完成的监听
*/
public ImageDataSource(FragmentActivity activity, String path, OnImagesLoadedListener loadedListener) {
this.activity = activity;
this.loadedListener = loadedListener;
LoaderManager loaderManager = activity.getSupportLoaderManager();
if (path == null) {
loaderManager.initLoader(LOADER_ALL, null, this);//加载所有的图片
} else {
//加载指定目录的图片
Bundle bundle = new Bundle();
bundle.putString("path", path);
loaderManager.initLoader(LOADER_CATEGORY, bundle, this);
}
}
/**
* 初始化图片搜索Worker
* @param loaderManager
* @param applicationContext
* @param projection 加载数据的映射(MediaStore.Images.Media.DATA等)
* @return
*/
public SearchWorker FromSearch(LoaderManager loaderManager, Context applicationContext, String[] projection){
Map<String,Object> map = new HashMap<>();
map.put("lm",loaderManager);
map.put("ac",applicationContext);
map.put("isQueryByFormat",false);
map.put("selections",new String[]{""});
map.put("projection",projection);
return new SearchWorker(map);
}
/**
* @param activity 用于初始化LoaderManager,需要兼容到2.3
* @param path 指定扫描的文件夹目录,可以为 null,表示扫描所有图片
* @param loadedListener 图片加载完成的监听
*/
public ImageDataSource(FragmentActivity activity, String path, OnImagesLoadedListener loadedListener) {
this.activity = activity;
this.loadedListener = loadedListener;
LoaderManager loaderManager = activity.getSupportLoaderManager();
if (path == null) {
loaderManager.initLoader(LOADER_ALL, null, this);//加载所有的图片
} else {
//加载指定目录的图片
Bundle bundle = new Bundle();
bundle.putString("path", path);
loaderManager.initLoader(LOADER_CATEGORY, bundle, this);
}
}
/**
* This method retrieves the search text from the EditText, constructs the
* URL (using {@link NetworkUtils}) for the github repository you'd like to find, displays
* that URL in a TextView, and finally request that an AsyncTaskLoader performs the GET request.
*/
private void makeGithubSearchQuery() {
String githubQuery = mSearchBoxEditText.getText().toString();
// COMPLETED (17) If no search was entered, indicate that there isn't anything to search for and return
if (TextUtils.isEmpty(githubQuery)) {
mUrlDisplayTextView.setText("No query entered.");
}
URL githubSearchUrl = NetworkUtils.buildUrl(githubQuery);
mUrlDisplayTextView.setText(githubSearchUrl.toString());
// COMPLETED (18) Remove the call to execute the AsyncTask
// COMPLETED (19) Create a bundle called queryBundle
// COMPLETED (20) Use putString with SEARCH_QUERY_URL_EXTRA as the key and the String value of the URL as the value
Bundle queryBundle = new Bundle();
queryBundle.putString(SEARCH_QUERY_URL_EXTRA, githubSearchUrl.toString());
// COMPLETED (21) Call getSupportLoaderManager and store it in a LoaderManager variable
android.support.v4.app.LoaderManager loaderManager = getSupportLoaderManager();
// COMPLETED (22) Get our Loader by calling getLoader and passing the ID we specified
Loader<String> githubSearchLoader = loaderManager.getLoader(GITHUB_SEARCH_LOADER);
// COMPLETED (23) If the Loader was null, initialize it. Else, restart it.
if (githubSearchLoader == null) {
loaderManager.initLoader(GITHUB_SEARCH_LOADER, queryBundle, this);
} else {
loaderManager.restartLoader(GITHUB_SEARCH_LOADER, queryBundle, this);
}
}
public TrafficPresenter(@NonNull GithubRepository githubRepository,
@NonNull TrafficContract.View view,
@NonNull LoaderProvider loaderProvider,
@NonNull LoaderManager loaderManager) {
mGithubRepository = githubRepository;
mView = view;
mLoaderProvider = loaderProvider;
mLoaderManager = loaderManager;
mView.setPresenter(this);
}
public DashboardPresenter(@NonNull GithubRepository githubRepository,
@NonNull DashboardContract.View view,
@NonNull LoaderProvider loaderProvider,
@NonNull LoaderManager loaderManager) {
mGithubRepository = githubRepository;
mDashboardView = view;
mLoaderProvider = loaderProvider;
mLoaderManager = loaderManager;
mDashboardView.setPresenter(this);
}
public PublicRepositoryPresenter(@NonNull GithubRepository githubRepository,
@NonNull PublicRepositoryContract.View view,
@NonNull LoaderProvider loaderProvider,
@NonNull LoaderManager loaderManager) {
mGithubRepository = githubRepository;
mPublicRepositoriesView = view;
mLoaderProvider = loaderProvider;
mLoaderManager = loaderManager;
mPublicRepositoriesView.setPresenter(this);
}
public TrendingRepositoryPresenter(@NonNull GithubRepository githubRepository,
@NonNull TrendingRepositoryContract.View view,
@NonNull LoaderProvider loaderProvider,
@NonNull LoaderManager loaderManager) {
mGithubRepository = githubRepository;
mView = view;
mLoaderProvider = loaderProvider;
mLoaderManager = loaderManager;
mView.setPresenter(this);
}
public void loadNext(View view){
//获取LoaderManager
LoaderManager loaderManager = getSupportLoaderManager();
//LoaderManager.restartLoader
loaderManager.restartLoader(1,null,this);
}
/**
* @param activity 用于初始化LoaderManager,需要兼容到2.3
* @param path 指定扫描的文件夹目录,可以为 null,表示扫描所有图片
* @param loadedListener 图片加载完成的监听
*/
public ImageDataSource(FragmentActivity activity, String path, OnImagesLoadedListener loadedListener) {
this.activity = activity;
this.loadedListener = loadedListener;
LoaderManager loaderManager = activity.getSupportLoaderManager();
if (path == null) {
loaderManager.initLoader(LOADER_ALL, null, this);//加载所有的图片
} else {
//加载指定目录的图片
Bundle bundle = new Bundle();
bundle.putString("path", path);
loaderManager.initLoader(LOADER_CATEGORY, bundle, this);
}
}
private void loadWeather(boolean restart) {
mWeatherLayout.setVisibility(View.INVISIBLE);
mErrorLayout.setVisibility(View.GONE);
mLoadingView.showLoadingIndicator();
LoaderManager.LoaderCallbacks<City> callbacks = new WeatherCallbacks();
if (restart) {
getSupportLoaderManager().restartLoader(R.id.weather_loader_id, Bundle.EMPTY, callbacks);
} else {
getSupportLoaderManager().initLoader(R.id.weather_loader_id, Bundle.EMPTY, callbacks);
}
}
@Override
public LoaderManager getSupportLoaderManager() {
if (isLocalLaunch()) {
return super.getSupportLoaderManager();
} else {
return mMainActivity.getSupportLoaderManager();
}
}
@Override
public android.app.LoaderManager getLoaderManager() {
if (isLocalLaunch()) {
return super.getLoaderManager();
} else {
return mMainActivity.getLoaderManager();
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Check for a Arena Quest ID as an argument, and find the arena quest
Bundle args = getArguments();
if (args != null) {
long questId = args.getLong(ARG_ARENA_QUEST_ID, -1);
if (questId != -1) {
LoaderManager lm = getLoaderManager();
lm.initLoader(R.id.arena_quest_detail_fragment, args, new ArenaQuestLoaderCallbacks());
}
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
// Check for a Monster ID as an argument, and find the monster
Bundle args = getArguments();
if (args != null) {
long monsterId = args.getLong(ARG_MONSTER_ID, -1);
if (monsterId != -1) {
LoaderManager lm = getLoaderManager();
lm.initLoader(R.id.monster_detail_fragment, args, new MonsterLoaderCallbacks());
}
}
}
public AlbumPresenter(
@NonNull AlbumRepository albumRepository,
@NonNull LoaderManager loaderManager,
@NonNull AlbumContract.View albumView) {
mLoadManager = checkNotNull(loaderManager, "loader manager cannot be null");
mAlbumView = checkNotNull(albumView, "albumView cannot be null");
mAlbumRepository = checkNotNull(albumRepository, "albumRepository cannot be null");
//为mAlbumView 设置Presenter
mAlbumView.setPresenter(this);
}