How to show images with horizontal scrolling android

扶醉桌前 提交于 2019-12-23 03:05:12

问题


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.

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.

Thanks a lot in advanced.


回答1:


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

fragment/activity layout:

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

item:

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

adapter:

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

implementation in fragment/activity:

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



回答2:


You can use HorizontalScrollView or ViewPager for your purpose.

Android Viewpager as Image Slide Gallery




回答3:


You can use viewpager to implement slide show gallery

follow link -

http://codetheory.in/android-image-slideshow-using-viewpager-pageradapter/




回答4:


A LayoutManager is responsible for measuring and positioning item views within a RecyclerView as well as determining the policy for when to recycle item views that are no longer visible to the user. By changing the LayoutManager a RecyclerView can be used to implement a standard vertically scrolling list, a uniform grid, staggered grids, horizontally scrolling collections and more. Several stock layout managers are provided for general use.

Try the below code to get recycler view with horizontal item

    LinearLayoutManager ll_manager= new LinearLayoutManager(getActivity());
    ll_manager.setOrientation(LinearLayoutManager.HORIZONTAL);
    recyclerView.setLayoutManager(llmRecent);



回答5:


You can also use the following piece of code:

GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 1);
gridLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(gridLayoutManager);


来源:https://stackoverflow.com/questions/34674576/how-to-show-images-with-horizontal-scrolling-android

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