RecyclerView display multiple images

随声附和 提交于 2019-12-22 01:18:36

问题


I recently tried to code a gallery type app that can display thumbnails of images from your phone. For that I used RecyclerView and I resized the Bitmaps using this tutorial: https://developer.android.com/training/displaying-bitmaps/load-bitmap.html

However, when I run my app I realized that there was only one image displayed at a time, and not multiple like I was expecting. On this image we can clearly see the huge gap between the two pictures, and it's not a third picture that couldn't load itself.

Screenshot

Here is my code :

public class MainActivity extends AppCompatActivity {

RecyclerView recycler_received;

ArrayList<Bitmap> liste_received;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    recycler_received=(RecyclerView)findViewById(R.id.recycle_pictures_sent);

    liste_received=new ArrayList<>();

    String dir="/sdcard/DCIM/Camera/";

    String[] liste_photos={"IMG_20160521_141348.jpg","IMG_20160521_141348.jpg","IMG_20160521_141627.jpg","IMG_20160521_142946.jpg",
            "IMG_20160521_185556.jpg","IMG_20160528_174737.jpg"};

    if (isStoragePermissionGranted())
    {
        for(int i=0;i<5;i++){
            liste_received.add(ResizeBitmap.decodeSampledBitmapFromResource(getResources(),dir+liste_photos[i],200,200));
        }

    }

    Adapter adapter=new Adapter(this,liste_received);
    recycler_received.setAdapter(adapter);
    recycler_received.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));







}

public  boolean isStoragePermissionGranted() {
    if (Build.VERSION.SDK_INT >= 23) {
        if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED) {
            Log.v("info","Permission is granted");
            return true;
        } else {

            Log.v("info","Permission is revoked");
            ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
            return false;
        }
    }
    else { //permission is automatically granted on sdk<23 upon installation
        Log.v("info","Permission is granted");
        return true;
    }


}

and my adapter:

public class Adapter extends RecyclerView.Adapter<Adapter.PicHolder> {


public static class PicHolder extends RecyclerView.ViewHolder{

    public ImageView mimage_sent;

    public PicHolder(View itemview){
        super(itemview);
        mimage_sent=(ImageView)itemview.findViewById(R.id.image_gallery);
    }
}

private ArrayList<Bitmap> data;
private Context context;

public Adapter(Context mcontext, ArrayList<Bitmap> mdata){
    context=mcontext;
    data=mdata;
}

private Context getContext() {
    return context;
}


@Override
public Adapter.PicHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Context context=parent.getContext();
    LayoutInflater inflater= LayoutInflater.from(context);

    View galleryView=inflater.inflate(R.layout.recyclerview_layout, parent, false);

    PicHolder picHolder=new PicHolder(galleryView);


    return picHolder;
}

@Override
public void onBindViewHolder(PicHolder holder, int position) {

    Bitmap bitmap= data.get(position);

    ImageView imageView=holder.mimage_sent;
    imageView.setImageBitmap(bitmap);

}


@Override
public int getItemCount() {
    return data.size();
}

How could I fill the gap between two items?

Also, does anyone have an idea on how I could load every pictures stored on a directory (ex:/sdcard/DCIM/Camera) without having to list them all like I did in my MainActivity?

EDIT:

Here is the main layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


<android.support.v7.widget.RecyclerView
    android:layout_width="fill_parent"
    android:layout_height="120dp"
    android:id="@+id/recycler_pictures_received"

    >

</android.support.v7.widget.RecyclerView>

</RelativeLayout>

and my item :

<?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="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/image_gallery"/>

</LinearLayout>

回答1:


Try this code:

onCreate bundel(){

 RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(),2);
        recyclerView.setLayoutManager(layoutManager);

}


来源:https://stackoverflow.com/questions/38747018/recyclerview-display-multiple-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!