Home > database >  How to build a custom horizontal recycler view like Play Store in Android?
How to build a custom horizontal recycler view like Play Store in Android?

Time:09-30

Playstore Recycler View

I already know how to make horizontal RecyclerView, but I want to make a custom recycler view having a blank space at the starting, and when we start to scroll the background image gets faded.

CodePudding user response:

  • just make your adapter pretend to be having more data than it actually has. when it gives you position <n (n : number of items you want empty at the beginning) don't load any image into and leave it empty. when it asks about the data size in getItemCount() return your data size n.
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder>{
    ...
    private final int N  =2 ;
    @Override
    public PageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {...}
    @Override
    public void onBindViewHolder(@NonNull PageViewHolder holder, int position) {
         if(position <N)return ;
         else {
             // bind your holder with data
         }
    }

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

  • you might also make use of the view Type value by overriding method getItemViewType and return value that indicates if you want this view holder to be empty or to have another behavior than the regular view holder.
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder>{
    ...
    private final int N  =2 ;
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
         View v ;
         if(viewType ==0){
             v = LayoutInflater.from(context).inflate(R.layout.view_holder_item_1, parent, false);
         }else {
             v = LayoutInflater.from(context).inflate(R.layout.view_holder_item_2, parent, false);
         }
         return new new MyViewHolder(v.getRootView()) ;
}
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
         if(position <N){
             // bind your holder view of type 1 with data you want
         }
         else {
             // bind your holder view of type 2 with data you want
         }
    }

    @Override
    public int getItemCount() {
        return pageEntities.size()  N;
    }
    @Override
    public int getItemViewType(int position) {
        return position <N ?0:1 ;
    }
}

where your type 1 in this case would be the empty view holder.

  • Related