我使用Recyclerview制作自己的画廊 它可以正常工作,但不完全如我所愿。
这是我的代码的结果:
我想产生这个结果:
有我的MainActivity.java:
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private String[] list ;
private RecyclerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(this,new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE
},200);
return;
}
list = new String[new File(Environment.getExternalStorageDirectory() + File.separator + "folderName").list().length];
recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
layoutManager = new GridLayoutManager(this, 3);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
getPictureList();
}
private void getPictureList() {
File[] files = new File(Environment.getExternalStorageDirectory() + File.separator + "folderName").listFiles();
for (int i = 0; i < files.length; i++){
list[i] = files[i].getAbsolutePath();
}
Collections.sort(Arrays.asList(list));
Collections.reverse(Arrays.asList(list));
adapter = new RecyclerAdapter(list);
recyclerView.setAdapter(adapter);
}
}
现在有我的Recyclerview适配器:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private String[] list;
public RecyclerAdapter(String[] list){
this.list = list;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
ImageView imageView = (ImageView) LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.image_view_layout, viewGroup, false);
imageView.setAdjustViewBounds(true);
imageView.setPadding(10, 10, 10, 10);
MyViewHolder myViewHolder = new MyViewHolder(imageView);
return myViewHolder;
}
@Override
public void onBindViewHolder(@NonNull final MyViewHolder viewHolder, final int i) {
Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(list[i]), 150, 150);
viewHolder.imageView.setImageBitmap(ThumbImage);
viewHolder.imageView.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), PreviewPicture.class);
intent.putExtra("fileName", list[i]);
v.getContext().startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return list.length;
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
ImageView imageView;
public MyViewHolder(@NonNull ImageView itemView) {
super(itemView);
imageView = itemView;
}
}
}
还有我的image_view_layout:
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ImageView>
结果是第一张照片。
注意:
我的照片名称是picture_TIMESTAMP.jpg
在每个日期,我都希望将手机中的栏目设为真实的画廊
谢谢!
答案 0 :(得分:0)
使用新的适配器和布局创建另一个recyclerview,其中包含实际的recyclerview和位于其顶部的文本字段(您的主要活动变为包含实际的recyclerView的新recyclerView),然后您需要按日期对图像进行排序并在每个sub-recyclerView上放置正确的列表