我正在使用Recyclerview来显示项目列表.现在每次都可以有一些照片/多张照片,我需要在一行中水平滚动显示.
我目前正在使用Gallery小部件来显示照片,但现在已经弃用了,所以我想要一些其他东西用于水平显示图像,具有与Gallery相同的功能.你能帮帮我吗?
非常感谢先进.
以下是水平可滚动RecycleView显示位图图像的示例:
片段/活动布局:
... ...
项目:
适配器:
public class HorizontalPhotosAdapter extends RecyclerView.Adapter{ private Context context; private LayoutInflater inflater; private ArrayList 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 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);