RecyclerView显示多个图像

时间:2016-08-03 14:52:11

标签: android bitmap android-recyclerview

我最近尝试编写一个可以显示手机图像缩略图的图库类型应用。为此,我使用了RecyclerView,并使用本教程重新调整了位图:https://developer.android.com/training/displaying-bitmaps/load-bitmap.html

然而,当我运行我的应用程序时,我意识到一次只显示一个图像,而不是像我期待的那样多个。在这张图片上,我们可以清楚地看到两张照片之间的巨大差距,而且这不是第三幅无法加载的照片。

Screenshot

这是我的代码:

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


}

和我的适配器:

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

我怎样填补两个项目之间的空白?

此外,是否有人知道如何加载存储在目录(ex:/sdcard/DCIM/Camera)上的每张图片,而不必像我在MainActivity中那样列出所有图片?

编辑:

这是主要布局:

<?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>

和我的项目:

<?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 个答案:

答案 0 :(得分:0)

试试这段代码:

onCreate bundel(){

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

}