In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to use List Fragments to create photo album Gallery in Android Studio, which is very detailed and has certain reference value. Friends who are interested must finish reading it!
AsyncTaskLoaders and Fragments
Loading the entire picture library to List is a computationally intensive and intensive task. Therefore, we want to solve this problem through asynchronous loading using the AsyncTaskLoader provided by Android. Here, I have written a custom AsyncTaskLoader tool class to load pictures in the gallery. I named it PhotoGalleryImageProvider, which can be found in the source code.
Fragments provides a special interface to the Loader of asynchronous tasks to automatically trigger asynchronous loading tasks. Our gallery list looks like the following code in Fragment:
@ Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); / / Create an empty loader and pre-initialize the photo list items as an empty list. Context context = getActivity (). GetBaseContext (); / / Set up empty mAdapter mPhotoListItem = new ArrayList (); mAdapter = new PhotoAdapter (context, R.layout.photo_item, mPhotoListItem, false); / / Prepare the loader. Either re-connect with an existing one, / / or start a new one. GetLoaderManager () .initLoader (0, null, this);}
Please note this line of *:
GetLoaderManager () .initLoader (0, null, this)
The purpose of this line is to enable AsyncLoader automatically. The code for AsyncLoader is placed at the end of the Class file.
/ * Loader Handlers for loading the photos in the background. * / @ Override public Loader onCreateLoader (int id, Bundle args) {/ / This is called when a new Loader needs to be created. This / / sample only has one Loader with no arguments, so it is simple. Return new PhotoGalleryAsyncLoader (getActivity ());}
Each time the background task successfully acquires a picture in the gallery, the following function will be called back:
@ Override public void onLoadFinished (Loader loader, List data) {/ / Set the new data in the mAdapter. MPhotoListItem.clear (); for (int I = 0; I < data.size (); iTunes +) {PhotoItem item = data.get (I); mPhotoListItem.add (item);} mAdapter.notifyDataSetChanged (); resolveEmptyText (); cancelProgressDialog ();}
The array of PhotoItem (used to store data for Adapter) contains thumbnails pointing to all the pictures in the gallery, as well as URL for full-size images. Once the data is obtained, Adapter will definitely notify it through a "notifyDataSetChanged" callback, thus refreshing the current list of images.
Use cursor to get thumbnails
As I mentioned earlier, I have provided a utility class called "PhotoGalleryImageProvider" that makes it easy to get thumbnails of pictures in the gallery with cursors. The main uses of this class are as follows:
/ * Fetch both full sized images and thumbnails via a single query. * Returns all images not in the Camera Roll. * @ param context * @ return * / public static List getAlbumThumbnails (Context context) {final String [] projection = {MediaStore.Images.Thumbnails.DATA,MediaStore.Images.Thumbnails.IMAGE_ID} Cursor thumbnailsCursor = context.getContentResolver (). Query (MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, / / Which columns to return null, / / Return all rows null, null); Return result;}
It is relatively simple to use the Cursors provided by Android to get pictures in the background. Another advanced use is to use CursorLoader to manipulate Cursor. CursorLoader has a built-in AsyncTaskLoader that can be used to automatically handle the background loading process. Because I need to render both thumbnails and full-size images, I chose to write a custom Task Loader even though I could get the same result with AsyncTask with CursorLoader.
The above is all the contents of the article "how to create a photo album Gallery with List Fragments in Android Studio". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.