【发布时间】:2016-01-08 10:20:12
【问题描述】:
我正在使用 Recyclerview 来显示项目列表。现在每次都可以有一些照片/多张照片,我需要在一行中水平滚动显示。
我目前正在使用 Gallery 小部件来显示照片,但由于它现在已被弃用,所以我想要使用其他东西来水平显示图像,并具有与 Gallery 相同的功能。你能帮帮我吗?
非常感谢。
【问题讨论】:
我正在使用 Recyclerview 来显示项目列表。现在每次都可以有一些照片/多张照片,我需要在一行中水平滚动显示。
我目前正在使用 Gallery 小部件来显示照片,但由于它现在已被弃用,所以我想要使用其他东西来水平显示图像,并具有与 Gallery 相同的功能。你能帮帮我吗?
非常感谢。
【问题讨论】:
以下是显示位图图像的水平可滚动 RecycleView 示例:
片段/活动布局:
<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);
【讨论】:
您可以根据需要使用 HorizontalScrollView 或 ViewPager。
【讨论】:
您可以使用 viewpager 来实现幻灯片放映库
点击链接 -
http://codetheory.in/android-image-slideshow-using-viewpager-pageradapter/
【讨论】:
LayoutManager 负责测量和定位 RecyclerView 中的项目视图,并确定何时回收用户不再可见的项目视图的策略。通过更改 LayoutManager,可以使用 RecyclerView 实现标准的垂直滚动列表、统一网格、交错网格、水平滚动集合等。提供了几个库存布局管理器供一般使用。
尝试下面的代码以获取带有水平项目的回收站视图
LinearLayoutManager ll_manager= new LinearLayoutManager(getActivity());
ll_manager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(llmRecent);
【讨论】:
你也可以使用下面这段代码:
GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 1);
gridLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(gridLayoutManager);
【讨论】: