我正在开发一个应用程序,其中我必须显示存储在手机目录中的图像。我已经实现了将代码获取到recyclerview中的所有图像的代码,但是我想显示在标头中拍摄图像的日期。我已经搜索了很多天,但是找不到任何具体答案。我遵循的设计是在 Image
我在这里初始化我的观点
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view= inflater.inflate(R.layout.fragment_list_images,container,false);
ButterKnife.bind(this,view);
context= view.getContext();
adapter= new ImagesAdapter();
LinearLayoutManager linearVertical = new LinearLayoutManager(view.getContext(), LinearLayoutManager.VERTICAL,false);
recyclerView.setLayoutManager(linearVertical);
recyclerView.setAdapter(adapter);
checkboxHeaderVideo.setClickable(false);
adapter.setData(getImages(context));
return view;
}
在这里,我从目录中获取所有图片
private List<ImagesModel> getImages(Context context) {
ArrayList<ImagesModel> ImagesModels= new ArrayList<>();
int int_position = 0;
Uri uri;
int column_index_data, thum, column_index_gif, thumGif;
String absolutePathOfImage = null;
String duration = null;
uri = MediaStore.Image.Media.EXTERNAL_CONTENT_URI;
String[] projection = {MediaStore.MediaColumns.DATA, MediaStore.Image.Media.BUCKET_DISPLAY_NAME,MediaStore.Image.Media._ID,MediaStore.Image.Thumbnails.DATA};
final String orderBy = MediaStore.Image.Media.DATE_TAKEN;
cursor = context.getContentResolver().query(uri, projection, null, null, orderBy + " DESC");
column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
thum = cursor.getColumnIndexOrThrow(MediaStore.Image.Thumbnails.DATA);
while (cursor.moveToNext()) {
absolutePathOfImage = cursor.getString(column_index_data);
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
File files= new File(absolutePathOfImage);
if (files.exists()){
retriever.setDataSource(absolutePathOfImage);
ArrayList<String> list = AppUtils.fileParsing(absolutePathOfImage);
String path_name =list.get(0);
Log.i("path name",path_name);
ImagesModel obj_model = new ImagesModel();
obj_model.setImageName(path_name);
obj_model.setThumbNail(cursor.getString(thum));
obj_model.setSelected(false);
obj_model.setAbsolutePath(absolutePathOfImage);
ImagesModels.add(obj_model);
}
}
listSize= ImagesModels.size();
return ImagesModels;
}
图像适配器。
public class ImagesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
List<ImagesModel> modelList;
private boolean selection;
OnSelectionChangeListener selectionChangeListener;
int count=0;
public void setSelectionChangeListener(OnSelectionChangeListener selectionChangeListener) {
this.selectionChangeListener = selectionChangeListener;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.li_Images_list,parent,false);
return new ImagesItemHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
int size= modelList.size();
((ImagesItemHolder)holder).bindData(modelList.get(position),position,size);
}
@Override
public int getItemCount() {
return modelList.size();
}
public void setData(List<ImagesModel> list) {
this.modelList= list;
notifyDataSetChanged();
}
public boolean isSelection() {
return selection;
}
public void setSelection(boolean selection) {
this.selection = selection;
if (selectionChangeListener != null)
selectionChangeListener.onSelectionChange(selection);
notifyDataSetChanged();
}
public void clearSelection() {
for (ImagesModel obj : getData()) {
obj.setSelected(false);
}
setSelection(false);
count=0;
}
public interface OnSelectionChangeListener {
void onSelectionChange(boolean selection);
}
public List<ImagesModel> getData() {
return modelList;
}
public void setChecked(int position) {
count++;
getData().get(position).setSelected(!getData().get(position).isSelected());
if (!isAnyItemSelected()) {
setSelection(false);
} else notifyItemChanged(position);
}
boolean isAnyItemSelected() {
for (ImagesModel obj : getData()) {
if (obj.isSelected())
return true;
}
return false;
}
public void setCheckedAll(boolean selection) {
for (ImagesModel object : getData())
object.setSelected(selection);
count= getData().size();
setSelection(selection);
}
public int getSelectedItemCount(){
return count;
}
public ArrayList<ImagesModel> getAllSelectedItems() {
ArrayList<ImagesModel> selectedObjects = new ArrayList<>();
if (getData() != null && !getData().isEmpty()) {
for (ImagesModel item : getData()) {
if (item.isSelected()) selectedObjects.add(item);
}
}
return selectedObjects;
}
问题是我不知道如何以特定格式将日期添加为标题并在该特定日期下方显示相关图像