2

In my case I have recycler view with Grid Layout Manager and custom Decorator. In every row(which have 3 column maximum) can be from 1 to 3 items.

The question is how to draw empty cells in case when count of items in the line are one or two? Warning!!! It’s not a lastRow!

Example: Span count in row 3, elements from collection to draw = 2 how to draw empty cell in 3rd column? Span count in row 3, elements from collection to draw = 1 how to draw empty cell in 2nd and 3rd columns?

S. Matsepura
  • 1,673
  • 1
  • 17
  • 24
Gevork Safaryan
  • 237
  • 1
  • 7
  • 1
    I can think of adding 'dummy' empty data (null) and when the data is null, drawing an empty field in your adapter. Maybe someone else knows of a better solution :D – Vucko Jun 14 '17 at 07:44
  • 1
    @Vucko Thanks for reply bro, in my case this is a bad option so I asked the question in search of a more exotic solution. Since the search in Google did not lead to anything. – Gevork Safaryan Jun 14 '17 at 07:49
  • How about the 2nd answer in this [question](https://stackoverflow.com/questions/28531996/android-recyclerview-gridlayoutmanager-column-spacing/28533234) – Vucko Jun 14 '17 at 07:55
  • Can you tell what's your use case? What's the point of drawing those empty cells? – azizbekian Jun 16 '17 at 15:41

2 Answers2

1

Adapter for Recyclerview has to know about the view. For this purpose we use getItemViewType.By getting view type we can add any view to the recyclerview.

public class MainRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private final static int ADD_VIEW_HOLDER = 0;
private final static int EMPTY_VIEW_HOLDER = 1;

public List<DataList> mDrinkItems;


@Override
public int getItemViewType(int position) {
    if (position== mDrinkItems.size()-1)
        return EMPTY_VIEW_HOLDER;
    else
        return ADD_VIEW_HOLDER;
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    switch (viewType){
        case ADD_VIEW_HOLDER:
            View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.li_main_content,parent,false);
            return new MainItemHolder(view);
        case EMPTY_VIEW_HOLDER:
            View EMPTYView= LayoutInflater.from(parent.getContext()).inflate(R.layout.li_EMPTY_action,parent,false);
            return  new EMPTYViewItemHolder(EMPTYView);
            default:
                 return null;
    }
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    int size= mDrinkItems.size();
    switch (getItemViewType(position)){
        case ADD_VIEW_HOLDER:
            ((MainItemHolder)holder).bindData(mDrinkItems.get(position),position,size);
            break;
        case EMPTY_VIEW_HOLDER:
            ((EMPTYViewItemHolder)holder).bindData(mDrinkItems.get(position),position,size);
            break;
    }

}

@Override
public int getItemCount() {
    return mDrinkItems.size();
}

public void setData(List<DailyConsumption> mlist) {
    this.mDrinkItems = mlist;
    notifyDataSetChanged();
}

When adding data to list to set adapter you get the last index of list to set empty view.

List<DataList> mList = new ArrayList<>();
mList.add(mList.size(),new DataList(// whatever you want to add));
Shahkar Raza
  • 355
  • 3
  • 11
0

You can achieve it by defining different view types.

Just Override the getItemViewType()

@Override
    public int getItemViewType(int position) {
            if (position < mElementsList.size()) {
                return ITEM_TYPE_NORMAL;
            } else if (position >= mElementsList.size()) {
                return ITEM_TYPE_EMPTY;
            }
        }
    }

Then Override onCreateViewHolder

onCreateViewHolder(Viewgroup parent, int viewType) {
   if(viewType == ITEM_TYPE_NORMAL) {
     //Use layout for normal type
   } else if (viewType == ITEM_TYPE_EMPTY) {
     //Use layout for emptyType
   }
}
user2991413
  • 521
  • 2
  • 9
  • 26