【问题标题】:how to create image gallery with horizontal scroll如何使用水平滚动创建图片库
【发布时间】:2016-04-19 11:02:47
【问题描述】:

如何在 android studio 中制作一个水平滚动的图片库,其中图片可以在屏幕上显示一个图像,并且左右两侧的按钮可以移动到其他图像

【问题讨论】:

  • 同样使用viewpager..

标签: android image scroll gallery swap


【解决方案1】:

您可以使用ViewPager 代替HorizontalScrollView

请关注tutorial

【讨论】:

    【解决方案2】:

    您也可以使用水平回收视图。代码如下:

    片段/活动布局:

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

    【讨论】:

      猜你喜欢
      • 2021-03-07
      • 2015-06-08
      • 1970-01-01
      • 1970-01-01
      • 2020-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多