我在RecyclerView中创建交错视图并显示SD卡文件夹图像。
我做了什么:我设法在SD卡上创建文件夹并在那里保存所有点击的图像。
iSSUE:现在我无法在交错的回收站视图中显示文件夹中的图像。
Recyclerview布局:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/coordinatorLayout_signup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler_view_cameragallery"
android:layout_marginTop="10dp"
android:layout_below="@+id/view3"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
RecyclerActivity.class
StaggeredGridLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
cameraRecyclerView.setLayoutManager(gridLayoutManager);
adapter = new CameraAdapter(new ArrayList<String>(),getContext());
cameraRecyclerView.setAdapter(adapter);
CameraButton点击处理(在SD卡文件夹中保存图像):
@Override
public void onClick(View v) {
newpath = new File(Environment.getExternalStorageDirectory(),
photoPath + "_" + holder.phone + "/");
// Create the storage directory if it does not exist
newpath.mkdirs();
checkinUUID = UUID.randomUUID();
//media file name
tempphoto = new File(newpath,checkinUUID + ".jpg");
/*create file to save image*/
photoUri = Uri.fromFile(tempphoto);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//set the image file name
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoUri);
// handle the returned data in onActivityResult
startActivityForResult(intent,
Constant.TAKE_CAMERA_PICTURE_REQUEST);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == Constant.TAKE_CAMERA_PICTURE_REQUEST) {
if (newpath.isDirectory() == false) {
newpath.mkdirs();
}
if (newpath.isDirectory() == true) {
if(newpath.exists())
paths = new String[]{tempphoto.getPath()};
MediaScannerConnection.scanFile(getActivity(), paths, null, null);
listofImagesPath =new ArrayList<String>();
listofImagesPath = RetriveCapturedImagePath();
if(listofImagesPath!=null){
adapter.addApplications(listofImagesPath);
}
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}
RetriveCapturedImagePath()
private ArrayList<String> RetriveCapturedImagePath() {
ArrayList<String> tFileList = new ArrayList<String>();
if (newpath.exists()) {
File[] files=newpath.listFiles();
Arrays.sort(files);
for(int i=0; i<files.length; i++){
File file = files[i];
if(file.isDirectory())
continue;
tFileList.add(file.getPath());
}
}
return tFileList;
}
Adapterclass:
public class CameraAdapter extends RecyclerView.Adapter<CameraAdapter.ViewHolder> {
private ArrayList<String> imagesPath;
private Context context;
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_item_camera_view, parent, false);
return new ViewHolder(v);
}
public CameraAdapter(ArrayList<String> imagesPath, Context context) {
this.imagesPath = imagesPath;
this.context = context;
}
public void addApplications(ArrayList<String> candidates) {
this.imagesPath.addAll(candidates);
this.notifyItemRangeInserted(0, candidates.size() - 1);
}
public void clearApplications() {
int size = this.imagesPath.size();
if (size > 0) {
for (int i = 0; i < size; i++) {
imagesPath.remove(0);
}
this.notifyItemRangeRemoved(0, size);
}
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
for (int i=0;i<imagesPath.size();i++){
holder.imageview.setImageBitmap(BitmapFactory.decodeFile(imagesPath.get(position)));
}
// Bitmap bm = BitmapFactory.decodeFile(imagesPath)
}
@Override
public int getItemCount() {
return imagesPath.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public ImageView imageview ;
public TextView imageText;
public ViewHolder(View itemView) {
super(itemView);
this.imageview = (ImageView)itemView.findViewById(R.id.camera_image_view);
// this.imageText = (TextView) itemView.findViewById(R.id.camera_grid_text_id);
}
}
}
single_item_recycler.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/camera_image_view"
android:adjustViewBounds="true"
android:scaleType="centerCrop"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="hello"/>
</LinearLayout>
我上面的代码包含SD卡文件夹中的所有图像。但 无法在交错视图中显示它们。
请帮忙
答案 0 :(得分:0)
问题似乎出现在RecyclerActivity.class
,将Adapter
设置为RecyclerView
时,您要添加空ArrayList
。这就是为什么你的RecyclerView
没有显示任何内容。
adapter = new CameraAdapter(new ArrayList<String>(),getContext());
cameraRecyclerView.setAdapter(adapter);
您应该像这样设置imagePath的ArrayList
:
adapter = new CameraAdapter(imagePaths, getContext());
cameraRecyclerView.setAdapter(adapter);
然后,当ArrayList
中的数据发生变化(填充检索到的数据之后)时,只需在notifyDatasetChanged()
的{{1}}上调用RecyclerView
即可。这应该可以解决问题。
此外,在您的Adapter
方法中,您有一个不必要的for循环:
onBindViewHolder