下面列出了android.support.v4.app.LoaderManager#initLoader ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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 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());
}
}
}
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 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);
// 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());
}
}
//-------------------------------------------------------------------------------------
}
/**
* @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);
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getListView().setDivider(null);
LoaderManager manager = getActivity().getSupportLoaderManager();
Bundle args = new Bundle();
Feed feed = new Feed("はてなブックマーク - 新着エントリー",
"http://b.hatena.ne.jp/entrylist?sort=hot&threshold=500&mode=rss");
args.putSerializable(RSS_FEED, feed);
manager.initLoader(0, args, this);
}
/**
* Re-query database.
*
* @param forceUpdate force update
*/
public void requery(final boolean forceUpdate) {
Log.d(TAG, "requery(", forceUpdate, ")");
if (!this.ignoreQuery) {
LoaderManager lm = getLoaderManager();
if (forceUpdate && lm.getLoader(uid) != null) {
lm.restartLoader(uid, null, this);
} else {
lm.initLoader(uid, null, this);
}
} else {
Log.d(TAG, "requery(", forceUpdate, "): ignore");
}
}
@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());
}
}
}
/**
* @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);
}
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Initialize the loaders
LoaderManager manager = getLoaderManager();
Bundle args = new Bundle();
args.putString(ARG_EVENT_ID, GutenbergApplication.from(getActivity()).getEventId());
manager.initLoader(LOADER_ATTENDEES, args, this);
manager.initLoader(LOADER_EVENT, args, this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
if (key.equals(GutenbergApplication.PREF_EVENT_ID)) {
LoaderManager manager = getLoaderManager();
Bundle args = new Bundle();
args.putString(ARG_EVENT_ID, prefs.getString(key, null));
manager.destroyLoader(LOADER_ATTENDEES);
manager.initLoader(LOADER_ATTENDEES, args, this);
manager.destroyLoader(LOADER_EVENT);
manager.initLoader(LOADER_EVENT, args, this);
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Bundle args = new Bundle(getArguments());
args.putString(ARG_EVENT_ID, GutenbergApplication.from(getActivity()).getEventId());
LoaderManager manager = getLoaderManager();
manager.initLoader(LOADER_ATTENDEES, args, this);
if (args.getBoolean(ARG_ONLY_COMING)) {
manager.initLoader(LOADER_COUNT_ALL_ATTENDEES, args, this);
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Check for a Quest ID as an argument, and find the monster
Bundle args = getArguments();
if (args != null) {
long questId = args.getLong(ARG_QUEST_ID, -1);
if (questId != -1) {
LoaderManager lm = getLoaderManager();
lm.initLoader(R.id.quest_detail_fragment, args, new QuestLoaderCallbacks());
}
}
}
/**
* @param activity 用于初始化LoaderManager,需要兼容到2.3
* @param path 指定扫描的文件夹目录,可以为 null,表示扫描所有图片
*/
CursorDataSource(FragmentActivity activity, String path) {
this.activity = activity;
LoaderManager loaderManager = activity.getSupportLoaderManager();
if (path == null) {
// 加载所有的图片
loader = loaderManager.initLoader(getId(LOADER_ALL), null, this);
} else {
// 加载指定目录的图片
Bundle bundle = new Bundle();
bundle.putString("path", path);
loader = loaderManager.initLoader(getId(LOADER_CATEGORY), bundle, this);
}
}
/** Create the fragment UI view */
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate UI view for the fragment
final View rootView = inflater.inflate(R.layout.recycler, container, false); // think about layout
// Create adapter
adapter = new NewsRecyclerAdapter(getContext(), new ArrayList<News>());
// Get a reference to the recycler view
RecyclerView recyclerView = rootView.findViewById(R.id.recycler_view);
// Set layout for recycler view
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setHasFixedSize(true);
// Set adapter for recycler view
recyclerView.setAdapter(adapter);
// Get reference to the Progress bar
progressSpinner = rootView.findViewById(R.id.progress_spinner);
// Indeterminate progress bar type
progressSpinner.setIndeterminate(true);
// Set progress bar color
progressSpinner.getIndeterminateDrawable()
.setColorFilter(
ContextCompat.getColor(getContext(), R.color.colorPrimary),
PorterDuff.Mode.SRC_IN
);
// Set empty view when there is no data on the recycler view
TextView mEmptyStateView = rootView.findViewById(R.id.empty_text_view);
// Check internet connectivity
if (getActivity() != null) {
ConnectivityManager connMgr = (ConnectivityManager) getActivity()
.getSystemService(Context.CONNECTIVITY_SERVICE);
// Get details on the currently active default data network
if (connMgr != null) {
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
// If there is a network connection, fetch data
if (networkInfo != null && networkInfo.isConnected()) {
// Get a reference to the loader manager in order to interact with the loaders
LoaderManager mLoaderManager = getLoaderManager();
// Initialize the loader manager. Pass in the constant declared above as the ID of the
// loader manager and pass in null for the bundle parameter. Finally, also pass in the
// context of the application since this application implements the LoaderCallbacks interface
mLoaderManager.initLoader(LOADER_ID, null, this);
} else {
// Otherwise, display error
// First, hide loading indicator so error message will be visible
progressSpinner.setVisibility(View.GONE);
// Update empty state with no connection error message
mEmptyStateView.setText(R.string.no_internet_connection);
}
}
}
// Return the populated UI view
return rootView;
}
/**
* 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();
/*
* If the user didn't enter anything, there's nothing to search for. In the case where no
* search text was entered but the search button was clicked, we will display a message
* stating that there is nothing to search for and we will not attempt to load anything.
*
* If there is text entered in the search box when the search button was clicked, we will
* create the URL that will return our Github search results, display that URL, and then
* pass that URL to the Loader. The reason we pass the URL as a String is simply a matter
* of convenience. There are other ways of achieving this same result, but we felt this
* was the simplest.
*/
if (TextUtils.isEmpty(githubQuery)) {
mUrlDisplayTextView.setText("No query entered, nothing to search for.");
return;
}
URL githubSearchUrl = NetworkUtils.buildUrl(githubQuery);
mUrlDisplayTextView.setText(githubSearchUrl.toString());
Bundle queryBundle = new Bundle();
queryBundle.putString(SEARCH_QUERY_URL_EXTRA, githubSearchUrl.toString());
/*
* Now that we've created our bundle that we will pass to our Loader, we need to decide
* if we should restart the loader (if the loader already existed) or if we need to
* initialize the loader (if the loader did NOT already exist).
*
* We do this by first store the support loader manager in the variable loaderManager.
* All things related to the Loader go through through the LoaderManager. Once we have a
* hold on the support loader manager, (loaderManager) we can attempt to access our
* githubSearchLoader. To do this, we use LoaderManager's method, "getLoader", and pass in
* the ID we assigned in its creation. You can think of this process similar to finding a
* View by ID. We give the LoaderManager an ID and it returns a loader (if one exists). If
* one doesn't exist, we tell the LoaderManager to create one. If one does exist, we tell
* the LoaderManager to restart it.
*/
LoaderManager loaderManager = getSupportLoaderManager();
Loader<String> githubSearchLoader = loaderManager.getLoader(GITHUB_SEARCH_LOADER);
if (githubSearchLoader == null) {
loaderManager.initLoader(GITHUB_SEARCH_LOADER, queryBundle, this);
} else {
loaderManager.restartLoader(GITHUB_SEARCH_LOADER, queryBundle, this);
}
}
private void initLoaderManager() {
final LoaderManager loaderManager = getLoaderManager();
loaderCallbacks = new MessagesLoaderCallbacks();
loaderManager.initLoader(0, null, loaderCallbacks);
}
/**
* Init Loader Manager
* @param context context
* @param loaderManager loader manager
* @param loaderId loader id
* @param callbacks callbacks
* @param args arguments for loader construction
* @param <T>
*/
public <T> LoaderManagerCreator(Context context, LoaderManager loaderManager, int loaderId, LoaderManager.LoaderCallbacks<T> callbacks, Bundle args) {
loaderManager.initLoader(loaderId, args, callbacks);
}