📜  Android 多图像视图 (1)

📅  最后修改于: 2023-12-03 14:59:16.301000             🧑  作者: Mango

Android多图像视图

在Android开发中,我们经常需要显示多张图片,比如一个相册应用显示用户所有照片,或者一个商品详情页面展示商品多个角度的照片等。这时候,就需要使用Android的多图像视图控件。

GridView

GridView是一个常见的多图像视图控件,它可以显示多张图片,并且可以滚动浏览。GridView可以通过Adapter来填充数据,Adapter可以根据需求自定义布局和样式。

布局
<GridView
    android:id="@+id/grid_view"
    android:numColumns="3"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
Adapter
public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    private List<Bitmap> mBitmapList;

    public ImageAdapter(Context context, List<Bitmap> bitmapList) {
        this.mContext = context;
        this.mBitmapList = bitmapList;
    }

    @Override
    public int getCount() {
        return mBitmapList.size();
    }

    @Override
    public Object getItem(int position) {
        return mBitmapList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(mContext);
        } else {
            imageView = (ImageView) convertView;
        }
        imageView.setImageBitmap(mBitmapList.get(position));
        return imageView;
    }
}
使用
GridView gridView = findViewById(R.id.grid_view);
ImageAdapter adapter = new ImageAdapter(this, mBitmapList);
gridView.setAdapter(adapter);
RecyclerView

RecyclerView是Android提供的另一个多图像视图控件,它与GridView类似,但是更加强大和灵活。RecyclerView支持多种布局方式,可以实现ListView、GridView等多种布局效果。Adapter同样可以进行自定义,需要实现RecyclerView.Adapter和RecyclerView.ViewHolder。

布局
<RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
Adapter
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ViewHolder> {
    private Context mContext;
    private List<Bitmap> mBitmapList;

    public ImageAdapter(Context context, List<Bitmap> bitmapList) {
        this.mContext = context;
        this.mBitmapList = bitmapList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_image, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.imageView.setImageBitmap(mBitmapList.get(position));
    }

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

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;

        ViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.image_view);
        }
    }
}
使用
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
ImageAdapter adapter = new ImageAdapter(this, mBitmapList);
recyclerView.setAdapter(adapter);
总结

在Android中,多图像视图控件是非常常见的控件,GridView和RecyclerView是两种常用的控件,它们可以帮助我们快速构建出多图像的UI界面。通过Adapter和ViewHolder的自定义,我们可以实现更加灵活、更加多样的布局效果。