【问题标题】:Staggered View with RecyclerviewRecyclerview 的交错视图
【发布时间】:2016-05-12 07:32:08
【问题描述】:

我正在 RecyclerView 中创建交错视图并显示 SD 卡文件夹图像。

我做了什么:我设法在 SD 卡上创建文件夹并将所有点击的图像保存在那里。

iSSUE:现在我无法在交错回收站视图的文件夹中显示这些图像。

回收站视图布局:

<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;
    }

适配器类:

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 卡文件夹中的所有图像。但 无法在交错视图中显示它们。

请帮忙

【问题讨论】:

  • 你没有看到任何图片或发生了什么吗?
  • @Jeffalee:图片保存在 SD 卡的文件夹中(如果您看到相机按钮代码)。我希望这些图像显示在 Grid view 中。这没有发生。

标签: android android-camera android-recyclerview android-sdcard staggered-gridview


【解决方案1】:

问题似乎出在您的RecyclerActivity.class 上,当将Adapter 设置为RecyclerView 时,您添加的是一个空的ArrayList。这就是为什么您的 RecyclerView 没有显示任何内容的原因。

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

你应该像这样设置 imagePaths 的ArrayList

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

然后当ArrayList中的数据发生变化时(用检索到的数据填充后),只需在RecyclerViewAdapter上调用notifyDatasetChanged()即可。这应该可以解决问题。

此外,在您的 onBindViewHolder 方法中,您有一个不必要的 for 循环:

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

【讨论】:

  • 如果您在按钮单击代码中看到 OnActivityResult() 方法,我正在调用类似的填充适配器。代码是:listofImagesPath =new ArrayList(); listofImagesPath = RetriveCapturedImagePath(); if(listofImagesPath!=null){ adapter.addApplications(listofImagesPath); addapplication 是 Adapter 中设置 Adapter 数组列表的方法。
  • 另外,OnBindViewHolder 方法有 for - 循环来显示文件夹中的所有图像。
  • 更新了我的答案。无需在适配器上调用 addApplications()。当您将 imagePath 列表提供给适配器的构造函数,然后填充该列表(在您的活动中,而不是在您的适配器中)时,调用 notfiyDatasetChanged 将更新您的适配器并显示您的图像。
  • 是的,但是做了以上所有操作,仍然没有在 recyclerview 中显示图像
猜你喜欢
  • 1970-01-01
  • 2018-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多