Android: how to implement image gallery using RecyclerView

I want to create an image gallery with the title TextView and ImageViews in the HorizontalScrollView , as shown in the image.

I use RecyclerView to reduce resources for my application and implement images with many ArrayList , for example:

  itemFotoIDs.addAll(Arrays.asList( R.drawable.cable_crunches_1 ,R.drawable.dips_1 ,R.drawable.kabelrudern_breit_1 ,R.drawable.kabelrudern_eng_1 ,R.drawable.klimmzug_1 ,R.drawable.kh_schulterdruecken_1 ,R.drawable.kh_curls_1 ,R.drawable.lh_bankdruecken_1 ,R.drawable.lh_bankdruecken_eng_1 ,R.drawable.lh_kniebeuge_1 ,R.drawable.lh_kreuzheben_1 ,R.drawable.lh_schraegbankdruecken_1 ,R.drawable.latzug_obergriff_1 ,R.drawable.seitheben_1 ,R.drawable.szhantel_curls_1 )); 

Now my question is:. What is the best solution to create an empty record in a string of this ArrayList if, for example, I have five ArrayList with 15 records, and two have only 14 or 13 records. But I want the empty entry in the exact line, not at the end. I tried to implement null instead of R.drawable. , but this leads to a crash of the application.

+5
source share
1 answer

I would like to send my own answer in a few similar questions: fooobar.com/questions/1239162 / ... (NB! Go ahead , I would use class / method names)

The only difference would be to replace TextViews with ImageView s. Like this:

 <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="@drawable/rounded_background" android:layout_margin="4dp" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:id="@+id/chipImageView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </FrameLayout> 

As for empty slots, there are several options:

  • You can pass null, and then to what was called ChipView (in your case, viewing with an image), in the displayItem() method, check if the parameter is null - then just set the correct width in View
  • You can create a separate viewType for the horizontal RecyclerView and create an empty view for it (as a placeholder). Then you need to update the ChipsAdapter :

     static int IMAGE_VIEW_TYPE = 1; static int EMPTY_SLOP_TYPE = 2; @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if (viewType = IMAGE_VIEW_TYPE) { return new ChipViewHolder(new ChipView(parent.getContext())); } else { return new ChipViewHolder(new EmptyView(parent.getContext())); } } @Override public int getItemViewType(int position) { return chipsArray[position] == null? EMPTY_SLOP_TYPE : IMAGE_VIEW_TYPE; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { if (holder.itemView instanceof ChipView) { ((ChipView) holder.itemView).displayItem(chipsArray[position]); } } 

    And create an EmptyView with a predefined width of non-0.

Personally, I prefer the second option (denouement, it is easier to support onClick - without additional logic there, etc.)

Hope this helps.

+1
source

Source: https://habr.com/ru/post/1239263/


All Articles