Created
May 13, 2019 00:18
-
-
Save CodeBoy722/3024747ffb41ede6d6925c6993dbaed3 to your computer and use it in GitHub Desktop.
Android-Simple-Image-Gallery's pictureFolderAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.androidcodeman.simpleimagegallery.utils; | |
import android.content.Context; | |
import androidx.annotation.NonNull; | |
import androidx.cardview.widget.CardView; | |
import androidx.recyclerview.widget.RecyclerView; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
import com.androidcodeman.simpleimagegallery.R; | |
import com.bumptech.glide.Glide; | |
import com.bumptech.glide.request.RequestOptions; | |
import java.util.ArrayList; | |
/** | |
* Author CodeBoy722 | |
* | |
* An adapter for populating RecyclerView with items representing folders that contain images | |
*/ | |
public class pictureFolderAdapter extends RecyclerView.Adapter<pictureFolderAdapter.FolderHolder>{ | |
private ArrayList<imageFolder> folders; | |
private Context folderContx; | |
private itemClickListener listenToClick; | |
/** | |
* | |
* @param folders An ArrayList of String that represents paths to folders on the external storage that contain pictures | |
* @param folderContx The Activity or fragment Context | |
* @param listen interFace for communication between adapter and fragment or activity | |
*/ | |
public pictureFolderAdapter(ArrayList<imageFolder> folders, Context folderContx, itemClickListener listen) { | |
this.folders = folders; | |
this.folderContx = folderContx; | |
this.listenToClick = listen; | |
} | |
@NonNull | |
@Override | |
public FolderHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { | |
LayoutInflater inflater = LayoutInflater.from(parent.getContext()); | |
View cell = inflater.inflate(R.layout.picture_folder_item, parent, false); | |
return new FolderHolder(cell); | |
} | |
@Override | |
public void onBindViewHolder(@NonNull FolderHolder holder, int position) { | |
final imageFolder folder = folders.get(position); | |
Glide.with(folderContx) | |
.load(folder.getFirstPic()) | |
.apply(new RequestOptions().centerCrop()) | |
.into(holder.folderPic); | |
String text = "("+folder.getNumberOfPics()+") "+folder.getFolderName(); | |
holder.folderName.setText(text); | |
holder.folderPic.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
listenToClick.onPicClicked(folder.getPath(),folder.getFolderName()); | |
} | |
}); | |
} | |
@Override | |
public int getItemCount() { | |
return folders.size(); | |
} | |
public class FolderHolder extends RecyclerView.ViewHolder{ | |
ImageView folderPic; | |
TextView folderName; | |
CardView folderCard; | |
public FolderHolder(@NonNull View itemView) { | |
super(itemView); | |
folderPic = itemView.findViewById(R.id.folderPic); | |
folderName = itemView.findViewById(R.id.folderName); | |
folderCard = itemView.findViewById(R.id.folderCard); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment