-
Notifications
You must be signed in to change notification settings - Fork 1
Search
The Search Module provides a comprehensive solution for searching and displaying images within the application. It follows the MVVM architecture and leverages Android’s modern components for efficient UI updates and data handling.
- Image search functionality
- Real-time search results display
- Image preview and selection
- Efficient data management
SearchFragment
serves as the main UI component for displaying search results and handling user interactions. It manages the search input, displays results, and handles image selection.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
Initializes the RecyclerView, sets up the adapter, and binds ViewModel data.
Implementation Details:
- Sets up a grid layout for search results.
- Observes
SearchViewModel
's image list for data changes. - Handles search input and triggers search operations.
@Override
public void onImageClick(Uri uri)
Handles image click events, launching the detail view for the selected image.
SearchFragment fragment = new SearchFragment();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.container, fragment)
.commit();
SearchViewModel
manages the data and business logic for the search functionality, following the MVVM pattern. It handles loading, searching, and displaying images.
private final MutableLiveData<List<Uri>> imageList
private final MutableLiveData<Boolean> isLoading
private final MutableLiveData<String> error
public void searchImages(String query)
Searches for images based on the provided query and updates LiveData.
Parameters:
-
query
: The search query string
public void loadImages()
Loads images from the local file system or database.
SearchViewModel viewModel = new ViewModelProvider(this).get(SearchViewModel.class);
viewModel.getImageList().observe(this, images -> {
// Update UI
});
viewModel.searchImages("nature");
ImageAdapter
is a RecyclerView adapter for displaying image thumbnails in a grid. It manages image loading and click event delegation.
public ImageAdapter(List<Uri> imageList, OnImageClickListener listener)
Initializes the adapter with a list of image URIs and a click listener.
public void updateData(List<Uri> imageList)
Updates the adapter’s data and refreshes the view.
class ImageViewHolder extends RecyclerView.ViewHolder
Holds references to the image view.
public interface OnImageClickListener {
void onImageClick(Uri uri);
}
Callback interface for image click events.
ImageAdapter adapter = new ImageAdapter(imageUris, uri -> {
// Handle image click
});
recyclerView.setAdapter(adapter);