Skip items in recycler view

There's a very simple fix for this:

If you perform view.setVisibility(View.GONE); on the view while binding it to the ViewHolder, the view would be hidden but there would still be a space right where the view was supposed to be; therefore, this approach isn't efficient.

How then do we solve this problem?

All you need to do is to set the height and/or width of the view you're trying to hide to zero. Here's a simple way to achieve this:

View Holder:

    public class MyViewHolder extends RecyclerView.ViewHolder{

        public LinearLayout.LayoutParams params;
        public LinearLayout rootView //the outermost view from your layout. Note that it doesn't necessarily have to be a LinearLayout.

        //todo: Don't forget to add your other views

        public MyViewHolder(View itemView){
            super(itemView);

            params = new LinearLayout.LayoutParams(0, 0);
            rootView = itemView.findViewById(R.id.rootView);

            //todo: Don't forget to initialize your views


        }

    }

onBindViewHolder:

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position){

    if(your_condition){
        holder.rootView.setLayoutParams(holder.params);  
        //this line hides the view completely
    }
    else{
        //todo: Do your regular stuff
    }

}

I hope this helps. Merry coding!


Lets go in depth as of how recycler view works

we have 2 functions onCreateView and onBindview. As the names of functions are quite self explaining onCreateView creates the view and onBindView takes the created view and binds data into it

now lets assume that entire view type is similar and you use an array of objects or cursor to populate the entire view.

so in bindView in order to fetch data you must have used either

 cursor.moveToPosition(position)

or

 mList.get(position)

as you can see that binding is happening based on the position that we get from onBindView arguments

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
     //mList.get(position) or cursor.moveToPosition
 }

so you can use this knowledge to specifically skip binding of view

say you have a function which accepts postion as parameter and returns actual position as result

private int getActualPostion(int position){
     //your logic to skip the given postion 
     //say  if(position == 4){return postion+2}
}

so you can implement something like this

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
       mList.get(getActualPosition(position));
 }

this will allow you to skip those view which are not to be shown

finally in method getCount which is used by recycler view to decide the number of views

 @Override
 public int getItemCount() {
     //foreach in array { if(already downloaded) i++} 
     // return array.size - i
 }

I hope this helps this will also give your more flexibility in a way that u may add more filters and use same dataset ... skip views more easily