How to set the height of an item row in GridLayoutManager

后端 未结 8 626
有刺的猬
有刺的猬 2021-01-31 03:18

My Recycler Item which inflate in onCreateViewHolder




        
相关标签:
8条回答
  • 2021-01-31 03:46

    On a recent API Level currently 25 the height from the recycler in onCreateViewHolder is always empty. This snippet is to set the hight after the recycler view's onMeasure is invoked and set the correct height to the inflated list view.

    @Override
    public DataBindingViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
        // You may inflate your view here.
        parent.post(new Runnable() {
    
            @Override
            public void run() {
                int height = parent.getMeasuredHeight() / rows;
                View view = holder.getBinding().getRoot();
                view.getLayoutParams().height = height;
            }
        });
        return holder;
    }
    
    0 讨论(0)
  • 2021-01-31 03:50

    sometime, getting size of inflate view in adapter return 0 or negative. another approach is get required size from out side the adapter, manipulate it and set it into view. in my case, another problem was size set effectless. so i set the size using layout parameter

    here is setting my adapter in activity:

    Display display = MainActivity.this.getWindowManager().getDefaultDisplay();
                        Point size = new Point();
                        display.getSize(size);
                        int  y = size.y;
                        y=(int)y/2;
     GridLayoutManager linearLayoutManager = new GridLayoutManager(MainActivity.this,2);
                        recyclerView.setLayoutManager(linearLayoutManager);
                        NewOrderAdapter newOrderAdapter=new NewOrderAdapter(MainActivity.this,arrayListname,arrayListimage,y);
                        recyclerView.setAdapter(newOrderAdapter);
    

    and i set view size like this:

    @Override
        public myviewholder onCreateViewHolder(ViewGroup viewGroup, int i) {
            View view = inflator.inflate(R.layout.new_order_row, viewGroup, false);
            GridLayoutManager.LayoutParams params = (GridLayoutManager.LayoutParams) view.getLayoutParams();
            params.height = ysize;
            view.setLayoutParams(params);
            myviewholder holder = new myviewholder(view);
            return holder;
        }
    

    and dont forget to set a height to layout in your layout for initializatiin

    0 讨论(0)
  • 2021-01-31 03:52

    As of support library 25.1.0, ABOVE ANSWER DOESN'T WORK. I suggest below modifications:

        public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
            GridLayoutManager.LayoutParams lp = (GridLayoutManager.LayoutParams) v.getLayoutParams();
            lp.height = parent.getMeasuredHeight() / 4;
            v.setLayoutParams(lp);
            return new ViewHolder(v);
        }
    
    0 讨论(0)
  • 2021-01-31 03:54

    You don't need to set the height of an item. The problem here is that image tries to fill all the space. Just add android:adjustViewBounds="true" to your ImageView and it will not add blank spaces

    0 讨论(0)
  • 2021-01-31 03:54

    kotlin version

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ItemBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        binding.root.post {
            binding.root.layoutParams.height = parent.width/3
            binding.root.requestLayout()
        }
        return ViewHolder(binding)
    }
    

    here 3 is the span count of your GridLayoutManager . You can replace binding.root with your itemView , if you are not using Databinding

    0 讨论(0)
  • 2021-01-31 03:57

    When inflating layout for your views in adapter, you can set their height programmatically. In order to evaluate proper height to use you can rely on parent ViewGroup (that is the RecyclerView itself). Here it is a sample:

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = mLayoutInflater.inflate(R.layout.view_item, parent, false);
        // work here if you need to control height of your items
        // keep in mind that parent is RecyclerView in this case
        int height = parent.getMeasuredHeight() / 4;
        itemView.setMinimumHeight(height);
        return new ItemViewHolder(itemView);        
    }
    

    Hope this could help.

    0 讨论(0)
提交回复
热议问题