【发布时间】:2020-06-14 01:28:13
【问题描述】:
我创建了一个简单的图库,显示手机上的所有图像。我想按日期(月份)划分图像,并且还想要一个包含最近拍摄的照片的顶部。请参考下图:
我该怎么做呢?
我的代码:
SelectFileActivity.Java
public class SelectFileActivity extends AppCompatActivity {
RecyclerView recyclerView;
GalleryAdapter galleryAdapter;
List < String > images;
TextView gallery_number;
ImageView back_arrow;
private static final int REQUEST_CODE_STORAGE_PERMISSION = 101;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_file);
// Remove status bar
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
// Initialize content
gallery_number = findViewById(R.id.gallery_number);
recyclerView = findViewById(R.id.recyclerview_gallery_images);
back_arrow = findViewById(R.id.select_file_back_arrow);
// Check for permission
if (ContextCompat.checkSelfPermission(SelectFileActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(SelectFileActivity.this,
new String[] {
Manifest.permission.READ_EXTERNAL_STORAGE
}, REQUEST_CODE_STORAGE_PERMISSION);
} else {
loadImages();
}
}
@Override
public void onBackPressed() {
startActivity(new Intent(this, HomeActivity.class));
finish();
}
private void loadImages() {
recyclerView.setHasFixedSize(true);
// Set the number of pictures per a row
recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
images = SelectImagesGallery.listOfImages(this);
galleryAdapter = new GalleryAdapter(this, images, new GalleryAdapter.PhotoListener() {
@Override
public void onPhotoClick(String path) {
// Do something with the selected photo
}
});
recyclerView.setAdapter(galleryAdapter);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CODE_STORAGE_PERMISSION) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
loadImages();
} else {
Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
}
}
}
GalleryAdapter.java
public class GalleryAdapter extends RecyclerView.Adapter < GalleryAdapter.ViewHolder > {
private Context context;
private List < String > images;
protected PhotoListener photoListener;
public GalleryAdapter(Context context, List < String > images, PhotoListener photoListener) {
this.context = context;
this.images = images;
this.photoListener = photoListener;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(
LayoutInflater.from(context).inflate(R.layout.gallery_item, parent, false)
);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
String image = images.get(position);
// Load images to Glide
Glide.with(context)
.load(image)
.transform(new CenterCrop(), new RoundedCorners(15))
.into(holder.image);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
photoListener.onPhotoClick(image);
}
});
}
@Override
public int getItemCount() {
return images.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView image;
public ViewHolder(@NonNull View itemView) {
super(itemView);
image = itemView.findViewById(R.id.image);
}
}
public interface PhotoListener {
void onPhotoClick(String path);
}
}
activity_select_file.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/windowBackground"
android:orientation="vertical"
tools:context=".upload.SelectFileActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview_gallery_images"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
【问题讨论】:
标签: java android xml android-recyclerview