When presenting information that has mixed media (like images and text), the Material Design guidelines recommend using a CardView, which is a FrameLayout with some extra features (such as elevation and rounded corners) that give it a consistent look and feel across many different applications and platforms. CardView is a UI component found in the Android Support Libraries.
Using images is resource intensive for your app: the Android framework has to load the entire image into memory at full resolution, even if the app only displays a small thumbnail of the image.
In this section you learn how to use the Glide library to load large images efficiently, without draining your resources or even crashing your app due to 'Out of Memory' exceptions.
- we added some picture to the drawable folder of the project.
- we created an array of strings in the string.xml so that each item refers to a drawable resource.
- we added the new image resource to the sport object. we edited the sport class for that.
- fixed the initializeData() method because the constructor for the Sport object demands the image resource as the third parameter.
- A convenient data structure to use would be a TypedArray. A TypedArray allows you to store an array of other XML resources. Using a TypedArray, you can obtain the image resources as well as the sports title and information by using indexing in the same loop.
TypedArray sportsImageResources =
getResources().obtainTypedArray(R.array.sports_images);- You can access an element at index i in the TypedArray by using the appropriate "get" method, depending on the type of resource in the array. In this specific case, it contains resource IDs, so you use the getResourceId() method.
- Clean up the data in the typed array once you have created the Sport data ArrayList:
sportImageResources.recycle();- The adjustViewBounds attribute makes the ImageView adjust its boundaries to preserve the aspect ratio of the image.
- Load the images using Glide
- There are a number of ways to reduce the memory consumption when loading images, but one of the easiest approaches is to use an Image Loading Library like Glide, which you will do in this step. Glide uses background processing, as well some other complex processing, to reduce the memory requirements of loading images. It also includes some useful features like showing placeholder images while the desired images are loaded. we edited the adapter so that it loads the image into imageview using the glide library.
The Android SDK includes a class called ItemTouchHelper that is used to define what happens to RecyclerView list items when the user performs various touch actions, such as swipe, or drag and drop. Some of the common use cases are already implemented in a set of methods in ItemTouchHelper.SimpleCallback.
- we created a ItemTouchHelper like this in the onCreate of the main activity.
ItemTouchHelper helper=new ItemTouchHelper(
new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT
,ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean onMove (@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped (@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
mSportsData.remove(viewHolder.getAdapterPosition());
mAdapter.notifyItemRemoved(viewHolder.getAdapterPosition());
}
});- we attached the recyclerview with the hellper
helper.attachToRecyclerView(mRecyclerView);- declare the directions that we want this move to happen
- In the onMove() method, get the original and target index from the second and third argument passed in (corresponding to the original and target view holders).
- Swap the items in the dataset by calling Collections.swap() and pass in the dataset, and the initial and final indexes:
- Notify the adapter that the item was moved, passing in the old and new indexes, and change the return statement to true: