Search code examples
androidlayoutandroid-recyclerviewrecyclerview-layout

Skip items in recycler view


Hi I want to skip some items from recyclerview.

here is the screenshot

Here is the bit of code

item_Data.xml

 <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:id="@+id/mainlayout"
android:layout_height="wrap_content">
<ImageView
    android:visibility="gone"
    android:id="@id/image"
    android:layout_gravity="center"
    android:layout_width="100dp"
    android:layout_height="100dp" />
<TextView
    android:visibility="gone"
    android:textStyle="bold"
    android:id="@id/title"
    android:layout_gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:maxLength="15" />

And recycler view is

   @Override
public void onBindViewHolder(final MovieViewHolder holder, final int position) {
    String download= news.get((position)).getDownloadLinkForApkNew();
    String desc_new = news.get(position).getApkJData();
    if (download.isEmpty()==false && desc_new.isEmpty()==false) {
        holder.movieTitle.setVisibility(View.VISIBLE);
        holder.imageView.setVisibility(View.VISIBLE);
        Picasso.with(context).load(news.get((position)).getBetterFeaturedImage().getSourceUrl()).into(holder.imageView);
        holder.movieTitle.setText(news.get(position).getTitle().getRendered());
    }

I don't want the items that doesn't have download and desc_new. My logic works items are not visible but they leave there space. how can I remove the spaces between the items.


Solution

  • 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