0

我在RecyclerView内部创建交错视图并显示SD卡文件夹图像。使用Recyclerview交错查看

我做了什么:我设法在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> 

我具有由具有人的上述代码l SD卡文件夹中的图像。但 无法在交错视图中显示它们。

设置AdapterRecyclerView要添加一个空ArrayList时,请帮

+0

不要看到在所有或发生了什么任何的图片? – Jeffalee

+0

@Jeffalee:图片保存在SD卡的文件夹中(如果您看到相机按钮代码)。我想要在网格视图中显示图像。这没有发生。 –

回答

0

这个问题似乎是在你的RecyclerActivity.class。这就是为什么你的RecyclerView没有显示任何东西。

adapter = new CameraAdapter(new ArrayList<String>(),getContext()); 
    cameraRecyclerView.setAdapter(adapter); 

应设置imagePaths的ArrayList有这样的:

adapter = new CameraAdapter(imagePaths, getContext()); 
    cameraRecyclerView.setAdapter(adapter); 

然后当ArrayList变化的数据(与检索到的数据填充之后),只需调用notifyDatasetChanged()RecyclerViewAdapter。这应该够了吧。

此外,在您的onBindViewHolder方法你有环一unnecesary:

 for (int i=0;i<imagesPath.size();i++){ 
     holder.imageview.setImageBitmap(BitmapFactory.decodeFile(imagesPath.get(position))); 
    } 
+0

如果您在按钮单击代码中看到OnActivityResult()方法,那么我将以类似方式调用填充适配器。代码是: listofImagesPath = new ArrayList (); listofImagesPath = RetriveCapturedImagePath(); (listofImagesPath!= null){ adapter.addApplications(listofImagesPath); addapplication是在Adapter中设置Adapter数组列表的方法。 –

+0

另外,OnBindViewHolder方法有 - for循环来显示文件夹中的所有图像。 –

+0

更新了我的答案。无需在适配器上调用addApplications()。当您将imagePath列表提供给适配器的构造函数,然后填充该列表(在您的活动中,而不是在您的适配器中),调用notfiyDatasetChanged将更新您的适配器并显示您的图像。 – Jeffalee