Skip items in recycler view

前端 未结 7 1231
鱼传尺愫
鱼传尺愫 2020-12-20 19:17

Hi I want to skip some items from recyclerview.

\"here

Here is the bi

7条回答
  •  太阳男子
    2020-12-20 19:45

    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!

提交回复
热议问题