如何使用水平滚动Android显示图像 [英] How to show images with horizontal scrolling android

查看:79
本文介绍了如何使用水平滚动Android显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Recyclerview显示项目列表.现在每次可以有一些照片/多张照片,我需要在单行中水平滚动显示这些照片.

I am using a Recyclerview to show the list of items. Now each time can have some photos/multiple photos which I need to show with horizontal scrolling in a single row.

我目前正在使用Gallery小部件来显示照片,但由于现在已弃用,因此我希望使用其他东西来水平显示与Gallery具有相同功能的图像.你能在这里帮我吗?

I am currently using Gallery widget to show the photos but as it is now deprecated, so I want some other thing to use to show images horizontally with same features as Gallery have. Can you please help me here.

非常感谢高级

推荐答案

以下是显示位图图像的水平滚动RecycleView的示例:

Here is the example of horizontal scrollable RecycleView showing bitmap images:

片段/活动布局:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

       ...

    <android.support.v7.widget.RecyclerView
        android:id="@+id/horizontal_recycler_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_marginBottom="15dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:horizontalSpacing="10dp"
        android:isScrollContainer="false"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp"
        />
            ...

</LinearLayout>

项目:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
    android:id="@+id/horizontal_item_view_image"
    android:layout_marginRight="10dp"
    android:layout_width="109dp"
    android:layout_height="109dp" />

</LinearLayout>

适配器:

public class HorizontalPhotosAdapter extends RecyclerView.Adapter<HorizontalPhotosAdapter.MyViewHolder> {

    private Context context;
    private LayoutInflater inflater;
    private ArrayList<Bitmap> bitmapList;

    public class MyViewHolder extends RecyclerView.ViewHolder {

        private ImageView riv;

        public MyViewHolder(View view) {
            super(view);

            riv = (ImageView) view.findViewById(R.id.horizontal_item_view_image);

        }
    }


    public HorizontalPhotosAdapter(Context context, ArrayList<Bitmap> bitmapList, String[] imageUrls) {
        this.context = context;
        this.bitmapList = bitmapList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.horizontal_item_view, parent, false);

        if (itemView.getLayoutParams ().width == RecyclerView.LayoutParams.MATCH_PARENT)
            itemView.getLayoutParams ().width = RecyclerView.LayoutParams.WRAP_CONTENT;

        return new MyViewHolder(itemView);
    }

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

        holder.riv.setImageBitmap(bitmapList.get(position));

    }


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

片段/活动中的实现:

            horizontalAdapter=new HorizontalPhotosAdapter(getContext(), bitmapList);
            horizontal_recycler_view.setAdapter(horizontalAdapter);
            horizontalAdapter.notifyDataSetChanged();

            LinearLayoutManager horizontalLayoutManagaer = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
            horizontal_recycler_view.setLayoutManager(horizontalLayoutManagaer);
            horizontal_recycler_view.setAdapter(horizontalAdapter);

这篇关于如何使用水平滚动Android显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆