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.
source share