简体   繁体   中英

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

Playstore 回收站视图

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.

  • 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM