How to set RecyclerView Max Height

前端 未结 12 1638
陌清茗
陌清茗 2020-12-01 15:55

I want to set Max Height of RecylerView.I am able to set max height using below code.Below code makes height 60% of current screen.

     DisplayMetrics displ         


        
12条回答
  •  暖寄归人
    2020-12-01 16:16

    The simplest way is to use ConstraintLayout and setting height constraint on Recycleview.

    
    
       
        
    
    
    

    Please note that before Lollipop, the recycleview will not scroll. As a solution, add the below code in the activity's oncreate.

    if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            ConstraintLayout CL_OUTER_RV_Choose_Categories = findViewById(R.id.CL_OUTER_RV_Choose_Categories);
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) CL_OUTER_RV_Choose_Categories.getLayoutParams();
            lp.height = LinearLayout.LayoutParams.WRAP_CONTENT;
        }
    

    This will ensure that height is fixed only on supported devices, and the complete recycle view is shown on older devices.

    Do let me know if there is any better way.

提交回复
热议问题